3 using UnityEngine
.Assertions
;
5 namespace UnityEngine
.Experimental
.Rendering
7 public partial class DebugUI
9 // Generic field - will be serialized in the editor if it's not read-only
10 public abstract class Field
<T
> : Widget
, IValueField
12 public Func
<T
> getter { get; set; }
13 public Action
<T
> setter { get; set; }
15 // This should be an `event` but they don't play nice with object initializers in the
16 // version of C# we use.
17 public Action
<Field
<T
>, T
> onValueChanged
;
19 object IValueField
.ValidateValue(object value)
21 return ValidateValue((T
)value);
24 public virtual T
ValidateValue(T
value)
29 object IValueField
.GetValue()
36 Assert
.IsNotNull(getter
);
40 public void SetValue(object value)
45 public void SetValue(T
value)
47 Assert
.IsNotNull(setter
);
48 var v
= ValidateValue(value);
50 if (!v
.Equals(getter()))
54 if (onValueChanged
!= null)
55 onValueChanged(this, v
);
60 public class BoolField
: Field
<bool> {}
62 public class IntField
: Field
<int>
68 public int incStep
= 1;
69 public int intStepMult
= 10;
71 public override int ValidateValue(int value)
73 if (min
!= null) value = Mathf
.Max(value, min());
74 if (max
!= null) value = Mathf
.Min(value, max());
79 public class UIntField
: Field
<uint>
81 public Func
<uint> min
;
82 public Func
<uint> max
;
85 public uint incStep
= 1u;
86 public uint intStepMult
= 10u;
88 public override uint ValidateValue(uint value)
90 if (min
!= null) value = (uint)Mathf
.Max((int)value, (int)min());
91 if (max
!= null) value = (uint)Mathf
.Min((int)value, (int)max());
96 public class FloatField
: Field
<float>
98 public Func
<float> min
;
99 public Func
<float> max
;
102 public float incStep
= 0.1f
;
103 public float incStepMult
= 10f
;
104 public int decimals
= 3;
106 public override float ValidateValue(float value)
108 if (min
!= null) value = Mathf
.Max(value, min());
109 if (max
!= null) value = Mathf
.Min(value, max());
114 public class EnumField
: Field
<int>
116 public GUIContent
[] enumNames
;
117 public int[] enumValues
;
118 public int[] quickSeparators
;
119 public int[] indexes
;
121 public Func
<int> getIndex { get; set; }
122 public Action
<int> setIndex { get; set; }
124 public int currentIndex { get { return getIndex(); }
set { setIndex(value); }
}
130 enumNames
= Enum
.GetNames(value).Select(x
=> new GUIContent(x
)).ToArray();
132 // Linq.Cast<T> on a typeless Array breaks the JIT on PS4/Mono so we have to do it manually
133 //enumValues = Enum.GetValues(value).Cast<int>().ToArray();
135 var values
= Enum
.GetValues(value);
136 enumValues
= new int[values
.Length
];
137 for (int i
= 0; i
< values
.Length
; i
++)
138 enumValues
[i
] = (int)values
.GetValue(i
);
141 InitQuickSeparators();
145 public void InitQuickSeparators()
147 var enumNamesPrefix
= enumNames
.Select(x
=>
149 string[] splitted
= x
.text
.Split('/');
150 if (splitted
.Length
== 1)
155 quickSeparators
= new int[enumNamesPrefix
.Distinct().Count()];
156 string lastPrefix
= null;
157 for (int i
= 0, wholeNameIndex
= 0; i
< quickSeparators
.Length
; ++i
)
159 var currentTestedPrefix
= enumNamesPrefix
.ElementAt(wholeNameIndex
);
160 while (lastPrefix
== currentTestedPrefix
)
162 currentTestedPrefix
= enumNamesPrefix
.ElementAt(++wholeNameIndex
);
164 lastPrefix
= currentTestedPrefix
;
165 quickSeparators
[i
] = wholeNameIndex
++;
169 public void InitIndexes()
171 indexes
= new int[enumNames
.Length
];
172 for (int i
= 0; i
< enumNames
.Length
; i
++)
179 public class ColorField
: Field
<Color
>
181 public bool hdr
= false;
182 public bool showAlpha
= true;
185 public bool showPicker
= true;
188 public float incStep
= 0.025f
;
189 public float incStepMult
= 5f
;
190 public int decimals
= 3;
192 public override Color
ValidateValue(Color
value)
196 value.r
= Mathf
.Clamp01(value.r
);
197 value.g
= Mathf
.Clamp01(value.g
);
198 value.b
= Mathf
.Clamp01(value.b
);
199 value.a
= Mathf
.Clamp01(value.a
);
206 public class Vector2Field
: Field
<Vector2
>
209 public float incStep
= 0.025f
;
210 public float incStepMult
= 10f
;
211 public int decimals
= 3;
214 public class Vector3Field
: Field
<Vector3
>
217 public float incStep
= 0.025f
;
218 public float incStepMult
= 10f
;
219 public int decimals
= 3;
222 public class Vector4Field
: Field
<Vector4
>
225 public float incStep
= 0.025f
;
226 public float incStepMult
= 10f
;
227 public int decimals
= 3;