// Use this for initialization void Start () { if (!buttonPrefab) { Debug.LogError("Help! No button prefab found!"); } foreach (Transform child in gameObject.transform) { Destroy(child.gameObject); } foreach (string key in Fitboard.FitboardKeys) { GameObject newButton = Instantiate(buttonPrefab) as GameObject; newButton.transform.SetParent(gameObject.transform); ConfigButton CB = newButton.GetComponent<ConfigButton>(); TestButton TB = newButton.GetComponent<TestButton>(); if (CB) { Text txt = CB.GetComponentInChildren<Text>(); txt.text = key; CB.keyID = key; newButton.name = key; } else if (TB) { Text txt = TB.GetComponentInChildren<Text>(); txt.text = key; TB.keyID = key; newButton.name = key; } else { Debug.LogError("Uh oh! This button prefab: " + buttonPrefab + "doesn't have a config or test script"); } } }
private void ButtonClick(int position) { selectedButton.Image.color = Color.white; selectedButton = ConfigButtons[position]; selectedButton.Image.color = selectedColor; if (OnButtonClick != null) { OnButtonClick.Invoke(position); } }
/// <summary> /// Instantiates the relevant keys based on the version and button prefab. Forces a layout rebuild. /// </summary> private void instantiateKeys() { settingsButtons.Clear(); foreach (string key in keyList) { // Instantiates new button // Handles reloading current config (in Start of buttons) GameObject newButton = Instantiate(buttonPrefab) as GameObject; newButton.transform.SetParent(gameObject.transform, false); ConfigButton CB = newButton.GetComponent <ConfigButton>(); SettingsButton SB = newButton.GetComponent <SettingsButton>(); TestButton TB = newButton.GetComponent <TestButton>(); if (key == "") { CB.setInactive(); } else { if (SB) { Debug.Log("loading settings buttons"); if (FitBoardSave.hasConfig) { Debug.Log("Houston, we have a config"); int toggle = ConfigUtils.getConfigToggle(key); SB.setButton(key, ConfigUtils.getToggleColor(toggle), true, toggle); settingsButtons.Add(SB); } else { Debug.Log("loading default config"); SB.setButton(key, Color.grey, true); settingsButtons.Add(SB); } } else if (TB) { TB.setButton(key, Color.grey, true); } else { Debug.LogError("Uh oh! This button prefab: " + buttonPrefab + "doesn't have a config or test script"); } } } LayoutRebuilder.ForceRebuildLayoutImmediate(layout); }
/// <summary> /// Saves the current FITBoard configuration in PlayerPrefs. /// </summary> void TaskOnClick() { // reinstantiate all key assignments keys1 = new List <string>(); keys2 = new List <string>(); keys3 = new List <string>(); keys4 = new List <string>(); // loop through all keys foreach (Transform button in keysParent.transform) { ConfigButton CB = button.GetComponent <ConfigButton>(); if (CB.currentToggle == 0) // unassigned key // do nothing! { } else if (CB.currentToggle == 1) // key is assigned to key 1 { keys1.Add(CB.keyID); } else if (CB.currentToggle == 2) // key is assigned to key 2 { keys2.Add(CB.keyID); } else if (CB.currentToggle == 3) // key is assigned to key 3 { keys3.Add(CB.keyID); } else if (CB.currentToggle == 4) // key is assigned to key 4 { keys4.Add(CB.keyID); } else { Debug.LogError("What?? Hmm why is the currentToggle of this key: " + CB.keyID + " out of bounds?"); } } FitboardConfig.Keys1 = keys1; FitboardConfig.Keys2 = keys2; FitboardConfig.Keys3 = keys3; FitboardConfig.Keys4 = keys4; lm.LoadLevel(LevelManager.prevScene); }
private void Start() { var buttons = GetComponentsInChildren <Button>(); int buttonCount = buttons.Length; ConfigButtons = new ConfigButton[buttonCount]; for (int i = 0; i < buttonCount; i++) { var button = buttons[i]; var image = button.GetComponent <Image>(); var text = button.GetComponentInChildren <Text>(); var configButton = new ConfigButton(i, button, image, text); ConfigButtons[i] = configButton; button.onClick.AddListener(() => ButtonClick(configButton.Index)); } selectedButton = ConfigButtons[0]; ButtonClick(0); }
private void Awake() { instance = this; }