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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
|
using UnityEngine;
using UnityEditor;
using System;
using System.Collections.Generic;
using Cinemachine.Utility;
using System.Reflection;
namespace Cinemachine.Editor
{
[CustomEditor(typeof(CinemachineVirtualCamera))]
internal class CinemachineVirtualCameraEditor
: CinemachineVirtualCameraBaseEditor<CinemachineVirtualCamera>
{
// Static state and caches - Call UpdateStaticData() to refresh this
struct StageData
{
string ExpandedKey { get { return "CNMCN_Core_Vcam_Expanded_" + Name; } }
public bool IsExpanded
{
get { return EditorPrefs.GetBool(ExpandedKey, false); }
set { EditorPrefs.SetBool(ExpandedKey, value); }
}
public string Name;
public Type[] types; // first entry is null
public GUIContent[] PopupOptions;
}
static StageData[] sStageData = null;
// Instance data - call UpdateInstanceData() to refresh this
int[] m_stageState = null;
bool[] m_stageError = null;
CinemachineComponentBase[] m_components;
UnityEditor.Editor[] m_componentEditors;
protected override void OnEnable()
{
// Build static menu arrays via reflection
base.OnEnable();
UpdateStaticData();
}
protected override void OnDisable()
{
base.OnDisable();
// Must destroy editors or we get exceptions
if (m_componentEditors != null)
foreach (UnityEditor.Editor e in m_componentEditors)
if (e != null)
UnityEngine.Object.DestroyImmediate(e);
}
Vector3 mPreviousPosition;
private void OnSceneGUI()
{
if (!Target.UserIsDragging)
mPreviousPosition = Target.transform.position;
if (Selection.Contains(Target.gameObject) && Tools.current == Tool.Move
&& Event.current.type == EventType.MouseDrag)
{
// User might be dragging our position handle
Target.UserIsDragging = true;
Vector3 delta = Target.transform.position - mPreviousPosition;
if (!delta.AlmostZero())
{
Undo.RegisterFullObjectHierarchyUndo(Target.gameObject, "Camera drag");
Target.OnPositionDragged(delta);
mPreviousPosition = Target.transform.position;
}
}
else if (GUIUtility.hotControl == 0 && Target.UserIsDragging)
{
// We're not dragging anything now, but we were
UnityEditorInternal.InternalEditorUtility.RepaintAllViews();
Target.UserIsDragging = false;
}
}
public override void OnInspectorGUI()
{
BeginInspector();
DrawHeaderInInspector();
DrawPropertyInInspector(FindProperty(x => x.m_Priority));
DrawTargetsInInspector(FindProperty(x => x.m_Follow), FindProperty(x => x.m_LookAt));
DrawRemainingPropertiesInInspector();
DrawPipelineInInspector();
DrawExtensionsWidgetInInspector();
}
protected void DrawPipelineInInspector()
{
UpdateInstanceData();
foreach (CinemachineCore.Stage stage in Enum.GetValues(typeof(CinemachineCore.Stage)))
{
int index = (int)stage;
// Skip pipeline stages that have no implementations
if (sStageData[index].PopupOptions.Length <= 1)
continue;
const float indentOffset = 6;
GUIStyle stageBoxStyle = GUI.skin.box;
EditorGUILayout.BeginVertical(stageBoxStyle);
Rect rect = EditorGUILayout.GetControlRect(true);
// Don't use PrefixLabel() because it will link the enabled status of field and label
GUIContent label = new GUIContent(NicifyName(stage.ToString()));
if (m_stageError[index])
label.image = EditorGUIUtility.IconContent("console.warnicon.sml").image;
float labelWidth = EditorGUIUtility.labelWidth - (indentOffset + EditorGUI.indentLevel * 15);
Rect r = rect; r.width = labelWidth;
EditorGUI.LabelField(r, label);
r = rect; r.width -= labelWidth; r.x += labelWidth;
GUI.enabled = !StageIsLocked(stage);
int newSelection = EditorGUI.Popup(r, m_stageState[index], sStageData[index].PopupOptions);
GUI.enabled = true;
Type type = sStageData[index].types[newSelection];
if (newSelection != m_stageState[index])
{
SetPipelineStage(stage, type);
if (newSelection != 0)
sStageData[index].IsExpanded = true;
UpdateInstanceData(); // because we changed it
return;
}
if (type != null)
{
Rect stageRect = new Rect(
rect.x - indentOffset, rect.y, rect.width + indentOffset, rect.height);
sStageData[index].IsExpanded = EditorGUI.Foldout(
stageRect, sStageData[index].IsExpanded, GUIContent.none);
if (sStageData[index].IsExpanded)
{
// Make the editor for that stage
UnityEditor.Editor e = GetEditorForPipelineStage(stage);
if (e != null)
{
++EditorGUI.indentLevel;
EditorGUILayout.Separator();
e.OnInspectorGUI();
EditorGUILayout.Separator();
--EditorGUI.indentLevel;
}
}
}
EditorGUILayout.EndVertical();
}
}
bool StageIsLocked(CinemachineCore.Stage stage)
{
CinemachineCore.Stage[] locked = Target.m_LockStageInInspector;
if (locked != null)
for (int i = 0; i < locked.Length; ++i)
if (locked[i] == stage)
return true;
return false;
}
UnityEditor.Editor GetEditorForPipelineStage(CinemachineCore.Stage stage)
{
foreach (UnityEditor.Editor e in m_componentEditors)
{
if (e != null)
{
CinemachineComponentBase c = e.target as CinemachineComponentBase;
if (c != null && c.Stage == stage)
return e;
}
}
return null;
}
/// <summary>
/// Register with CinemachineVirtualCamera to create the pipeline in an undo-friendly manner
/// </summary>
[InitializeOnLoad]
class CreatePipelineWithUndo
{
static CreatePipelineWithUndo()
{
CinemachineVirtualCamera.CreatePipelineOverride =
(CinemachineVirtualCamera vcam, string name, CinemachineComponentBase[] copyFrom) =>
{
// Create a new pipeline
GameObject go = new GameObject(name);
Undo.RegisterCreatedObjectUndo(go, "created pipeline");
Undo.SetTransformParent(go.transform, vcam.transform, "parenting pipeline");
Undo.AddComponent<CinemachinePipeline>(go);
// If copying, transfer the components
if (copyFrom != null)
{
foreach (Component c in copyFrom)
{
Component copy = Undo.AddComponent(go, c.GetType());
Undo.RecordObject(copy, "copying pipeline");
ReflectionHelpers.CopyFields(c, copy);
}
}
return go.transform;
};
CinemachineVirtualCamera.DestroyPipelineOverride = (GameObject pipeline) =>
{
Undo.DestroyObjectImmediate(pipeline);
};
}
}
void SetPipelineStage(CinemachineCore.Stage stage, Type type)
{
Undo.SetCurrentGroupName("Cinemachine pipeline change");
// Get the existing components
Transform owner = Target.GetComponentOwner();
CinemachineComponentBase[] components = owner.GetComponents<CinemachineComponentBase>();
if (components == null)
components = new CinemachineComponentBase[0];
// Find an appropriate insertion point
int numComponents = components.Length;
int insertPoint = 0;
for (insertPoint = 0; insertPoint < numComponents; ++insertPoint)
if (components[insertPoint].Stage >= stage)
break;
// Remove the existing components at that stage
for (int i = numComponents - 1; i >= 0; --i)
{
if (components[i].Stage == stage)
{
Undo.DestroyObjectImmediate(components[i]);
components[i] = null;
--numComponents;
if (i < insertPoint)
--insertPoint;
}
}
// Add the new stage
if (type != null)
{
MonoBehaviour b = Undo.AddComponent(owner.gameObject, type) as MonoBehaviour;
while (numComponents-- > insertPoint)
UnityEditorInternal.ComponentUtility.MoveComponentDown(b);
}
}
// This code dynamically discovers eligible classes and builds the menu
// data for the various component pipeline stages.
void UpdateStaticData()
{
if (sStageData != null)
return;
sStageData = new StageData[Enum.GetValues(typeof(CinemachineCore.Stage)).Length];
var stageTypes = new List<Type>[Enum.GetValues(typeof(CinemachineCore.Stage)).Length];
for (int i = 0; i < stageTypes.Length; ++i)
{
sStageData[i].Name = ((CinemachineCore.Stage)i).ToString();
stageTypes[i] = new List<Type>();
}
// Get all ICinemachineComponents
var allTypes
= ReflectionHelpers.GetTypesInAllLoadedAssemblies(
(Type t) => t.IsSubclassOf(typeof(CinemachineComponentBase)));
// Create a temp game object so we can instance behaviours
GameObject go = new GameObject("Cinemachine Temp Object");
go.hideFlags = HideFlags.DontSaveInBuild | HideFlags.DontSaveInEditor;
foreach (Type t in allTypes)
{
MonoBehaviour b = go.AddComponent(t) as MonoBehaviour;
CinemachineComponentBase c = b != null ? (CinemachineComponentBase)b : null;
if (c != null)
{
CinemachineCore.Stage stage = c.Stage;
stageTypes[(int)stage].Add(t);
}
}
GameObject.DestroyImmediate(go);
// Create the static lists
for (int i = 0; i < stageTypes.Length; ++i)
{
stageTypes[i].Insert(0, null); // first item is "none"
sStageData[i].types = stageTypes[i].ToArray();
GUIContent[] names = new GUIContent[sStageData[i].types.Length];
for (int n = 0; n < names.Length; ++n)
{
if (n == 0)
{
bool useSimple
= (i == (int)CinemachineCore.Stage.Aim)
|| (i == (int)CinemachineCore.Stage.Body);
names[n] = new GUIContent((useSimple) ? "Do nothing" : "none");
}
else
names[n] = new GUIContent(NicifyName(sStageData[i].types[n].Name));
}
sStageData[i].PopupOptions = names;
}
}
string NicifyName(string name)
{
if (name.StartsWith("Cinemachine"))
name = name.Substring(11); // Trim the prefix
return ObjectNames.NicifyVariableName(name);
}
void UpdateInstanceData()
{
// Invalidate the target's cache - this is to support Undo
Target.InvalidateComponentPipeline();
UpdateComponentEditors();
UpdateStageState(m_components);
}
// This code dynamically builds editors for the pipeline components.
// Expansion state is cached statically to preserve foldout state.
void UpdateComponentEditors()
{
CinemachineComponentBase[] components = Target.GetComponentPipeline();
int numComponents = components != null ? components.Length : 0;
if (m_components == null || m_components.Length != numComponents)
m_components = new CinemachineComponentBase[numComponents];
bool dirty = (numComponents == 0);
for (int i = 0; i < numComponents; ++i)
{
if (components[i] != m_components[i])
{
dirty = true;
m_components[i] = components[i];
}
}
if (dirty)
{
// Destroy the subeditors
if (m_componentEditors != null)
foreach (UnityEditor.Editor e in m_componentEditors)
if (e != null)
UnityEngine.Object.DestroyImmediate(e);
// Create new editors
m_componentEditors = new UnityEditor.Editor[numComponents];
for (int i = 0; i < numComponents; ++i)
{
MonoBehaviour b = components[i] as MonoBehaviour;
if (b != null)
CreateCachedEditor(b, null, ref m_componentEditors[i]);
}
}
}
void UpdateStageState(CinemachineComponentBase[] components)
{
m_stageState = new int[Enum.GetValues(typeof(CinemachineCore.Stage)).Length];
m_stageError = new bool[Enum.GetValues(typeof(CinemachineCore.Stage)).Length];
foreach (var c in components)
{
CinemachineCore.Stage stage = c.Stage;
int index = 0;
for (index = sStageData[(int)stage].types.Length - 1; index > 0; --index)
if (sStageData[(int)stage].types[index] == c.GetType())
break;
m_stageState[(int)stage] = index;
m_stageError[(int)stage] = !c.IsValid;
}
}
// Because the cinemachine components are attached to hidden objects, their
// gizmos don't get drawn by default. We have to do it explicitly.
[InitializeOnLoad]
static class CollectGizmoDrawers
{
static CollectGizmoDrawers()
{
m_GizmoDrawers = new Dictionary<Type, MethodInfo>();
Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
foreach (Assembly assembly in assemblies)
{
foreach (var type in assembly.GetTypes())
{
try
{
bool added = false;
foreach (var method in type.GetMethods(
BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static))
{
if (added)
break;
if (!method.IsStatic)
continue;
var attributes = method.GetCustomAttributes(typeof(DrawGizmo), true) as DrawGizmo[];
foreach (var a in attributes)
{
if (typeof(CinemachineComponentBase).IsAssignableFrom(a.drawnType))
{
m_GizmoDrawers.Add(a.drawnType, method);
added = true;
break;
}
}
}
}
catch (System.Exception)
{
// screw it
}
}
}
}
public static Dictionary<Type, MethodInfo> m_GizmoDrawers;
}
[DrawGizmo(GizmoType.Active | GizmoType.InSelectionHierarchy, typeof(CinemachineVirtualCamera))]
internal static void DrawVirtualCameraGizmos(CinemachineVirtualCamera vcam, GizmoType selectionType)
{
var pipeline = vcam.GetComponentPipeline();
foreach (var c in pipeline)
{
MethodInfo method;
if (CollectGizmoDrawers.m_GizmoDrawers.TryGetValue(c.GetType(), out method))
method.Invoke(null, new object[] { c, selectionType });
}
}
}
}
|