You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

208 lines
6.8 KiB

  1. using UnityEngine;
  2. using UnityEngine.PostProcessing;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Reflection;
  7. namespace UnityEditor.PostProcessing
  8. {
  9. //[CanEditMultipleObjects]
  10. [CustomEditor(typeof(PostProcessingProfile))]
  11. public class PostProcessingInspector : Editor
  12. {
  13. static GUIContent s_PreviewTitle = new GUIContent("Monitors");
  14. PostProcessingProfile m_ConcreteTarget
  15. {
  16. get { return target as PostProcessingProfile; }
  17. }
  18. int m_CurrentMonitorID
  19. {
  20. get { return m_ConcreteTarget.monitors.currentMonitorID; }
  21. set { m_ConcreteTarget.monitors.currentMonitorID = value; }
  22. }
  23. List<PostProcessingMonitor> m_Monitors;
  24. GUIContent[] m_MonitorNames;
  25. Dictionary<PostProcessingModelEditor, PostProcessingModel> m_CustomEditors = new Dictionary<PostProcessingModelEditor, PostProcessingModel>();
  26. public bool IsInteractivePreviewOpened { get; private set; }
  27. void OnEnable()
  28. {
  29. if (target == null)
  30. return;
  31. // Aggregate custom post-fx editors
  32. var assembly = Assembly.GetAssembly(typeof(PostProcessingInspector));
  33. var editorTypes = assembly.GetTypes()
  34. .Where(x => x.IsDefined(typeof(PostProcessingModelEditorAttribute), false));
  35. var customEditors = new Dictionary<Type, PostProcessingModelEditor>();
  36. foreach (var editor in editorTypes)
  37. {
  38. var attr = (PostProcessingModelEditorAttribute)editor.GetCustomAttributes(typeof(PostProcessingModelEditorAttribute), false)[0];
  39. var effectType = attr.type;
  40. var alwaysEnabled = attr.alwaysEnabled;
  41. var editorInst = (PostProcessingModelEditor)Activator.CreateInstance(editor);
  42. editorInst.alwaysEnabled = alwaysEnabled;
  43. editorInst.profile = target as PostProcessingProfile;
  44. editorInst.inspector = this;
  45. customEditors.Add(effectType, editorInst);
  46. }
  47. // ... and corresponding models
  48. var baseType = target.GetType();
  49. var property = serializedObject.GetIterator();
  50. while (property.Next(true))
  51. {
  52. if (!property.hasChildren)
  53. continue;
  54. var type = baseType;
  55. var srcObject = ReflectionUtils.GetFieldValueFromPath(serializedObject.targetObject, ref type, property.propertyPath);
  56. if (srcObject == null)
  57. continue;
  58. PostProcessingModelEditor editor;
  59. if (customEditors.TryGetValue(type, out editor))
  60. {
  61. var effect = (PostProcessingModel)srcObject;
  62. if (editor.alwaysEnabled)
  63. effect.enabled = editor.alwaysEnabled;
  64. m_CustomEditors.Add(editor, effect);
  65. editor.target = effect;
  66. editor.serializedProperty = property.Copy();
  67. editor.OnPreEnable();
  68. }
  69. }
  70. // Prepare monitors
  71. m_Monitors = new List<PostProcessingMonitor>();
  72. var monitors = new List<PostProcessingMonitor>
  73. {
  74. new HistogramMonitor(),
  75. new WaveformMonitor(),
  76. new ParadeMonitor(),
  77. new VectorscopeMonitor()
  78. };
  79. var monitorNames = new List<GUIContent>();
  80. foreach (var monitor in monitors)
  81. {
  82. if (monitor.IsSupported())
  83. {
  84. monitor.Init(m_ConcreteTarget.monitors, this);
  85. m_Monitors.Add(monitor);
  86. monitorNames.Add(monitor.GetMonitorTitle());
  87. }
  88. }
  89. m_MonitorNames = monitorNames.ToArray();
  90. if (m_Monitors.Count > 0)
  91. m_ConcreteTarget.monitors.onFrameEndEditorOnly = OnFrameEnd;
  92. }
  93. void OnDisable()
  94. {
  95. if (m_CustomEditors != null)
  96. {
  97. foreach (var editor in m_CustomEditors.Keys)
  98. editor.OnDisable();
  99. m_CustomEditors.Clear();
  100. }
  101. if (m_Monitors != null)
  102. {
  103. foreach (var monitor in m_Monitors)
  104. monitor.Dispose();
  105. m_Monitors.Clear();
  106. }
  107. if (m_ConcreteTarget != null)
  108. m_ConcreteTarget.monitors.onFrameEndEditorOnly = null;
  109. }
  110. void OnFrameEnd(RenderTexture source)
  111. {
  112. if (!IsInteractivePreviewOpened)
  113. return;
  114. if (m_CurrentMonitorID < m_Monitors.Count)
  115. m_Monitors[m_CurrentMonitorID].OnFrameData(source);
  116. IsInteractivePreviewOpened = false;
  117. }
  118. public override void OnInspectorGUI()
  119. {
  120. serializedObject.Update();
  121. // Handles undo/redo events first (before they get used by the editors' widgets)
  122. var e = Event.current;
  123. if (e.type == EventType.ValidateCommand && e.commandName == "UndoRedoPerformed")
  124. {
  125. foreach (var editor in m_CustomEditors)
  126. editor.Value.OnValidate();
  127. }
  128. if (!m_ConcreteTarget.debugViews.IsModeActive(BuiltinDebugViewsModel.Mode.None))
  129. EditorGUILayout.HelpBox("A debug view is currently enabled. Changes done to an effect might not be visible.", MessageType.Info);
  130. foreach (var editor in m_CustomEditors)
  131. {
  132. EditorGUI.BeginChangeCheck();
  133. editor.Key.OnGUI();
  134. if (EditorGUI.EndChangeCheck())
  135. editor.Value.OnValidate();
  136. }
  137. serializedObject.ApplyModifiedProperties();
  138. }
  139. public override GUIContent GetPreviewTitle()
  140. {
  141. return s_PreviewTitle;
  142. }
  143. public override bool HasPreviewGUI()
  144. {
  145. return GraphicsUtils.supportsDX11 && m_Monitors.Count > 0;
  146. }
  147. public override void OnPreviewSettings()
  148. {
  149. using (new EditorGUILayout.HorizontalScope())
  150. {
  151. if (m_CurrentMonitorID < m_Monitors.Count)
  152. m_Monitors[m_CurrentMonitorID].OnMonitorSettings();
  153. GUILayout.Space(5);
  154. m_CurrentMonitorID = EditorGUILayout.Popup(m_CurrentMonitorID, m_MonitorNames, FxStyles.preDropdown, GUILayout.MaxWidth(100f));
  155. }
  156. }
  157. public override void OnInteractivePreviewGUI(Rect r, GUIStyle background)
  158. {
  159. IsInteractivePreviewOpened = true;
  160. if (m_CurrentMonitorID < m_Monitors.Count)
  161. m_Monitors[m_CurrentMonitorID].OnMonitorGUI(r);
  162. }
  163. }
  164. }