HKI Core
UIVarToggle.cs
Go to the documentation of this file.
1 using UnityEngine;
2 using UnityEngine.UI;
3 using HKI.Core.Loc;
4 using HKI.Core.Variables;
5 
6 namespace HKI.Core.UI
7 {
11  [AddComponentMenu("HKI/UI/UIVarToggle")]
12  [RequireComponent(typeof(Toggle))]
13  [DisallowMultipleComponent]
14  public class UIVarToggle : MonoBehaviour
15  {
16  // Public variables
17  [Header("General Properties")]
18  [SerializeField] Boolean Variable = null;
19 
20  [SerializeField] bool SetVarOnValueChangedCallback = false;
21  [SerializeField] bool SetVarOnDisable = false;
22 
23  [SerializeField] Language Language = null;
24  [SerializeField] TextLocalization TrueTextLocalization = null;
25  [SerializeField] TextLocalization FalseTextLocalization = null;
26 
27  [SerializeField] GameObject ValueTextGO = null;
28 
29  // Private variables
30  TMProAndUGUITextAccessor valueText = null;
31 
32  Toggle toggle = null;
33 
34  // Awake function
35  void Awake()
36  {
37  if(Variable == null)
38  {
39  Debug.LogError("(UIValueText)" + name + " there isn't a variable attached to this value text!");
40  return;
41  }
42 
43  toggle = GetComponent<Toggle>();
44 
45  toggle.onValueChanged.AddListener((value) => { OnValueChanged(); });
46 
47  valueText = new TMProAndUGUITextAccessor(ValueTextGO);
48  }
49 
50  // OnEnable function
51  void OnEnable()
52  {
53  if(Variable != null)
54  {
55  toggle.isOn = Variable.Value;
56 
57  ChangeText();
58 
59  if(Language != null)
60  Language.OnValueChanged += ChangeText;
61 
62  Variable.OnValueChanged += OnExternalChange;
63  }
64  }
65 
66  // OnDisable function
67  void OnDisable()
68  {
69  if(Variable != null)
70  {
71  if(Language != null)
72  Language.OnValueChanged -= ChangeText;
73 
74  Variable.OnValueChanged -= OnExternalChange;
75 
76  if(SetVarOnDisable)
77  Variable.SetValue = toggle.isOn;
78  }
79  }
80 
81  // Interface function
82  public void SetVarToToggleValue()
83  {
84  if(Variable != null)
85  Variable.SetValue = toggle.isOn;
86  }
87 
88  public void Reset()
89  {
90  if (Variable != null)
91  {
92  toggle.isOn = Variable.Value;
93 
94  ChangeText();
95  }
96  }
97 
98  // Helper function
100  {
101  ChangeText();
102 
103  if(SetVarOnValueChangedCallback && Variable.Value != toggle.isOn)
104  Variable.SetValue = toggle.isOn;
105  }
106 
108  {
109  if(Variable != null && toggle.isOn != Variable.Value)
110  toggle.isOn = Variable.Value;
111  }
112 
113  void ChangeText()
114  {
115  if (TrueTextLocalization != null && FalseTextLocalization != null)
116  valueText.ChangeText(toggle.isOn ? TrueTextLocalization.GetText(Language) : FalseTextLocalization.GetText(Language));
117  }
118  }
119 }
This MonoBehaviour enables to use Unity UIToggle with a Boolean HKIVar value. It also can use an Unit...
Definition: UIVarToggle.cs:14
This ScriptableObject contains as a container for all text localization information.
string GetText(Language language)
This function returns the string in a specific language. If there is no localization element in the r...
This class allows the use of the standard Unity UI Text component and the TextMeshPro component witho...
Special Variable enables the use of the SystemLanguage data type as a variable of the settings system...
Definition: Language.cs:13
Implementation of a boolean value as a HKIVar via HKIVarGeneric.
Definition: Boolean.cs:16