ABAssetItem.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. using XGame.Editor.Asset;
  2. using UnityEditor;
  3. using UnityEngine;
  4. using XGame.Editor.Reorderable;
  5. namespace XGame.Editor.Build.AssetBundles
  6. {
  7. public class ABAssetItem : IReorderableItem, System.IDisposable, IReorderableItemData<string>
  8. {
  9. public float Height { get; private set; }
  10. public string Data { get; private set; }
  11. private Object _assetObj;
  12. public ABAssetItem(string assetGuid)
  13. {
  14. Data = assetGuid;
  15. var assetPath = AssetDatabase.GUIDToAssetPath(assetGuid);
  16. _assetObj = AssetDatabase.LoadMainAssetAtPath(assetPath);
  17. }
  18. void IReorderableItem.OnGUI(Rect rect)
  19. {
  20. var lastAsset = _assetObj;
  21. Height = 0;
  22. var lineH = EditorGUIUtility.singleLineHeight;
  23. var tempRect = rect;
  24. tempRect.x += 14;
  25. tempRect.width -= 14;
  26. tempRect.height = lineH;
  27. _assetObj = EditorGUI.ObjectField(tempRect, _assetObj?.name ?? "", _assetObj, typeof(Object), false);
  28. tempRect.y += lineH;
  29. Height += lineH;
  30. var assetPath = AssetDatabase.GetAssetPath(_assetObj);
  31. if (!string.IsNullOrEmpty(assetPath))
  32. {
  33. if (!assetPath.StartsWith(PathDefine.ResAddressableRelative))
  34. //if (FileUtil.IsFileIgnore(assetPath) || !assetPath.StartsWith(FileUtil.ToRelativePath(PathDefine.ResAddressablePath)))
  35. {
  36. EditorUtility.DisplayDialog("资源选择错误", $"选择了错误的资源或文件夹,只能选择Assets/Res/Addressable目录下的资源. Path:{assetPath}", "确认");
  37. _assetObj = lastAsset;
  38. assetPath = AssetDatabase.GetAssetPath(_assetObj);
  39. }
  40. }
  41. Data = AssetDatabase.AssetPathToGUID(assetPath);
  42. }
  43. void System.IDisposable.Dispose()
  44. {
  45. _assetObj = null;
  46. }
  47. }
  48. }