AddressableAssetFile.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. using XGame.Framework.Asset;
  2. using XGame.Framework.Asset.Addressable.Data;
  3. using UnityEditor;
  4. using UnityEngine;
  5. namespace XGame.Editor.Asset.Addressable
  6. {
  7. /// <summary>
  8. /// 文件
  9. /// </summary>
  10. public class AddressableAssetFile : AddressableAssetEntry
  11. {
  12. private readonly long addressableId;
  13. private string addressableName;
  14. private readonly string assetGUID;
  15. public string AssetGUID => assetGUID;
  16. //private AddressableAssetType assetType;
  17. private string relativePath;
  18. private Object asset;
  19. /// <summary>
  20. /// 刷新名字的监听事件
  21. /// </summary>
  22. private event System.Func<ApplyEvnetContext, bool> onApply;
  23. internal event System.Func<ApplyEvnetContext, bool> OnApply
  24. {
  25. add => onApply += value;
  26. remove => onApply -= value;
  27. }
  28. /// <summary>
  29. /// 移除资源的监听事件
  30. /// </summary>
  31. private event System.Action<string> onRemove;
  32. public event System.Action<string> OnRemove
  33. {
  34. add => onRemove += value;
  35. remove => onRemove -= value;
  36. }
  37. internal FileSign Sign { set; get; }
  38. public override EntryType EnType => EntryType.File;
  39. public AddressableAssetFile(string path, AssetInfo assetInfo) : base(path)
  40. {
  41. addressableId = assetInfo.addressableId;
  42. addressableName = assetInfo.addressableName;
  43. assetGUID = assetInfo.assetGUID;
  44. //assetType = assetInfo.assetType;
  45. relativePath = assetInfo.relativePath;
  46. //var path = AssetDatabase.GUIDToAssetPath(assetGUID);
  47. if (string.IsNullOrEmpty(path))
  48. {
  49. Debug.LogError($"AddressableAssetInfo Error: {assetInfo}");
  50. return;
  51. }
  52. asset = AssetDatabase.LoadMainAssetAtPath(path);
  53. }
  54. protected override void OnSetParent()
  55. {
  56. }
  57. protected override void OnGUI()
  58. {
  59. //开始垂直排序
  60. EditorGUILayout.BeginVertical("Box");
  61. IsFoldout = EditorGUILayout.Foldout(IsFoldout, ShowName);
  62. if (IsFoldout)
  63. {
  64. //展示区域,禁止操作
  65. EditorGUI.BeginDisabledGroup(true);
  66. var assetContent = new GUIContent("Asset", "Asset Object");
  67. EditorGUILayout.ObjectField(assetContent, asset, typeof(Object), false);
  68. EditorGUILayout.LongField("Addressable ID", addressableId);
  69. //EditorGUILayout.EnumFlagsField("Asset Type", assetType);
  70. relativePath = EditorGUILayout.TextField("Relative Path", relativePath);
  71. EditorGUI.EndDisabledGroup();
  72. //可编辑区域
  73. var nameContent = new GUIContent("Addressable Name", "资源名字,只允许使用小写字母, 数字和下划线的组合");
  74. addressableName = EditorGUILayout.TextField(nameContent, addressableName);
  75. //按钮
  76. DrawButtons();
  77. }
  78. //结束垂直排序
  79. EditorGUILayout.EndVertical();
  80. }
  81. protected override void OnClear()
  82. {
  83. asset = null;
  84. onApply = null;
  85. onRemove = null;
  86. }
  87. private void DrawButtons()
  88. {
  89. //按钮横板排序
  90. EditorGUILayout.BeginHorizontal();
  91. if (Sign == FileSign.Invalid)
  92. {
  93. // 无效资源
  94. bool isRemove = GUILayout.Button("Remove", GUILayout.Width(60));
  95. if (isRemove)
  96. {
  97. onRemove?.Invoke(this.assetGUID);
  98. }
  99. }
  100. else
  101. {
  102. string fileName = string.Empty;
  103. // 有效资源
  104. bool isApply = GUILayout.Button("Apply", GUILayout.Width(60));
  105. if (isApply)
  106. {
  107. fileName = addressableName;
  108. }
  109. if (asset != null)
  110. {
  111. bool useFileName = GUILayout.Button("Use File Name", GUILayout.Width(100));
  112. if (useFileName)
  113. {
  114. fileName = AddressableHelper.GetFileNameIncludeI18n(AssetDatabase.GetAssetPath(asset));
  115. }
  116. }
  117. if (!string.IsNullOrEmpty(fileName))
  118. {
  119. if (onApply?.Invoke(new ApplyEvnetContext()
  120. {
  121. assetGUID = assetGUID,
  122. addressableName = fileName,
  123. fileSign = Sign,
  124. assetEntry = this
  125. }) ?? false)
  126. {
  127. addressableName = fileName;
  128. }
  129. }
  130. }
  131. //结束按钮的横板排序
  132. EditorGUILayout.EndHorizontal();
  133. }
  134. }
  135. }