ScriptableObjectUtility.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. using UnityEngine;
  2. using UnityEditor;
  3. using System.IO;
  4. using System;
  5. namespace Cinemachine.Editor
  6. {
  7. public class ScriptableObjectUtility : ScriptableObject
  8. {
  9. public static string kPackageRoot = "Packages/com.unity.cinemachine";
  10. /// <summary>Get the Cinemachine package install path. Works whether CM is
  11. /// a packman package or an ordinary asset.</summary>
  12. public static string CinemachineInstallPath
  13. {
  14. get
  15. {
  16. // First see if we're a UPM package - use some asset that we expect to find
  17. string path = Path.GetFullPath(kPackageRoot + "/Editor/EditorResources/cm_logo_sm.png");
  18. int index = path.LastIndexOf("/Editor");
  19. if (index < 0 || !File.Exists(path))
  20. {
  21. // Try as an ordinary asset
  22. ScriptableObject dummy = ScriptableObject.CreateInstance<ScriptableObjectUtility>();
  23. path = AssetDatabase.GetAssetPath(MonoScript.FromScriptableObject(dummy));
  24. if (path.Length > 0)
  25. path = Path.GetFullPath(path);
  26. DestroyImmediate(dummy);
  27. }
  28. path = path.Replace('\\', '/'); // because of GetFullPath()
  29. index = path.LastIndexOf("/Editor");
  30. if (index >= 0)
  31. path = path.Substring(0, index);
  32. if (path.Length > 0)
  33. path = Path.GetFullPath(path); // stupid backslashes
  34. return path;
  35. }
  36. }
  37. /// <summary>Get the Cinemachine package install path. Works whether CM is
  38. /// a packman package or an ordinary asset.</summary>
  39. public static string CinemachineRealativeInstallPath
  40. {
  41. get
  42. {
  43. ScriptableObject dummy = ScriptableObject.CreateInstance<ScriptableObjectUtility>();
  44. var path = AssetDatabase.GetAssetPath(MonoScript.FromScriptableObject(dummy));
  45. DestroyImmediate(dummy);
  46. var index = path.LastIndexOf("/Editor");
  47. if (index >= 0)
  48. path = path.Substring(0, index);
  49. return path;
  50. }
  51. }
  52. /// <summary>Create a scriptable object asset</summary>
  53. public static T CreateAt<T>(string assetPath) where T : ScriptableObject
  54. {
  55. return CreateAt(typeof(T), assetPath) as T;
  56. }
  57. /// <summary>Create a scriptable object asset</summary>
  58. public static ScriptableObject CreateAt(Type assetType, string assetPath)
  59. {
  60. ScriptableObject asset = ScriptableObject.CreateInstance(assetType);
  61. if (asset == null)
  62. {
  63. Debug.LogError("failed to create instance of " + assetType.Name + " at " + assetPath);
  64. return null;
  65. }
  66. AssetDatabase.CreateAsset(asset, assetPath);
  67. return asset;
  68. }
  69. public static void Create<T>(bool prependFolderName = false, bool trimName = true) where T : ScriptableObject
  70. {
  71. string className = typeof(T).Name;
  72. string assetName = className;
  73. string folder = GetSelectedAssetFolder();
  74. if (trimName)
  75. {
  76. string[] standardNames = new string[] { "Asset", "Attributes", "Container" };
  77. foreach (string standardName in standardNames)
  78. {
  79. assetName = assetName.Replace(standardName, "");
  80. }
  81. }
  82. if (prependFolderName)
  83. {
  84. string folderName = Path.GetFileName(folder);
  85. assetName = (string.IsNullOrEmpty(assetName) ? folderName : string.Format("{0}_{1}", folderName, assetName));
  86. }
  87. Create(className, assetName, folder);
  88. }
  89. private static ScriptableObject Create(string className, string assetName, string folder)
  90. {
  91. ScriptableObject asset = ScriptableObject.CreateInstance(className);
  92. if (asset == null)
  93. {
  94. Debug.LogError("failed to create instance of " + className);
  95. return null;
  96. }
  97. asset.name = assetName ?? className;
  98. string assetPath = GetUnusedAssetPath(folder, asset.name);
  99. AssetDatabase.CreateAsset(asset, assetPath);
  100. return asset;
  101. }
  102. private static string GetSelectedAssetFolder()
  103. {
  104. if ((Selection.activeObject != null) && AssetDatabase.Contains(Selection.activeObject))
  105. {
  106. string assetPath = AssetDatabase.GetAssetPath(Selection.activeObject);
  107. string assetPathAbsolute = string.Format("{0}/{1}", Path.GetDirectoryName(Application.dataPath), assetPath);
  108. if (Directory.Exists(assetPathAbsolute))
  109. {
  110. return assetPath;
  111. }
  112. else
  113. {
  114. return Path.GetDirectoryName(assetPath);
  115. }
  116. }
  117. return "Assets";
  118. }
  119. private static string GetUnusedAssetPath(string folder, string assetName)
  120. {
  121. for (int n = 0; n < 9999; n++)
  122. {
  123. string assetPath = string.Format("{0}/{1}{2}.asset", folder, assetName, (n == 0 ? "" : n.ToString()));
  124. string existingGUID = AssetDatabase.AssetPathToGUID(assetPath);
  125. if (string.IsNullOrEmpty(existingGUID))
  126. {
  127. return assetPath;
  128. }
  129. }
  130. return null;
  131. }
  132. }
  133. }