AssetUtil.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #if UNITY_EDITOR
  2. using UnityEditor;
  3. #endif
  4. namespace SRF.Helpers
  5. {
  6. using System.IO;
  7. using UnityEngine;
  8. public static class AssetUtil
  9. {
  10. #if UNITY_EDITOR
  11. // This makes it easy to create, name and place unique new ScriptableObject asset files.
  12. /// <summary>
  13. /// </summary>
  14. public static T CreateAsset<T>() where T : ScriptableObject
  15. {
  16. var path = AssetDatabase.GetAssetPath(Selection.activeObject);
  17. if (path == "")
  18. {
  19. path = "Assets";
  20. }
  21. else if (Path.GetExtension(path) != "")
  22. {
  23. path = path.Replace(Path.GetFileName(path), "");
  24. }
  25. return CreateAsset<T>(path, "New " + typeof (T).Name);
  26. }
  27. public static T CreateAsset<T>(string path, string name) where T : ScriptableObject
  28. {
  29. if (string.IsNullOrEmpty(path))
  30. {
  31. path = "Assets";
  32. }
  33. if (!name.EndsWith(".asset"))
  34. {
  35. name += ".asset";
  36. }
  37. var assetPathAndName = AssetDatabase.GenerateUniqueAssetPath(path + "/" + name);
  38. var asset = ScriptableObject.CreateInstance<T>();
  39. AssetDatabase.CreateAsset(asset, assetPathAndName);
  40. AssetDatabase.SaveAssets();
  41. return asset;
  42. }
  43. public static void SelectAssetInProjectView(Object asset)
  44. {
  45. EditorUtility.FocusProjectWindow();
  46. Selection.activeObject = asset;
  47. }
  48. #endif
  49. }
  50. }