VolumeParameterDrawer.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.Rendering;
  4. namespace UnityEditor.Rendering
  5. {
  6. /// <summary>
  7. /// This attributes tells an <see cref="VolumeParameterDrawer"/> class which type of
  8. /// <see cref="VolumeParameter"/> it's an editor for.
  9. /// When you make a custom drawer for a parameter, you need add this attribute to the drawer
  10. /// class.
  11. /// </summary>
  12. /// <seealso cref="VolumeParameterDrawer"/>
  13. [AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
  14. public sealed class VolumeParameterDrawerAttribute : Attribute
  15. {
  16. /// <summary>
  17. /// A type derived from <see cref="VolumeParameter{T}"/>.
  18. /// </summary>
  19. public readonly Type parameterType;
  20. /// <summary>
  21. /// Creates a new <see cref="VolumeParameterDrawerAttribute"/> instance.
  22. /// </summary>
  23. /// <param name="parameterType">A type derived from <see cref="VolumeParameter{T}"/>.</param>
  24. public VolumeParameterDrawerAttribute(Type parameterType)
  25. {
  26. this.parameterType = parameterType;
  27. }
  28. }
  29. /// <summary>
  30. /// A base class to implement to draw custom editors for custom <see cref="VolumeParameter"/>.
  31. /// You must use a <see cref="VolumeParameterDrawerAttribute"/> to let the editor know which
  32. /// parameter this drawer is for.
  33. /// </summary>
  34. /// <remarks>
  35. /// If you do not provide a custom editor for a <see cref="VolumeParameter"/>, Unity uses the buil-in property drawers to draw the
  36. /// property as-is.
  37. /// </remarks>
  38. /// <example>
  39. /// Here's an example about how <see cref="ClampedFloatParameter"/> is implemented:
  40. /// <code>
  41. /// [VolumeParameterDrawer(typeof(ClampedFloatParameter))]
  42. /// class ClampedFloatParameterDrawer : VolumeParameterDrawer
  43. /// {
  44. /// public override bool OnGUI(SerializedDataParameter parameter, GUIContent title)
  45. /// {
  46. /// var value = parameter.value;
  47. ///
  48. /// if (value.propertyType != SerializedPropertyType.Float)
  49. /// return false;
  50. ///
  51. /// var o = parameter.GetObjectRef&lt;ClampedFloatParameter&gt;();
  52. /// EditorGUILayout.Slider(value, o.min, o.max, title);
  53. /// value.floatValue = Mathf.Clamp(value.floatValue, o.min, o.max);
  54. /// return true;
  55. /// }
  56. /// }
  57. /// </code>
  58. /// </example>
  59. /// <seealso cref="VolumeParameterDrawerAttribute"/>
  60. public abstract class VolumeParameterDrawer
  61. {
  62. // Override this and return false if you want to customize the override checkbox position,
  63. // else it'll automatically draw it and put the property content in a horizontal scope.
  64. /// <summary>
  65. /// Override this and return <c>false</c> if you want to customize the position of the override
  66. /// checkbox. If you don't, Unity automatically draws the checkbox and puts the property content in a
  67. /// horizontal scope.
  68. /// </summary>
  69. /// <returns><c>false</c> if the override checkbox position is customized, <c>true</c>
  70. /// otherwise</returns>
  71. public virtual bool IsAutoProperty() => true;
  72. /// <summary>
  73. /// Draws the parameter in the editor. If the input parameter is invalid you should return
  74. /// <c>false</c> so that Unity displays the default editor for this parameter.
  75. /// </summary>
  76. /// <param name="parameter">The parameter to draw.</param>
  77. /// <param name="title">The label and tooltip of the parameter.</param>
  78. /// <returns><c>true</c> if the input parameter is valid, <c>false</c> otherwise in which
  79. /// case Unity will revert to the default editor for this parameter</returns>
  80. public abstract bool OnGUI(SerializedDataParameter parameter, GUIContent title);
  81. }
  82. }