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