EmbeddedAssetPropertyDrawer.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. type = type.GetField(a[i],
  138. System.Reflection.BindingFlags.Public
  139. | System.Reflection.BindingFlags.NonPublic
  140. | System.Reflection.BindingFlags.Instance).FieldType;
  141. return type;
  142. }
  143. Type[] mAssetTypes = null;
  144. List<ScriptableObject> mAssetPresets;
  145. GUIContent[] mAssetPresetNames;
  146. void RebuildPresetList()
  147. {
  148. if (mAssetPresets != null && mAssetPresetNames != null)
  149. return;
  150. mAssetPresets = new List<ScriptableObject>();
  151. #if UNITY_2018_1_OR_NEWER
  152. if (mAssetTypes != null)
  153. {
  154. for (int i = 0; i < mAssetTypes.Length; ++i)
  155. InspectorUtility.AddAssetsFromPackageSubDirectory(
  156. mAssetTypes[i], mAssetPresets, "Presets/Noise");
  157. }
  158. #endif
  159. List<GUIContent> presetNameList = new List<GUIContent>();
  160. foreach (var n in mAssetPresets)
  161. presetNameList.Add(new GUIContent("Presets/" + n.name));
  162. mAssetPresetNames = presetNameList.ToArray();
  163. }
  164. void AssetFieldWithCreateButton(
  165. Rect r, SerializedProperty property, bool warnIfNull, string defaultName)
  166. {
  167. // Collect all the eligible asset types
  168. Type type = EmbeddedAssetType(property);
  169. if (mAssetTypes == null)
  170. mAssetTypes = ReflectionHelpers.GetTypesInAllDependentAssemblies(
  171. (Type t) => type.IsAssignableFrom(t) && !t.IsAbstract).ToArray();
  172. float iconSize = r.height + 4;
  173. r.width -= iconSize;
  174. GUIContent label = new GUIContent(property.displayName, property.tooltip);
  175. if (warnIfNull && property.objectReferenceValue == null)
  176. label.image = EditorGUIUtility.IconContent("console.warnicon.sml").image;
  177. EditorGUI.PropertyField(r, property, label);
  178. r.x += r.width; r.width = iconSize; r.height = iconSize;
  179. if (GUI.Button(r, EditorGUIUtility.IconContent("_Popup"), GUI.skin.label))
  180. {
  181. GenericMenu menu = new GenericMenu();
  182. if (property.objectReferenceValue != null)
  183. {
  184. menu.AddItem(new GUIContent("Edit"), false, ()
  185. => Selection.activeObject = property.objectReferenceValue);
  186. menu.AddItem(new GUIContent("Clone"), false, () =>
  187. {
  188. ScriptableObject copyFrom = property.objectReferenceValue as ScriptableObject;
  189. if (copyFrom != null)
  190. {
  191. string title = "Create New " + copyFrom.GetType().Name + " asset";
  192. ScriptableObject asset = CreateAsset(
  193. copyFrom.GetType(), copyFrom, defaultName, title);
  194. if (asset != null)
  195. {
  196. property.objectReferenceValue = asset;
  197. property.serializedObject.ApplyModifiedProperties();
  198. }
  199. }
  200. });
  201. menu.AddItem(new GUIContent("Locate"), false, ()
  202. => EditorGUIUtility.PingObject(property.objectReferenceValue));
  203. }
  204. RebuildPresetList();
  205. int i = 0;
  206. foreach (var a in mAssetPresets)
  207. {
  208. menu.AddItem(mAssetPresetNames[i++], false, () =>
  209. {
  210. property.objectReferenceValue = a;
  211. property.serializedObject.ApplyModifiedProperties();
  212. });
  213. }
  214. foreach (var t in mAssetTypes)
  215. {
  216. menu.AddItem(new GUIContent("New " + InspectorUtility.NicifyClassName(t.Name)), false, () =>
  217. {
  218. string title = "Create New " + t.Name + " asset";
  219. ScriptableObject asset = CreateAsset(t, null, defaultName, title);
  220. if (asset != null)
  221. {
  222. property.objectReferenceValue = asset;
  223. property.serializedObject.ApplyModifiedProperties();
  224. }
  225. });
  226. }
  227. menu.ShowAsContext();
  228. }
  229. }
  230. ScriptableObject CreateAsset(
  231. Type assetType, ScriptableObject copyFrom, string defaultName, string dialogTitle)
  232. {
  233. ScriptableObject asset = null;
  234. string path = EditorUtility.SaveFilePanelInProject(
  235. dialogTitle, defaultName, "asset", string.Empty);
  236. if (!string.IsNullOrEmpty(path))
  237. {
  238. if (copyFrom != null)
  239. {
  240. string fromPath = AssetDatabase.GetAssetPath(copyFrom);
  241. if (AssetDatabase.CopyAsset(fromPath, path))
  242. asset = AssetDatabase.LoadAssetAtPath(path, assetType) as ScriptableObject;
  243. }
  244. else
  245. {
  246. asset = ScriptableObjectUtility.CreateAt(assetType, path);
  247. }
  248. AssetDatabase.SaveAssets();
  249. AssetDatabase.Refresh();
  250. }
  251. return asset;
  252. }
  253. }
  254. }