10 [AddComponentMenu(
"HKI/UI/UIVarSlider")]
11 [RequireComponent(typeof(Slider))]
12 [DisallowMultipleComponent]
16 [Header(
"General Properties")]
20 [SerializeField]
bool SetVarOnValueChangedCallback =
false;
21 [SerializeField]
bool SetVarOnDisable =
false;
23 [SerializeField] GameObject ValueTextGO = null;
24 [SerializeField]
bool DisplayAsPercentage =
false;
25 [SerializeField]
int DecimalPlacesForFloatVariable = 2;
32 string formatString =
"N";
39 if(FloatVariable == null && IntegerVariable == null)
41 Debug.LogError(
"(UIVarSlider)" + name +
" there is neither a float nor an integer variable attached to this var slider!");
45 slider = GetComponent<Slider>();
47 if (FloatVariable != null)
51 slider.wholeNumbers =
false;
53 else if (IntegerVariable != null)
57 slider.wholeNumbers =
true;
60 range = slider.maxValue - slider.minValue;
62 if(FloatVariable != null)
63 formatString += DecimalPlacesForFloatVariable.ToString();
67 slider.onValueChanged.AddListener((value) => { OnValueChanged(); });
73 if(FloatVariable != null)
75 slider.value = FloatVariable.Value;
79 FloatVariable.OnValueChanged += OnExternalChange;
81 else if(IntegerVariable != null)
83 slider.value = IntegerVariable.Value;
87 IntegerVariable.OnValueChanged += OnExternalChange;
94 if(FloatVariable != null)
95 FloatVariable.OnValueChanged -= OnExternalChange;
96 else if(IntegerVariable != null)
97 IntegerVariable.OnValueChanged -= OnExternalChange;
100 SetVarToSliderValue();
106 if(FloatVariable != null && FloatVariable.Value != slider.value)
107 FloatVariable.SetValue = slider.value;
108 else if(IntegerVariable != null && IntegerVariable.Value != (
int)slider.value)
109 IntegerVariable.SetValue = (int)slider.value;
114 if (FloatVariable != null)
116 slider.value = FloatVariable.Value;
120 else if (IntegerVariable != null)
122 slider.value = IntegerVariable.Value;
133 if(SetVarOnValueChangedCallback)
134 SetVarToSliderValue();
139 if(FloatVariable != null && FloatVariable.Value != slider.value)
140 slider.value = FloatVariable.Value;
141 else if (IntegerVariable != null && IntegerVariable.Value != (
int)slider.value)
142 slider.value = (int)IntegerVariable.Value;
147 if(DisplayAsPercentage)
148 valueText.
ChangeText(((slider.value - slider.minValue) * 100.0f / range).ToString(formatString) +
"%");
151 if (FloatVariable != null)
152 valueText.
ChangeText(slider.value.ToString(formatString));
153 else if (IntegerVariable != null)
154 valueText.
ChangeText(((
int)slider.value).ToString());
Implementation of a min max integer value as a HKIVar via HKIVarGenericMinMax.
This MonoBehaviour enables to use Unity UISlider with an IntegerMinMax or a FloatMinMax HKIVar value...
void ChangeText(string text)
void SetVarToSliderValue()
This class allows the use of the standard Unity UI Text component and the TextMeshPro component witho...
Implementation of a min max float value as a HKIVar via HKIVarGenericMinMax.