ScriptableLoader.cs 1.0 KB

12345678910111213141516171819202122232425262728293031323334
  1. using UnityEditor;
  2. using UnityEngine;
  3. namespace ET.PackageManager.Editor
  4. {
  5. public static class ScriptableLoader
  6. {
  7. public static T Load<T>() where T : ScriptableObject
  8. {
  9. var settingType = typeof(T);
  10. var guids = AssetDatabase.FindAssets($"t:{settingType.Name}");
  11. if (guids.Length == 0)
  12. {
  13. Debug.LogError($"没有这个文件 : {settingType.Name}");
  14. return null;
  15. }
  16. else
  17. {
  18. if (guids.Length != 1)
  19. {
  20. foreach (var guid in guids)
  21. {
  22. string path = AssetDatabase.GUIDToAssetPath(guid);
  23. Debug.LogError($"找到多个文件 : {path}");
  24. }
  25. }
  26. string filePath = AssetDatabase.GUIDToAssetPath(guids[0]);
  27. var asset = AssetDatabase.LoadAssetAtPath<T>(filePath);
  28. return asset;
  29. }
  30. }
  31. }
  32. }