using System; using UnityEngine; using UnityEngine.Rendering; namespace UnityEditor.Rendering { /// /// Serialized state of a Debug Item. /// [Serializable] public abstract class DebugState : ScriptableObject { /// /// Path of the Debug Item. /// [SerializeField] protected string m_QueryPath; // We need this to keep track of the state modified in the current frame. // This helps reduces the cost of re-applying states to original widgets and is also needed // when two states point to the same value (e.g. when using split enums like HDRP does for // the `fullscreenDebugMode`. internal static DebugState m_CurrentDirtyState; /// /// Path of the Debug Item. /// public string queryPath { get { return m_QueryPath; } internal set { m_QueryPath = value; } } /// /// Returns the value of the Debug Item. /// /// Value of the Debug Item. public abstract object GetValue(); /// /// Set the value of the Debug Item. /// /// Input value. /// Debug Item field. public abstract void SetValue(object value, DebugUI.IValueField field); /// /// OnEnable implementation. /// public virtual void OnEnable() { hideFlags = HideFlags.HideAndDontSave; } } /// /// Generic serialized state of a Debug Item. /// /// [Serializable] public class DebugState : DebugState { /// /// Value of the Debug Item. /// [SerializeField] protected T m_Value; /// /// Value of the Debug Item /// public virtual T value { get { return m_Value; } set { m_Value = value; } } /// /// Returns the value of the Debug Item. /// /// Value of the Debug Item. public override object GetValue() { return value; } /// /// Set the value of the Debug Item. /// /// Input value. /// Debug Item field. public override void SetValue(object value, DebugUI.IValueField field) { this.value = (T)field.ValidateValue(value); } /// /// Returns the hash code of the Debug Item. /// /// Hash code of the Debug Item public override int GetHashCode() { unchecked { int hash = 13; hash = hash * 23 + m_QueryPath.GetHashCode(); if (value != null) hash = hash * 23 + value.GetHashCode(); return hash; } } } /// /// Attribute specifying which types should be save as this Debug State. /// public sealed class DebugStateAttribute : Attribute { internal readonly Type[] types; /// /// Debug State Attribute constructor /// /// List of types of the Debug State. public DebugStateAttribute(params Type[] types) { this.types = types; } } // Builtins /// /// Boolean Debug State. /// [Serializable, DebugState(typeof(DebugUI.BoolField), typeof(DebugUI.Foldout), typeof(DebugUI.HistoryBoolField))] public sealed class DebugStateBool : DebugState { } /// /// Enums Debug State. /// [Serializable, DebugState(typeof(DebugUI.EnumField), typeof(DebugUI.HistoryEnumField))] public sealed class DebugStateEnum : DebugState, ISerializationCallbackReceiver { DebugUI.EnumField m_EnumField; /// /// Set the value of the Debug Item. /// /// Input value. /// Debug Item field. public override void SetValue(object value, DebugUI.IValueField field) { m_EnumField = field as DebugUI.EnumField; base.SetValue(value, field); } void UpdateValue() { if (m_EnumField != null) { base.SetValue(value, m_EnumField); // There might be cases that the value does not map the index, look for the correct index var newCurrentIndex = Array.IndexOf(m_EnumField.enumValues, value); if (m_EnumField.currentIndex != newCurrentIndex) m_EnumField.currentIndex = newCurrentIndex; } } /// /// On Before Serialize Callback /// public void OnBeforeSerialize() => UpdateValue(); /// /// On After Deserialize Callback /// public void OnAfterDeserialize() => UpdateValue(); } /// /// Integer Debug State. /// [Serializable, DebugState(typeof(DebugUI.IntField))] public sealed class DebugStateInt : DebugState { } /// /// Flags Debug State. /// [Serializable, DebugState(typeof(DebugUI.BitField))] public sealed class DebugStateFlags : DebugState { [SerializeField] private SerializableEnum m_SerializableEnum; /// /// Value of the Debug Item /// public override Enum value { get => m_SerializableEnum?.value ?? default; set => m_SerializableEnum.value = value; } /// /// Set the value of the Debug Item. /// /// Input value. /// Debug Item field. public override void SetValue(object value, DebugUI.IValueField field) { if (m_SerializableEnum == null) m_SerializableEnum = new SerializableEnum((field as DebugUI.BitField).enumType); base.SetValue(value, field); } } /// /// Unsigned Integer Debug State. /// [Serializable, DebugState(typeof(DebugUI.UIntField))] public sealed class DebugStateUInt : DebugState { } /// /// Float Debug State. /// [Serializable, DebugState(typeof(DebugUI.FloatField))] public sealed class DebugStateFloat : DebugState { } /// /// Color Debug State. /// [Serializable, DebugState(typeof(DebugUI.ColorField))] public sealed class DebugStateColor : DebugState { } /// /// Vector2 Debug State. /// [Serializable, DebugState(typeof(DebugUI.Vector2Field))] public sealed class DebugStateVector2 : DebugState { } /// /// Vector3 Debug State. /// [Serializable, DebugState(typeof(DebugUI.Vector3Field))] public sealed class DebugStateVector3 : DebugState { } /// /// Vector4 Debug State. /// [Serializable, DebugState(typeof(DebugUI.Vector4Field))] public sealed class DebugStateVector4 : DebugState { } }