EmbeddedAssetPropertyDrawer.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. using UnityEngine;
  2. using UnityEditor;
  3. using System;
  4. using Cinemachine.Utility;
  5. using System.Linq;
  6. using System.Collections.Generic;
  7. namespace Cinemachine.Editor
  8. {
  9. [CustomPropertyDrawer(typeof(CinemachineEmbeddedAssetPropertyAttribute))]
  10. internal sealed class EmbeddedAssetPropertyDrawer : PropertyDrawer
  11. {
  12. const float vSpace = 2;
  13. const float kIndentAmount = 15;
  14. const float kBoxMargin = 3;
  15. float HelpBoxHeight { get { return EditorGUIUtility.singleLineHeight * 2.5f; } }
  16. bool mExpanded = false;
  17. bool WarnIfNull
  18. {
  19. get
  20. {
  21. var attr = attribute as CinemachineEmbeddedAssetPropertyAttribute;
  22. return attr == null ? false : attr.WarnIfNull;
  23. }
  24. }
  25. float HeaderHeight { get { return EditorGUIUtility.singleLineHeight * 1.5f; } }
  26. float DrawHeader(Rect rect, string text)
  27. {
  28. float delta = HeaderHeight - EditorGUIUtility.singleLineHeight;
  29. rect.y += delta; rect.height -= delta;
  30. EditorGUI.LabelField(rect, new GUIContent(text), EditorStyles.boldLabel);
  31. return HeaderHeight;
  32. }
  33. string HeaderText(SerializedProperty property)
  34. {
  35. var attrs = property.serializedObject.targetObject.GetType()
  36. .GetCustomAttributes(typeof(HeaderAttribute), false);
  37. if (attrs != null && attrs.Length > 0)
  38. return ((HeaderAttribute)attrs[0]).header;
  39. return null;
  40. }
  41. bool AssetHasCustomEditor(SerializedProperty property)
  42. {
  43. ScriptableObject asset = property.objectReferenceValue as ScriptableObject;
  44. if (asset != null)
  45. return ActiveEditorTracker.HasCustomEditor(asset);
  46. return false;
  47. }
  48. public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
  49. {
  50. bool hasCustomEditor = AssetHasCustomEditor(property);
  51. float height = base.GetPropertyHeight(property, label);
  52. height += + 2 * (kBoxMargin + vSpace);
  53. if (mExpanded && !hasCustomEditor)
  54. {
  55. height += HelpBoxHeight + kBoxMargin;
  56. ScriptableObject asset = property.objectReferenceValue as ScriptableObject;
  57. if (asset != null)
  58. {
  59. SerializedObject so = new SerializedObject(asset);
  60. var prop = so.GetIterator();
  61. prop.NextVisible(true);
  62. do
  63. {
  64. if (prop.name == "m_Script")
  65. continue;
  66. string header = HeaderText(prop);
  67. if (header != null)
  68. height += HeaderHeight + vSpace;
  69. height += EditorGUI.GetPropertyHeight(prop, false) + vSpace;
  70. }
  71. while (prop.NextVisible(prop.isExpanded));
  72. height += kBoxMargin;
  73. }
  74. }
  75. return height;
  76. }
  77. public override void OnGUI(Rect rect, SerializedProperty property, GUIContent label)
  78. {
  79. bool hasCustomEditor = AssetHasCustomEditor(property);
  80. rect.y += vSpace; rect.height -= 2 * vSpace;
  81. GUI.Box(rect, GUIContent.none, GUI.skin.box);
  82. rect.y += kBoxMargin;
  83. rect.height = EditorGUIUtility.singleLineHeight;
  84. EditorGUIUtility.labelWidth -= kBoxMargin;
  85. AssetFieldWithCreateButton(
  86. new Rect(rect.x + kBoxMargin, rect.y, rect.width - 2 * kBoxMargin, rect.height),
  87. property, WarnIfNull,
  88. property.serializedObject.targetObject.name + " " + label.text);
  89. ScriptableObject asset = property.objectReferenceValue as ScriptableObject;
  90. if (asset != null && !hasCustomEditor)
  91. {
  92. mExpanded = EditorGUI.Foldout(rect, mExpanded, GUIContent.none, true);
  93. if (mExpanded)
  94. {
  95. rect.y += rect.height + kBoxMargin + vSpace;
  96. rect.x += kIndentAmount + kBoxMargin;
  97. rect.width -= kIndentAmount + 2 * kBoxMargin;
  98. EditorGUIUtility.labelWidth -= kIndentAmount;
  99. EditorGUI.HelpBox(
  100. new Rect(rect.x, rect.y, rect.width, HelpBoxHeight),
  101. "This is a shared asset.\n"
  102. + "Changes made here will apply to all users of this asset",
  103. MessageType.Info);
  104. rect.y += HelpBoxHeight + kBoxMargin;
  105. SerializedObject so = new SerializedObject(property.objectReferenceValue);
  106. var prop = so.GetIterator();
  107. prop.NextVisible(true);
  108. var indent = EditorGUI.indentLevel;
  109. do
  110. {
  111. if (prop.name == "m_Script")
  112. continue;
  113. string header = HeaderText(prop);
  114. if (header != null)
  115. {
  116. DrawHeader(rect, header);
  117. rect.y += HeaderHeight + vSpace;
  118. rect.height -= HeaderHeight + vSpace;
  119. }
  120. rect.height = EditorGUI.GetPropertyHeight(prop, false);
  121. EditorGUI.indentLevel = indent + prop.depth;
  122. EditorGUI.PropertyField(rect, prop);
  123. rect.y += rect.height + vSpace;
  124. } while (prop.NextVisible(prop.isExpanded));
  125. if (GUI.changed)
  126. so.ApplyModifiedProperties();
  127. }
  128. EditorGUIUtility.labelWidth += kIndentAmount;
  129. }
  130. EditorGUIUtility.labelWidth += kBoxMargin;
  131. }
  132. Type EmbeddedAssetType(SerializedProperty property)
  133. {
  134. Type type = property.serializedObject.targetObject.GetType();
  135. var a = property.propertyPath.Split('.');
  136. for (int i = 0; i < a.Length; ++i)
  137. {
  138. var field = type.GetField(a[i],
  139. System.Reflection.BindingFlags.Public
  140. | System.Reflection.BindingFlags.NonPublic
  141. | System.Reflection.BindingFlags.Instance);
  142. if (field == null) continue;
  143. type = field.FieldType;
  144. }
  145. return type;
  146. }
  147. Type[] mAssetTypes = null;
  148. List<ScriptableObject> mAssetPresets;
  149. GUIContent[] mAssetPresetNames;
  150. void RebuildPresetList()
  151. {
  152. if (mAssetPresets != null && mAssetPresetNames != null)
  153. return;
  154. mAssetPresets = new List<ScriptableObject>();
  155. if (mAssetTypes != null)
  156. {
  157. for (int i = 0; i < mAssetTypes.Length; ++i)
  158. InspectorUtility.AddAssetsFromPackageSubDirectory(
  159. mAssetTypes[i], mAssetPresets, "Presets/Noise");
  160. }
  161. List<GUIContent> presetNameList = new List<GUIContent>();
  162. foreach (var n in mAssetPresets)
  163. presetNameList.Add(new GUIContent("Presets/" + n.name));
  164. mAssetPresetNames = presetNameList.ToArray();
  165. }
  166. void AssetFieldWithCreateButton(
  167. Rect r, SerializedProperty property, bool warnIfNull, string defaultName)
  168. {
  169. // Collect all the eligible asset types
  170. Type type = EmbeddedAssetType(property);
  171. if (mAssetTypes == null)
  172. mAssetTypes = ReflectionHelpers.GetTypesInAllDependentAssemblies(
  173. (Type t) => type.IsAssignableFrom(t) && !t.IsAbstract).ToArray();
  174. float iconSize = r.height + 4;
  175. r.width -= iconSize;
  176. GUIContent label = new GUIContent(property.displayName, property.tooltip);
  177. if (warnIfNull && property.objectReferenceValue == null)
  178. label.image = EditorGUIUtility.IconContent("console.warnicon.sml").image;
  179. EditorGUI.PropertyField(r, property, label);
  180. r.x += r.width; r.width = iconSize; r.height = iconSize;
  181. if (GUI.Button(r, EditorGUIUtility.IconContent("_Popup"), GUI.skin.label))
  182. {
  183. GenericMenu menu = new GenericMenu();
  184. if (property.objectReferenceValue != null)
  185. {
  186. menu.AddItem(new GUIContent("Edit"), false, ()
  187. => Selection.activeObject = property.objectReferenceValue);
  188. menu.AddItem(new GUIContent("Clone"), false, () =>
  189. {
  190. ScriptableObject copyFrom = property.objectReferenceValue as ScriptableObject;
  191. if (copyFrom != null)
  192. {
  193. string title = "Create New " + copyFrom.GetType().Name + " asset";
  194. ScriptableObject asset = CreateAsset(
  195. copyFrom.GetType(), copyFrom, defaultName, title);
  196. if (asset != null)
  197. {
  198. property.objectReferenceValue = asset;
  199. property.serializedObject.ApplyModifiedProperties();
  200. }
  201. }
  202. });
  203. menu.AddItem(new GUIContent("Locate"), false, ()
  204. => EditorGUIUtility.PingObject(property.objectReferenceValue));
  205. }
  206. RebuildPresetList();
  207. int i = 0;
  208. foreach (var a in mAssetPresets)
  209. {
  210. menu.AddItem(mAssetPresetNames[i++], false, () =>
  211. {
  212. property.objectReferenceValue = a;
  213. property.serializedObject.ApplyModifiedProperties();
  214. });
  215. }
  216. foreach (var t in mAssetTypes)
  217. {
  218. menu.AddItem(new GUIContent("New " + InspectorUtility.NicifyClassName(t.Name)), false, () =>
  219. {
  220. string title = "Create New " + t.Name + " asset";
  221. ScriptableObject asset = CreateAsset(t, null, defaultName, title);
  222. if (asset != null)
  223. {
  224. property.objectReferenceValue = asset;
  225. property.serializedObject.ApplyModifiedProperties();
  226. }
  227. });
  228. }
  229. menu.ShowAsContext();
  230. }
  231. }
  232. ScriptableObject CreateAsset(
  233. Type assetType, ScriptableObject copyFrom, string defaultName, string dialogTitle)
  234. {
  235. ScriptableObject asset = null;
  236. string path = EditorUtility.SaveFilePanelInProject(
  237. dialogTitle, defaultName, "asset", string.Empty);
  238. if (!string.IsNullOrEmpty(path))
  239. {
  240. if (copyFrom != null)
  241. {
  242. string fromPath = AssetDatabase.GetAssetPath(copyFrom);
  243. if (AssetDatabase.CopyAsset(fromPath, path))
  244. asset = AssetDatabase.LoadAssetAtPath(path, assetType) as ScriptableObject;
  245. }
  246. else
  247. {
  248. asset = ScriptableObjectUtility.CreateAt(assetType, path);
  249. }
  250. AssetDatabase.SaveAssets();
  251. AssetDatabase.Refresh();
  252. }
  253. return asset;
  254. }
  255. }
  256. }