HKI Core
UIVarSlider.cs
Go to the documentation of this file.
1 using UnityEngine;
2 using UnityEngine.UI;
3 using HKI.Core.Variables;
4 
5 namespace HKI.Core.UI
6 {
10  [AddComponentMenu("HKI/UI/UIVarSlider")]
11  [RequireComponent(typeof(Slider))]
12  [DisallowMultipleComponent]
13  public class UIVarSlider : MonoBehaviour
14  {
15  // Public variables
16  [Header("General Properties")]
17  [SerializeField] FloatMinMax FloatVariable = null;
18  [SerializeField] IntegerMinMax IntegerVariable = null;
19 
20  [SerializeField] bool SetVarOnValueChangedCallback = false;
21  [SerializeField] bool SetVarOnDisable = false;
22 
23  [SerializeField] GameObject ValueTextGO = null;
24  [SerializeField] bool DisplayAsPercentage = false;
25  [SerializeField] int DecimalPlacesForFloatVariable = 2;
26 
27  // Private variables
28  Slider slider = null;
29 
30  TMProAndUGUITextAccessor valueText = null;
31 
32  string formatString = "N";
33 
34  float range = 0.0f;
35 
36  // Awake function
37  void Awake()
38  {
39  if(FloatVariable == null && IntegerVariable == null)
40  {
41  Debug.LogError("(UIVarSlider)" + name + " there is neither a float nor an integer variable attached to this var slider!");
42  return;
43  }
44 
45  slider = GetComponent<Slider>();
46 
47  if (FloatVariable != null)
48  {
49  slider.minValue = FloatVariable.GetMinValue;
50  slider.maxValue = FloatVariable.GetMaxValue;
51  slider.wholeNumbers = false;
52  }
53  else if (IntegerVariable != null)
54  {
55  slider.minValue = IntegerVariable.GetMinValue;
56  slider.maxValue = IntegerVariable.GetMaxValue;
57  slider.wholeNumbers = true;
58  }
59 
60  range = slider.maxValue - slider.minValue;
61 
62  if(FloatVariable != null)
63  formatString += DecimalPlacesForFloatVariable.ToString();
64 
65  valueText = new TMProAndUGUITextAccessor(ValueTextGO);
66 
67  slider.onValueChanged.AddListener((value) => { OnValueChanged(); });
68  }
69 
70  // OnEnable function
71  void OnEnable()
72  {
73  if(FloatVariable != null)
74  {
75  slider.value = FloatVariable.Value;
76 
77  ChangeText();
78 
79  FloatVariable.OnValueChanged += OnExternalChange;
80  }
81  else if(IntegerVariable != null)
82  {
83  slider.value = IntegerVariable.Value;
84 
85  ChangeText();
86 
87  IntegerVariable.OnValueChanged += OnExternalChange;
88  }
89  }
90 
91  // OnDisable
92  void OnDisable()
93  {
94  if(FloatVariable != null)
95  FloatVariable.OnValueChanged -= OnExternalChange;
96  else if(IntegerVariable != null)
97  IntegerVariable.OnValueChanged -= OnExternalChange;
98 
99  if (SetVarOnDisable)
100  SetVarToSliderValue();
101  }
102 
103  // Interface function
104  public void SetVarToSliderValue()
105  {
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;
110  }
111 
112  public void Reset()
113  {
114  if (FloatVariable != null)
115  {
116  slider.value = FloatVariable.Value;
117 
118  ChangeText();
119  }
120  else if (IntegerVariable != null)
121  {
122  slider.value = IntegerVariable.Value;
123 
124  ChangeText();
125  }
126  }
127 
128  // Callback function
130  {
131  ChangeText();
132 
133  if(SetVarOnValueChangedCallback)
134  SetVarToSliderValue();
135  }
136 
138  {
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;
143  }
144 
145  void ChangeText()
146  {
147  if(DisplayAsPercentage)
148  valueText.ChangeText(((slider.value - slider.minValue) * 100.0f / range).ToString(formatString) + "%");
149  else
150  {
151  if (FloatVariable != null)
152  valueText.ChangeText(slider.value.ToString(formatString));
153  else if (IntegerVariable != null)
154  valueText.ChangeText(((int)slider.value).ToString());
155  }
156  }
157  }
158 }
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...
Definition: UIVarSlider.cs:13
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.
Definition: FloatMinMax.cs:16