DebugState.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.Rendering;
  4. namespace UnityEditor.Rendering
  5. {
  6. /// <summary>
  7. /// Serialized state of a Debug Item.
  8. /// </summary>
  9. [Serializable]
  10. public abstract class DebugState : ScriptableObject
  11. {
  12. /// <summary>
  13. /// Path of the Debug Item.
  14. /// </summary>
  15. [SerializeField]
  16. protected string m_QueryPath;
  17. // We need this to keep track of the state modified in the current frame.
  18. // This helps reduces the cost of re-applying states to original widgets and is also needed
  19. // when two states point to the same value (e.g. when using split enums like HDRP does for
  20. // the `fullscreenDebugMode`.
  21. internal static DebugState m_CurrentDirtyState;
  22. /// <summary>
  23. /// Path of the Debug Item.
  24. /// </summary>
  25. public string queryPath
  26. {
  27. get { return m_QueryPath; }
  28. internal set { m_QueryPath = value; }
  29. }
  30. /// <summary>
  31. /// Returns the value of the Debug Item.
  32. /// </summary>
  33. /// <returns>Value of the Debug Item.</returns>
  34. public abstract object GetValue();
  35. /// <summary>
  36. /// Set the value of the Debug Item.
  37. /// </summary>
  38. /// <param name="value">Input value.</param>
  39. /// <param name="field">Debug Item field.</param>
  40. public abstract void SetValue(object value, DebugUI.IValueField field);
  41. /// <summary>
  42. /// OnEnable implementation.
  43. /// </summary>
  44. public virtual void OnEnable()
  45. {
  46. hideFlags = HideFlags.HideAndDontSave;
  47. }
  48. }
  49. /// <summary>
  50. /// Generic serialized state of a Debug Item.
  51. /// </summary>
  52. /// <typeparam name="T"></typeparam>
  53. [Serializable]
  54. public class DebugState<T> : DebugState
  55. {
  56. /// <summary>
  57. /// Value of the Debug Item.
  58. /// </summary>
  59. [SerializeField]
  60. protected T m_Value;
  61. /// <summary>
  62. /// Value of the Debug Item
  63. /// </summary>
  64. public virtual T value
  65. {
  66. get { return m_Value; }
  67. set { m_Value = value; }
  68. }
  69. /// <summary>
  70. /// Returns the value of the Debug Item.
  71. /// </summary>
  72. /// <returns>Value of the Debug Item.</returns>
  73. public override object GetValue()
  74. {
  75. return value;
  76. }
  77. /// <summary>
  78. /// Set the value of the Debug Item.
  79. /// </summary>
  80. /// <param name="value">Input value.</param>
  81. /// <param name="field">Debug Item field.</param>
  82. public override void SetValue(object value, DebugUI.IValueField field)
  83. {
  84. this.value = (T)field.ValidateValue(value);
  85. }
  86. /// <summary>
  87. /// Returns the hash code of the Debug Item.
  88. /// </summary>
  89. /// <returns>Hash code of the Debug Item</returns>
  90. public override int GetHashCode()
  91. {
  92. unchecked
  93. {
  94. int hash = 13;
  95. hash = hash * 23 + m_QueryPath.GetHashCode();
  96. hash = hash * 23 + m_Value.GetHashCode();
  97. return hash;
  98. }
  99. }
  100. }
  101. /// <summary>
  102. /// Attribute specifying which types should be save as this Debug State.
  103. /// </summary>
  104. public sealed class DebugStateAttribute : Attribute
  105. {
  106. internal readonly Type[] types;
  107. /// <summary>
  108. /// Debug State Attribute constructor
  109. /// </summary>
  110. /// <param name="types">List of types of the Debug State.</param>
  111. public DebugStateAttribute(params Type[] types)
  112. {
  113. this.types = types;
  114. }
  115. }
  116. // Builtins
  117. /// <summary>
  118. /// Boolean Debug State.
  119. /// </summary>
  120. [Serializable, DebugState(typeof(DebugUI.BoolField), typeof(DebugUI.Foldout), typeof(DebugUI.HistoryBoolField))]
  121. public sealed class DebugStateBool : DebugState<bool> {}
  122. /// <summary>
  123. /// Integer Debug State.
  124. /// </summary>
  125. [Serializable, DebugState(typeof(DebugUI.IntField), typeof(DebugUI.EnumField), typeof(DebugUI.HistoryEnumField))]
  126. public sealed class DebugStateInt : DebugState<int> {}
  127. /// <summary>
  128. /// Flags Debug State.
  129. /// </summary>
  130. [Serializable, DebugState(typeof(DebugUI.BitField))]
  131. public sealed class DebugStateFlags : DebugState<Enum> { }
  132. /// <summary>
  133. /// Unsigned Integer Debug State.
  134. /// </summary>
  135. [Serializable, DebugState(typeof(DebugUI.UIntField))]
  136. public sealed class DebugStateUInt : DebugState<uint> {}
  137. /// <summary>
  138. /// Float Debug State.
  139. /// </summary>
  140. [Serializable, DebugState(typeof(DebugUI.FloatField))]
  141. public sealed class DebugStateFloat : DebugState<float> {}
  142. /// <summary>
  143. /// Color Debug State.
  144. /// </summary>
  145. [Serializable, DebugState(typeof(DebugUI.ColorField))]
  146. public sealed class DebugStateColor : DebugState<Color> {}
  147. /// <summary>
  148. /// Vector2 Debug State.
  149. /// </summary>
  150. [Serializable, DebugState(typeof(DebugUI.Vector2Field))]
  151. public sealed class DebugStateVector2 : DebugState<Vector2> {}
  152. /// <summary>
  153. /// Vector3 Debug State.
  154. /// </summary>
  155. [Serializable, DebugState(typeof(DebugUI.Vector3Field))]
  156. public sealed class DebugStateVector3 : DebugState<Vector3> {}
  157. /// <summary>
  158. /// Vector4 Debug State.
  159. /// </summary>
  160. [Serializable, DebugState(typeof(DebugUI.Vector4Field))]
  161. public sealed class DebugStateVector4 : DebugState<Vector4> {}
  162. }