ScriptableObjectUtility.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. using UnityEngine;
  2. using UnityEditor;
  3. using System.IO;
  4. using System;
  5. namespace Cinemachine.Editor
  6. {
  7. /// <summary>
  8. /// This is a collection of utilities surrounding ScriptableObjects
  9. /// </summary>
  10. public class ScriptableObjectUtility : ScriptableObject
  11. {
  12. /// <summary>
  13. /// The default relative path to the root directory where Cinemachine is installed
  14. /// </summary>
  15. public static string kPackageRoot = "Packages/com.unity.cinemachine";
  16. /// <summary>Get the Cinemachine package install path.</summary>
  17. public static string CinemachineInstallPath
  18. {
  19. get
  20. {
  21. string path = Path.GetFullPath(kPackageRoot);
  22. path = path.Replace('\\', '/'); // because of GetFullPath()
  23. return path;
  24. }
  25. }
  26. /// <summary>Get the relative Cinemachine package install path.</summary>
  27. public static string CinemachineRealativeInstallPath
  28. {
  29. get
  30. {
  31. return kPackageRoot;
  32. }
  33. }
  34. /// <summary>Create a scriptable object asset</summary>
  35. /// <typeparam name="T">The type of asset to create</typeparam>
  36. /// <param name="assetPath">The full path and filename of the asset to create</param>
  37. /// <returns>The newly-created asset</returns>
  38. public static T CreateAt<T>(string assetPath) where T : ScriptableObject
  39. {
  40. return CreateAt(typeof(T), assetPath) as T;
  41. }
  42. /// <summary>Create a scriptable object asset</summary>
  43. /// <param name="assetType">The type of asset to create</param>
  44. /// <param name="assetPath">The full path and filename of the asset to create</param>
  45. /// <returns>The newly-created asset</returns>
  46. public static ScriptableObject CreateAt(Type assetType, string assetPath)
  47. {
  48. ScriptableObject asset = ScriptableObject.CreateInstance(assetType);
  49. if (asset == null)
  50. {
  51. Debug.LogError("failed to create instance of " + assetType.Name + " at " + assetPath);
  52. return null;
  53. }
  54. AssetDatabase.CreateAsset(asset, assetPath);
  55. return asset;
  56. }
  57. /// <summary>Create a ScriptableObject asset</summary>
  58. /// <typeparam name="T">The type of asset to create</typeparam>
  59. /// <param name="prependFolderName">If true, prepend the selected asset folder name to the asset name</param>
  60. /// <param name="trimName">If true, remove instances of the "Asset", "Attributes", "Container" strings from the name</param>
  61. public static void Create<T>(bool prependFolderName = false, bool trimName = true) where T : ScriptableObject
  62. {
  63. string className = typeof(T).Name;
  64. string assetName = className;
  65. string folder = GetSelectedAssetFolder();
  66. if (trimName)
  67. {
  68. string[] standardNames = new string[] { "Asset", "Attributes", "Container" };
  69. foreach (string standardName in standardNames)
  70. {
  71. assetName = assetName.Replace(standardName, "");
  72. }
  73. }
  74. if (prependFolderName)
  75. {
  76. string folderName = Path.GetFileName(folder);
  77. assetName = (string.IsNullOrEmpty(assetName) ? folderName : string.Format("{0}_{1}", folderName, assetName));
  78. }
  79. Create(className, assetName, folder);
  80. }
  81. private static ScriptableObject Create(string className, string assetName, string folder)
  82. {
  83. ScriptableObject asset = ScriptableObject.CreateInstance(className);
  84. if (asset == null)
  85. {
  86. Debug.LogError("failed to create instance of " + className);
  87. return null;
  88. }
  89. asset.name = assetName ?? className;
  90. string assetPath = GetUnusedAssetPath(folder, asset.name);
  91. AssetDatabase.CreateAsset(asset, assetPath);
  92. return asset;
  93. }
  94. private static string GetSelectedAssetFolder()
  95. {
  96. if ((Selection.activeObject != null) && AssetDatabase.Contains(Selection.activeObject))
  97. {
  98. string assetPath = AssetDatabase.GetAssetPath(Selection.activeObject);
  99. string assetPathAbsolute = string.Format("{0}/{1}", Path.GetDirectoryName(Application.dataPath), assetPath);
  100. if (Directory.Exists(assetPathAbsolute))
  101. {
  102. return assetPath;
  103. }
  104. else
  105. {
  106. return Path.GetDirectoryName(assetPath);
  107. }
  108. }
  109. return "Assets";
  110. }
  111. private static string GetUnusedAssetPath(string folder, string assetName)
  112. {
  113. for (int n = 0; n < 9999; n++)
  114. {
  115. string assetPath = string.Format("{0}/{1}{2}.asset", folder, assetName, (n == 0 ? "" : n.ToString()));
  116. string existingGUID = AssetDatabase.AssetPathToGUID(assetPath);
  117. if (string.IsNullOrEmpty(existingGUID))
  118. {
  119. return assetPath;
  120. }
  121. }
  122. return null;
  123. }
  124. }
  125. }