SingleAddressableAssetWindow.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. using XGame.Editor.Asset.Addressable;
  2. using XGame.Framework.Asset;
  3. using XGame.Framework.Asset.Addressable;
  4. using XGame.Framework.Asset.Addressable.Data;
  5. using System.IO;
  6. using UnityEditor;
  7. using UnityEngine;
  8. namespace XGame.Editor.Asset
  9. {
  10. public class SingleAddressableAssetWindow : EditorWindow
  11. {
  12. [MenuItem("Assets/AddressableName", priority = 99)]
  13. private static void CreateWindow()
  14. {
  15. var activeObject = Selection.activeObject;
  16. var assetPath = AssetDatabase.GetAssetPath(activeObject);
  17. if (!AddressableHelper.TryAssetPathToClassify(assetPath, out var classify))
  18. {
  19. return;
  20. }
  21. //检查选中的资源是否有配置记录
  22. var manifest = AddressableHelper.LoadAssetManifest(classify);
  23. if (manifest == null)
  24. {
  25. Debug.LogError($"Can't find manifest file. Path: {PathDefine.ProductAssetManifestPath}");
  26. return;
  27. }
  28. var assetInfos = manifest.GetAssetInfos();
  29. var guid = AssetDatabase.AssetPathToGUID(assetPath);
  30. var index = assetInfos.FindIndex((info => info.assetGUID.Equals(guid)));
  31. if (index < 0)
  32. {
  33. Debug.LogError($"Can't find AddressableAssetInfo. Please select a valid object. Path:{assetPath} File:{Path.GetFileNameWithoutExtension(assetPath)}");
  34. return;
  35. }
  36. var window = EditorWindow.GetWindow<SingleAddressableAssetWindow>(true, "AddressableNameEditor");
  37. window.Show();
  38. window.Init(manifest, classify, assetInfos[index]);
  39. }
  40. [MenuItem("Assets/AddressableName", true, priority = 99)]
  41. private static bool VerifyCreateWindow()
  42. {
  43. var path = AssetDatabase.GetAssetPath(Selection.activeObject);
  44. if (string.IsNullOrEmpty(path) || Directory.Exists(path))
  45. {
  46. return false;
  47. }
  48. return (!FileUtil.IsFileIgnore(path) && AddressableHelper.IsValidPath(path));
  49. }
  50. private AddressableAssetFile assetFile;
  51. private AddressableInfosSo manifest;
  52. private AddressableClassify classifyOperation;
  53. private void Init(AddressableInfosSo manifest, AddressableClassify classify, AssetInfo assetInfo)
  54. {
  55. var assetPath = AssetDatabase.GetAssetPath(Selection.activeObject);
  56. //if (!AddressableHelper.TryAssetPathToClassify(assetPath, out var classify))
  57. //{
  58. // return;
  59. //}
  60. this.manifest = manifest;
  61. classifyOperation = classify;
  62. //var assetInfos = manifest.GetAssetInfos();
  63. //var guid = AssetDatabase.AssetPathToGUID(assetPath);
  64. //var index = assetInfos.FindIndex((info => info.assetGUID.Equals(guid)));
  65. assetFile?.Clear();
  66. assetFile = new AddressableAssetFile(assetPath, assetInfo)
  67. {
  68. IsFoldout = true
  69. };
  70. assetFile.OnApply += OnApplyFile;
  71. }
  72. //private void OnEnable()
  73. //{
  74. //}
  75. private void OnDisable()
  76. {
  77. assetFile?.Clear();
  78. manifest = null;
  79. }
  80. private void OnGUI()
  81. {
  82. assetFile?.GUI();
  83. }
  84. private bool OnApplyFile(ApplyEvnetContext context)
  85. {
  86. if (classifyOperation == AddressableClassify.BuiltIn)
  87. {
  88. Alert("内置资源禁止修改AddressableName.");
  89. return false;
  90. }
  91. if (classifyOperation == AddressableClassify.Backup)
  92. {
  93. Alert("禁止修改备份文件.");
  94. return false;
  95. }
  96. return AddressableHelper.TryApplyAddressableName(manifest, context.assetGUID, context.addressableName);
  97. //return manifest?.TryApplyAddressableName(guid, addressableName) ?? false;
  98. }
  99. private void Alert(string content)
  100. {
  101. EditorWindow.focusedWindow.ShowNotification(new GUIContent(content));
  102. }
  103. }
  104. }