void Start() { boolsUI.clearAllButton.onClick.AddListener(() => { for (int i = 0; i < bools.Count; i++) { DestroyBool(bools[i]); } }); boolsUI.addButton.onClick.AddListener(() => { int keysCount = bools.Count; string newItemKey = "New " + "Boolean" + " "; while (LocalPrefs.HasKey <bool>(newItemKey + keysCount)) { keysCount++; } string newKey = newItemKey + keysCount; LocalPrefs.Set(newKey, false); InstatiateBoolUI(newKey); }); string[] allBooleans = LocalPrefs.AllKeys <bool>(); bools = new List <BoolUI>(allBooleans.Length); for (int i = 0; i < allBooleans.Length; i++) { bools.Add(InstatiateBoolUI(allBooleans[i])); } SetBoolListeners(); }
void SetBoolListeners() { for (int i = 0; i < bools.Count; i++) { int m_i = i; var boolUI = bools[m_i]; bools[i].input.onEndEdit.RemoveAllListeners(); bools[i].toggle.onValueChanged.RemoveAllListeners(); bools[i].removeButton.onClick.RemoveAllListeners(); // On Input field bools[i].input.onEndEdit.AddListener((s) => { if (!LocalPrefs.HasKey <bool>(s)) { boolUI.key = LocalPrefs.ChangeKey <bool>(boolUI.key, s); bools[m_i] = boolUI; } else if (s != boolUI.key) { bools[m_i].input.text = boolUI.key; Debug.Log("Boolean with key \"" + s + "\" is already presented in preferences."); } SetBoolListeners(); }); // On Toggle bools[i].toggle.onValueChanged.AddListener((b) => { LocalPrefs.Set(boolUI.key, b); SetBoolListeners(); }); // On Remove button bools[i].removeButton.onClick.AddListener(() => { DestroyBool(boolUI); SetBoolListeners(); }); } }