using System; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class ButtonCheckable : MonoBehaviour { Dictionary buttonStatesList = new Dictionary(); // int posCount; // int currentPos; GameObject goUp; GameObject goDown; public bool isChecked = false; public bool isStarted = false; Image goUp_Img=null, goDown_Img=null; SpriteRenderer goUp_SR = null, goDown_SR = null; Manager man; void Start() { man = GameObject.Find("Manager").GetComponent(); // Debug.Log(go.name); Transform[] buttonStates = this.GetComponentsInChildren(); // Debug.Log(btnList.Length); // goUp = new GameObject(); // goDown = new GameObject(); for (int i = 0; i < buttonStates.Length; i++) { if (i == 0) continue; if (string.Compare(buttonStates[i].name.Substring(buttonStates[i].name.Length - 2), "up") == 0) // this.name.Length - 3 goUp = buttonStates[i].gameObject; if (string.Compare(buttonStates[i].name.Substring(buttonStates[i].name.Length - 4), "down") == 0) // this.name.Length - 3 goDown = buttonStates[i].gameObject; } goUp_Img = goUp.GetComponent(); goUp_SR = goUp.GetComponent(); goDown_Img = goDown.GetComponent(); goDown_SR = goDown.GetComponent(); if (goUp_Img != null) goUp_Img.enabled = !isChecked; if (goUp_SR != null) goUp_SR.enabled = !isChecked; if (goDown_Img != null) goDown_Img.enabled = isChecked; if (goDown_SR != null) goDown_SR.enabled = isChecked; isStarted = true; } void OnMouseDown() { if (man.mode == "scedit" || man.mode == "play") return; OnCheck(); } public void OnCheck() { if (goUp == null || goDown == null) Start(); if (goUp == null || goDown == null) return; try { isChecked = !isChecked; if (goUp_Img != null) goUp_Img.enabled = !isChecked; if (goUp_SR != null) goUp_SR.enabled = !isChecked; if (goDown_Img != null) goDown_Img.enabled = isChecked; if (goDown_SR != null) goDown_SR.enabled = isChecked; } catch (Exception e) { Debug.LogError(e.Message); } } public void SetState(int st) { if (st == 0) isChecked = true; else isChecked = false; OnCheck(); } }