using System; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class myButton : MonoBehaviour { Dictionary buttonStatesList = new Dictionary(); // int posCount; // int currentPos; GameObject goUp; GameObject goDown; Manager man; Image goUp_Img = null, goDown_Img = null; SpriteRenderer goUp_SR = null, goDown_SR = null; 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; } } if (goUp == null || goDown == null) return; goUp_Img = goUp.GetComponent(); goUp_SR = goUp.GetComponent(); goDown_Img = goDown.GetComponent(); goDown_SR = goDown.GetComponent(); if (goUp_Img != null && goDown_Img != null) { goUp_Img.enabled = true; goDown_Img.enabled = false; } else { goUp_SR.enabled = true; goDown_SR.enabled = false; } } public void OnMouseDown() { if (man.mode == "scedit" || man.mode == "play") return; OnDown(); man.player.SetObjectState(man.objects.Find(this.gameObject.name), 1); } public void OnDown() { if (goUp == null || goDown == null) return; if (goUp_Img != null && goDown_Img != null) { goUp_Img.enabled = false; goDown_Img.enabled = true; } else { goDown_SR.enabled = true; goUp_SR.enabled = false; } } void OnMouseUp() { if (man.mode == "scedit" || man.mode == "play") return; OnUp(); } public void OnUp() { if (goUp == null || goDown == null) return; try { if (goUp_Img != null && goDown_Img != null) { goUp_Img.enabled = true; goDown_Img.enabled = false; } else { goUp_SR.enabled = true; goDown_SR.enabled = false; } } catch(Exception e) { Debug.LogError(e.Message); } } }