PostProcessEffectBaseEditor.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.Rendering.PostProcessing;
  4. namespace UnityEditor.Rendering.PostProcessing
  5. {
  6. /// <summary>
  7. /// The base class for all post-processing effect related editors. If you want to customize the
  8. /// look of a custom post-processing effect, inherit from <see cref="PostProcessEffectEditor{T}"/>
  9. /// instead.
  10. /// </summary>
  11. /// <seealso cref="PostProcessEffectEditor{T}"/>
  12. public class PostProcessEffectBaseEditor
  13. {
  14. internal PostProcessEffectSettings target { get; private set; }
  15. internal SerializedObject serializedObject { get; private set; }
  16. internal SerializedProperty baseProperty;
  17. internal SerializedProperty activeProperty;
  18. SerializedProperty m_Enabled;
  19. Editor m_Inspector;
  20. internal PostProcessEffectBaseEditor()
  21. {
  22. }
  23. /// <summary>
  24. /// Repaints the inspector.
  25. /// </summary>
  26. public void Repaint()
  27. {
  28. m_Inspector.Repaint();
  29. }
  30. internal void Init(PostProcessEffectSettings target, Editor inspector)
  31. {
  32. this.target = target;
  33. m_Inspector = inspector;
  34. serializedObject = new SerializedObject(target);
  35. m_Enabled = serializedObject.FindProperty("enabled.value");
  36. activeProperty = serializedObject.FindProperty("active");
  37. OnEnable();
  38. }
  39. /// <summary>
  40. /// Called when the editor is initialized.
  41. /// </summary>
  42. public virtual void OnEnable()
  43. {
  44. }
  45. /// <summary>
  46. /// Called when the editor is de-initialized.
  47. /// </summary>
  48. public virtual void OnDisable()
  49. {
  50. }
  51. internal void OnInternalInspectorGUI()
  52. {
  53. serializedObject.Update();
  54. TopRowFields();
  55. OnInspectorGUI();
  56. EditorGUILayout.Space();
  57. serializedObject.ApplyModifiedProperties();
  58. }
  59. /// <summary>
  60. /// Called every time the inspector is being redrawn. This is where you should add your UI
  61. /// drawing code.
  62. /// </summary>
  63. public virtual void OnInspectorGUI()
  64. {
  65. }
  66. /// <summary>
  67. /// Returns the label to use as the effect title. You can override this to return a custom
  68. /// label, else it will use the effect type as the title.
  69. /// </summary>
  70. /// <returns>The label to use as the effect title</returns>
  71. public virtual string GetDisplayTitle()
  72. {
  73. return ObjectNames.NicifyVariableName(target.GetType().Name);
  74. }
  75. void TopRowFields()
  76. {
  77. using (new EditorGUILayout.HorizontalScope())
  78. {
  79. if (GUILayout.Button(EditorUtilities.GetContent("All|Toggle all overrides on. To maximize performances you should only toggle overrides that you actually need."), Styling.miniLabelButton, GUILayout.Width(17f), GUILayout.ExpandWidth(false)))
  80. SetAllOverridesTo(true);
  81. if (GUILayout.Button(EditorUtilities.GetContent("None|Toggle all overrides off."), Styling.miniLabelButton, GUILayout.Width(32f), GUILayout.ExpandWidth(false)))
  82. SetAllOverridesTo(false);
  83. GUILayout.FlexibleSpace();
  84. bool enabled = m_Enabled.boolValue;
  85. enabled = GUILayout.Toggle(enabled, EditorUtilities.GetContent("On|Enable this effect."), EditorStyles.miniButtonLeft, GUILayout.Width(35f), GUILayout.ExpandWidth(false));
  86. enabled = !GUILayout.Toggle(!enabled, EditorUtilities.GetContent("Off|Disable this effect."), EditorStyles.miniButtonRight, GUILayout.Width(35f), GUILayout.ExpandWidth(false));
  87. m_Enabled.boolValue = enabled;
  88. }
  89. }
  90. void SetAllOverridesTo(bool state)
  91. {
  92. Undo.RecordObject(target, "Toggle All");
  93. target.SetAllOverridesTo(state);
  94. serializedObject.Update();
  95. }
  96. /// <summary>
  97. /// Draws a property UI element.
  98. /// </summary>
  99. /// <param name="property">The property to draw</param>
  100. protected void PropertyField(SerializedParameterOverride property)
  101. {
  102. var title = EditorUtilities.GetContent(property.displayName);
  103. PropertyField(property, title);
  104. }
  105. /// <summary>
  106. /// Draws a property UI element with a custom title and/or tooltip.
  107. /// </summary>
  108. /// <param name="property">The property to draw</param>
  109. /// <param name="title">A custom title and/or tooltip</param>
  110. protected void PropertyField(SerializedParameterOverride property, GUIContent title)
  111. {
  112. // Check for DisplayNameAttribute first
  113. var displayNameAttr = property.GetAttribute<DisplayNameAttribute>();
  114. if (displayNameAttr != null)
  115. title.text = displayNameAttr.displayName;
  116. // Add tooltip if it's missing and an attribute is available
  117. if (string.IsNullOrEmpty(title.tooltip))
  118. {
  119. var tooltipAttr = property.GetAttribute<TooltipAttribute>();
  120. if (tooltipAttr != null)
  121. title.tooltip = tooltipAttr.tooltip;
  122. }
  123. // Look for a compatible attribute decorator
  124. AttributeDecorator decorator = null;
  125. Attribute attribute = null;
  126. foreach (var attr in property.attributes)
  127. {
  128. // Use the first decorator we found
  129. if (decorator == null)
  130. {
  131. decorator = EditorUtilities.GetDecorator(attr.GetType());
  132. attribute = attr;
  133. }
  134. // Draw unity built-in Decorators (Space, Header)
  135. if (attr is PropertyAttribute)
  136. {
  137. if (attr is SpaceAttribute)
  138. {
  139. EditorGUILayout.GetControlRect(false, (attr as SpaceAttribute).height);
  140. }
  141. else if (attr is HeaderAttribute)
  142. {
  143. var rect = EditorGUILayout.GetControlRect(false, 24f);
  144. rect.y += 8f;
  145. rect = EditorGUI.IndentedRect(rect);
  146. EditorGUI.LabelField(rect, (attr as HeaderAttribute).header, Styling.headerLabel);
  147. }
  148. }
  149. }
  150. bool invalidProp = false;
  151. if (decorator != null && !decorator.IsAutoProperty())
  152. {
  153. if (decorator.OnGUI(property.value, property.overrideState, title, attribute))
  154. return;
  155. // Attribute is invalid for the specified property; use default unity field instead
  156. invalidProp = true;
  157. }
  158. using (new EditorGUILayout.HorizontalScope())
  159. {
  160. // Override checkbox
  161. var overrideRect = GUILayoutUtility.GetRect(17f, 17f, GUILayout.ExpandWidth(false));
  162. overrideRect.yMin += 4f;
  163. EditorUtilities.DrawOverrideCheckbox(overrideRect, property.overrideState);
  164. // Property
  165. using (new EditorGUI.DisabledScope(!property.overrideState.boolValue))
  166. {
  167. if (decorator != null && !invalidProp)
  168. {
  169. if (decorator.OnGUI(property.value, property.overrideState, title, attribute))
  170. return;
  171. }
  172. // Default unity field
  173. if (property.value.hasVisibleChildren
  174. && property.value.propertyType != SerializedPropertyType.Vector2
  175. && property.value.propertyType != SerializedPropertyType.Vector3)
  176. {
  177. GUILayout.Space(12f);
  178. EditorGUILayout.PropertyField(property.value, title, true);
  179. }
  180. else
  181. {
  182. EditorGUILayout.PropertyField(property.value, title);
  183. }
  184. }
  185. }
  186. }
  187. }
  188. }