EmbeddedAssetHelpers.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. using UnityEngine;
  2. using UnityEditor;
  3. using UnityEditor.VersionControl;
  4. using System;
  5. namespace Cinemachine.Editor
  6. {
  7. /// <summary>
  8. /// Helper for drawing embedded asset editors
  9. /// </summary>
  10. internal class EmbeddeAssetEditor<T> where T : ScriptableObject
  11. {
  12. /// <summary>
  13. /// Create in OnEnable()
  14. /// </summary>
  15. public EmbeddeAssetEditor(string propertyName, UnityEditor.Editor owner)
  16. {
  17. m_PropertyName = propertyName;
  18. m_Owner = owner;
  19. m_CreateButtonGUIContent = new GUIContent(
  20. "Create Asset", "Create a new shared settings asset");
  21. }
  22. /// <summary>
  23. /// Called after the asset editor is created, in case it needs
  24. /// to be customized
  25. /// </summary>
  26. public OnCreateEditorDelegate OnCreateEditor;
  27. public delegate void OnCreateEditorDelegate(UnityEditor.Editor editor);
  28. /// <summary>
  29. /// Called when the asset being edited was changed by the user.
  30. /// </summary>
  31. public OnChangedDelegate OnChanged;
  32. public delegate void OnChangedDelegate(T obj);
  33. /// <summary>
  34. /// Free the resources in OnDisable()
  35. /// </summary>
  36. public void OnDisable()
  37. {
  38. DestroyEditor();
  39. m_Owner = null;
  40. }
  41. /// <summary>
  42. /// Customize this after creation if you want
  43. /// </summary>
  44. public GUIContent m_CreateButtonGUIContent;
  45. private string m_PropertyName;
  46. private UnityEditor.Editor m_Editor = null;
  47. private UnityEditor.Editor m_Owner = null;
  48. const int kIndentOffset = 3;
  49. /// <summary>
  50. /// Call this from OnInspectorGUI. Will draw the asset reference field, and
  51. /// the embedded editor, or a Create Asset button, if no asset is set.
  52. /// </summary>
  53. public void DrawEditorCombo(
  54. string title, string defaultName, string extension, string message,
  55. string showLabel, bool indent)
  56. {
  57. SerializedProperty property = m_Owner.serializedObject.FindProperty(m_PropertyName);
  58. if (m_Editor == null)
  59. UpdateEditor(property);
  60. if (m_Editor == null)
  61. AssetFieldWithCreateButton(property, title, defaultName, extension, message);
  62. else
  63. {
  64. EditorGUILayout.BeginVertical(GUI.skin.box);
  65. Rect rect = EditorGUILayout.GetControlRect(true);
  66. rect.height = EditorGUIUtility.singleLineHeight;
  67. EditorGUI.BeginChangeCheck();
  68. EditorGUI.PropertyField(rect, property);
  69. if (EditorGUI.EndChangeCheck())
  70. {
  71. m_Owner.serializedObject.ApplyModifiedProperties();
  72. UpdateEditor(property);
  73. }
  74. if (m_Editor != null)
  75. {
  76. Rect foldoutRect = new Rect(
  77. rect.x - kIndentOffset, rect.y, rect.width + kIndentOffset, rect.height);
  78. property.isExpanded = EditorGUI.Foldout(
  79. foldoutRect, property.isExpanded, GUIContent.none, true);
  80. bool canEditAsset = AssetDatabase.IsOpenForEdit(m_Editor.target, StatusQueryOptions.UseCachedIfPossible);
  81. GUI.enabled = canEditAsset;
  82. if (property.isExpanded)
  83. {
  84. EditorGUILayout.Separator();
  85. EditorGUILayout.HelpBox(
  86. "This is a shared asset. Changes made here will apply to all users of this asset.",
  87. MessageType.Info);
  88. EditorGUI.BeginChangeCheck();
  89. if (indent)
  90. ++EditorGUI.indentLevel;
  91. m_Editor.OnInspectorGUI();
  92. if (indent)
  93. --EditorGUI.indentLevel;
  94. if (EditorGUI.EndChangeCheck() && (OnChanged != null))
  95. OnChanged(property.objectReferenceValue as T);
  96. }
  97. GUI.enabled = true;
  98. if (m_Editor.target != null)
  99. {
  100. if (!canEditAsset && GUILayout.Button("Check out"))
  101. {
  102. Task task = Provider.Checkout(AssetDatabase.GetAssetPath(m_Editor.target), CheckoutMode.Asset);
  103. task.Wait();
  104. }
  105. }
  106. }
  107. EditorGUILayout.EndVertical();
  108. }
  109. }
  110. private void AssetFieldWithCreateButton(
  111. SerializedProperty property,
  112. string title, string defaultName, string extension, string message)
  113. {
  114. EditorGUI.BeginChangeCheck();
  115. float hSpace = 5;
  116. float buttonWidth = GUI.skin.button.CalcSize(m_CreateButtonGUIContent).x;
  117. Rect r = EditorGUILayout.GetControlRect(true);
  118. r.width -= buttonWidth + hSpace;
  119. EditorGUI.PropertyField(r, property);
  120. r.x += r.width + hSpace; r.width = buttonWidth;
  121. if (GUI.Button(r, m_CreateButtonGUIContent))
  122. {
  123. string newAssetPath = EditorUtility.SaveFilePanelInProject(
  124. title, defaultName, extension, message);
  125. if (!string.IsNullOrEmpty(newAssetPath))
  126. {
  127. T asset = ScriptableObjectUtility.CreateAt<T>(newAssetPath);
  128. property.objectReferenceValue = asset;
  129. m_Owner.serializedObject.ApplyModifiedProperties();
  130. }
  131. }
  132. if (EditorGUI.EndChangeCheck())
  133. {
  134. m_Owner.serializedObject.ApplyModifiedProperties();
  135. UpdateEditor(property);
  136. }
  137. }
  138. public void DestroyEditor()
  139. {
  140. if (m_Editor != null)
  141. {
  142. UnityEngine.Object.DestroyImmediate(m_Editor);
  143. m_Editor = null;
  144. }
  145. }
  146. public void UpdateEditor(SerializedProperty property)
  147. {
  148. var target = property.objectReferenceValue;
  149. if (m_Editor != null && m_Editor.target != target)
  150. DestroyEditor();
  151. if (target != null)
  152. {
  153. m_Editor = UnityEditor.Editor.CreateEditor(target);
  154. if (OnCreateEditor != null)
  155. OnCreateEditor(m_Editor);
  156. }
  157. }
  158. }
  159. }