EditorHelper.cs 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEditor;
  5. namespace YooAsset.Editor
  6. {
  7. public class EditorHelper
  8. {
  9. #if UNITY_2019_4_OR_NEWER
  10. private readonly static Dictionary<System.Type, string> _uxmlDic = new Dictionary<System.Type, string>();
  11. static EditorHelper()
  12. {
  13. // 资源包收集
  14. _uxmlDic.Add(typeof(AssetBundleCollectorWindow), "355c4ac5cdebddc4c8362bed6f17a79e");
  15. // 资源包构建
  16. _uxmlDic.Add(typeof(AssetBundleBuilderWindow), "28ba29adb4949284e8c48893218b0d9a");
  17. // 资源包调试
  18. _uxmlDic.Add(typeof(AssetBundleDebuggerWindow), "790db12999afd334e8fb6ba70ef0a947");
  19. _uxmlDic.Add(typeof(DebuggerAssetListViewer), "31c6096c1cb29b4469096b7b4942a322");
  20. _uxmlDic.Add(typeof(DebuggerBundleListViewer), "932a25ffd05c13c47994d66e9d73bc37");
  21. // 构建报告
  22. _uxmlDic.Add(typeof(AssetBundleReporterWindow), "9052b72c383e95043a0c7e7f369b1ad7");
  23. _uxmlDic.Add(typeof(ReporterSummaryViewer), "f8929271050855e42a1ccc6b14993a04");
  24. _uxmlDic.Add(typeof(ReporterAssetListViewer), "5f81bc15a55ee0a49a266f9d71e2372b");
  25. _uxmlDic.Add(typeof(ReporterBundleListViewer), "56d6dbe0d65ce334a8996beb19612989");
  26. }
  27. /// <summary>
  28. /// 加载窗口的布局文件
  29. /// </summary>
  30. public static UnityEngine.UIElements.VisualTreeAsset LoadWindowUXML<TWindow>() where TWindow : class
  31. {
  32. var windowType = typeof(TWindow);
  33. if (_uxmlDic.TryGetValue(windowType, out string uxmlGUID))
  34. {
  35. string assetPath = AssetDatabase.GUIDToAssetPath(uxmlGUID);
  36. if (string.IsNullOrEmpty(assetPath))
  37. throw new System.Exception($"Invalid YooAsset uxml guid : {uxmlGUID}");
  38. var visualTreeAsset = AssetDatabase.LoadAssetAtPath<UnityEngine.UIElements.VisualTreeAsset>(assetPath);
  39. if (visualTreeAsset == null)
  40. throw new System.Exception($"Failed to load {windowType}.uxml");
  41. return visualTreeAsset;
  42. }
  43. else
  44. {
  45. throw new System.Exception($"Invalid YooAsset window type : {windowType}");
  46. }
  47. }
  48. #endif
  49. /// <summary>
  50. /// 加载相关的配置文件
  51. /// </summary>
  52. public static TSetting LoadSettingData<TSetting>() where TSetting : ScriptableObject
  53. {
  54. var settingType = typeof(TSetting);
  55. var guids = AssetDatabase.FindAssets($"t:{settingType.Name}");
  56. if (guids.Length == 0)
  57. {
  58. Debug.LogWarning($"Create new {settingType.Name}.asset");
  59. var setting = ScriptableObject.CreateInstance<TSetting>();
  60. string filePath = $"Assets/{settingType.Name}.asset";
  61. AssetDatabase.CreateAsset(setting, filePath);
  62. AssetDatabase.SaveAssets();
  63. AssetDatabase.Refresh();
  64. return setting;
  65. }
  66. else
  67. {
  68. if (guids.Length != 1)
  69. {
  70. foreach (var guid in guids)
  71. {
  72. string path = AssetDatabase.GUIDToAssetPath(guid);
  73. Debug.LogWarning($"Found multiple file : {path}");
  74. }
  75. throw new System.Exception($"Found multiple {settingType.Name} files !");
  76. }
  77. string filePath = AssetDatabase.GUIDToAssetPath(guids[0]);
  78. var setting = AssetDatabase.LoadAssetAtPath<TSetting>(filePath);
  79. return setting;
  80. }
  81. }
  82. }
  83. }