PostProcessingInspector.cs
6.81 KB
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
using UnityEngine;
using UnityEngine.PostProcessing;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
namespace UnityEditor.PostProcessing
{
//[CanEditMultipleObjects]
[CustomEditor(typeof(PostProcessingProfile))]
public class PostProcessingInspector : Editor
{
static GUIContent s_PreviewTitle = new GUIContent("Monitors");
PostProcessingProfile m_ConcreteTarget
{
get { return target as PostProcessingProfile; }
}
int m_CurrentMonitorID
{
get { return m_ConcreteTarget.monitors.currentMonitorID; }
set { m_ConcreteTarget.monitors.currentMonitorID = value; }
}
List<PostProcessingMonitor> m_Monitors;
GUIContent[] m_MonitorNames;
Dictionary<PostProcessingModelEditor, PostProcessingModel> m_CustomEditors = new Dictionary<PostProcessingModelEditor, PostProcessingModel>();
public bool IsInteractivePreviewOpened { get; private set; }
void OnEnable()
{
if (target == null)
return;
// Aggregate custom post-fx editors
var assembly = Assembly.GetAssembly(typeof(PostProcessingInspector));
var editorTypes = assembly.GetTypes()
.Where(x => x.IsDefined(typeof(PostProcessingModelEditorAttribute), false));
var customEditors = new Dictionary<Type, PostProcessingModelEditor>();
foreach (var editor in editorTypes)
{
var attr = (PostProcessingModelEditorAttribute)editor.GetCustomAttributes(typeof(PostProcessingModelEditorAttribute), false)[0];
var effectType = attr.type;
var alwaysEnabled = attr.alwaysEnabled;
var editorInst = (PostProcessingModelEditor)Activator.CreateInstance(editor);
editorInst.alwaysEnabled = alwaysEnabled;
editorInst.profile = target as PostProcessingProfile;
editorInst.inspector = this;
customEditors.Add(effectType, editorInst);
}
// ... and corresponding models
var baseType = target.GetType();
var property = serializedObject.GetIterator();
while (property.Next(true))
{
if (!property.hasChildren)
continue;
var type = baseType;
var srcObject = ReflectionUtils.GetFieldValueFromPath(serializedObject.targetObject, ref type, property.propertyPath);
if (srcObject == null)
continue;
PostProcessingModelEditor editor;
if (customEditors.TryGetValue(type, out editor))
{
var effect = (PostProcessingModel)srcObject;
if (editor.alwaysEnabled)
effect.enabled = editor.alwaysEnabled;
m_CustomEditors.Add(editor, effect);
editor.target = effect;
editor.serializedProperty = property.Copy();
editor.OnPreEnable();
}
}
// Prepare monitors
m_Monitors = new List<PostProcessingMonitor>();
var monitors = new List<PostProcessingMonitor>
{
new HistogramMonitor(),
new WaveformMonitor(),
new ParadeMonitor(),
new VectorscopeMonitor()
};
var monitorNames = new List<GUIContent>();
foreach (var monitor in monitors)
{
if (monitor.IsSupported())
{
monitor.Init(m_ConcreteTarget.monitors, this);
m_Monitors.Add(monitor);
monitorNames.Add(monitor.GetMonitorTitle());
}
}
m_MonitorNames = monitorNames.ToArray();
if (m_Monitors.Count > 0)
m_ConcreteTarget.monitors.onFrameEndEditorOnly = OnFrameEnd;
}
void OnDisable()
{
if (m_CustomEditors != null)
{
foreach (var editor in m_CustomEditors.Keys)
editor.OnDisable();
m_CustomEditors.Clear();
}
if (m_Monitors != null)
{
foreach (var monitor in m_Monitors)
monitor.Dispose();
m_Monitors.Clear();
}
if (m_ConcreteTarget != null)
m_ConcreteTarget.monitors.onFrameEndEditorOnly = null;
}
void OnFrameEnd(RenderTexture source)
{
if (!IsInteractivePreviewOpened)
return;
if (m_CurrentMonitorID < m_Monitors.Count)
m_Monitors[m_CurrentMonitorID].OnFrameData(source);
IsInteractivePreviewOpened = false;
}
public override void OnInspectorGUI()
{
serializedObject.Update();
// Handles undo/redo events first (before they get used by the editors' widgets)
var e = Event.current;
if (e.type == EventType.ValidateCommand && e.commandName == "UndoRedoPerformed")
{
foreach (var editor in m_CustomEditors)
editor.Value.OnValidate();
}
if (!m_ConcreteTarget.debugViews.IsModeActive(BuiltinDebugViewsModel.Mode.None))
EditorGUILayout.HelpBox("A debug view is currently enabled. Changes done to an effect might not be visible.", MessageType.Info);
foreach (var editor in m_CustomEditors)
{
EditorGUI.BeginChangeCheck();
editor.Key.OnGUI();
if (EditorGUI.EndChangeCheck())
editor.Value.OnValidate();
}
serializedObject.ApplyModifiedProperties();
}
public override GUIContent GetPreviewTitle()
{
return s_PreviewTitle;
}
public override bool HasPreviewGUI()
{
return GraphicsUtils.supportsDX11 && m_Monitors.Count > 0;
}
public override void OnPreviewSettings()
{
using (new EditorGUILayout.HorizontalScope())
{
if (m_CurrentMonitorID < m_Monitors.Count)
m_Monitors[m_CurrentMonitorID].OnMonitorSettings();
GUILayout.Space(5);
m_CurrentMonitorID = EditorGUILayout.Popup(m_CurrentMonitorID, m_MonitorNames, FxStyles.preDropdown, GUILayout.MaxWidth(100f));
}
}
public override void OnInteractivePreviewGUI(Rect r, GUIStyle background)
{
IsInteractivePreviewOpened = true;
if (m_CurrentMonitorID < m_Monitors.Count)
m_Monitors[m_CurrentMonitorID].OnMonitorGUI(r);
}
}
}