ABPackerInfoItem.cs 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. using XGame.Editor.Asset.Tools;
  2. using System.IO;
  3. using UnityEditor;
  4. using UnityEngine;
  5. namespace XGame.Editor.Build.AssetBundles
  6. {
  7. public class ABPackerInfoItem : IReorderableItem, System.IDisposable, IReorderableItemData<ABPackerInfo>
  8. {
  9. public ABPackerInfo Data { get; private set; }
  10. public float Height { get; private set; }
  11. private readonly GUIContent assetContent = new GUIContent("Asset", "资源或者文件夹,只能选择Res/Addressable目录下的资源");
  12. private readonly GUIContent packerTypeContent = new GUIContent("Packer Type", "打包分类 \r\nTopDirectoryOnly:默认方式,当前目录的资源(不包含子目录)打一个包 \r\nAllDirectories:当前目录及子目录的资源打一个包 \r\nSingleAsset:单个资源打包");
  13. private Object _assetObj;
  14. public ABPackerInfoItem(ABPackerInfo data)
  15. {
  16. Data = data;
  17. _assetObj = AssetDatabase.LoadMainAssetAtPath(data.AssetPath);
  18. }
  19. void IReorderableItem.OnGUI(Rect rect)
  20. {
  21. var lastAsset = _assetObj;
  22. Height = 0;
  23. var lineH = EditorGUIUtility.singleLineHeight;
  24. var tempRect = rect;
  25. tempRect.x += 14;
  26. tempRect.width -= 14;
  27. tempRect.height = lineH;
  28. _assetObj = EditorGUI.ObjectField(tempRect, assetContent, _assetObj, typeof(Object), false);
  29. tempRect.y += lineH;
  30. Height += lineH;
  31. var assetPath = AssetDatabase.GetAssetPath(_assetObj);
  32. if (!string.IsNullOrEmpty(assetPath))
  33. {
  34. if (!AssetBundleUtils.IsValidPath(assetPath))
  35. //if (FileUtil.IsFileIgnore(assetPath) || !assetPath.StartsWith(FileUtil.ToRelativePath(PathDefine.Res/Addressable)))
  36. {
  37. EditorUtility.DisplayDialog("资源选择错误", $"选择了错误的资源或文件夹,只能选择Res/Addressable目录下的资源. Path:{assetPath}", "确认");
  38. _assetObj = lastAsset;
  39. assetPath = AssetDatabase.GetAssetPath(_assetObj);
  40. }
  41. }
  42. Data.GUID = AssetDatabase.AssetPathToGUID(assetPath);
  43. var isFile = (!string.IsNullOrEmpty(assetPath) && File.Exists(assetPath));
  44. if (isFile)
  45. {
  46. Data.packerType = ABPackerType.SingleAsset;
  47. EditorGUI.BeginDisabledGroup(true);
  48. }
  49. Data.packerType = (ABPackerType)EditorGUI.EnumPopup(tempRect, packerTypeContent, Data.packerType);
  50. if (isFile)
  51. {
  52. EditorGUI.EndDisabledGroup();
  53. }
  54. tempRect.y += lineH;
  55. Height += lineH;
  56. }
  57. void System.IDisposable.Dispose()
  58. {
  59. Data = null;
  60. _assetObj = null;
  61. }
  62. }
  63. }