using System.Collections; using UnityEngine; using UnityEngine.UI; [RequireComponent(typeof(Graphic))] public class ButtonTester : MonoBehaviour { public bool active = false; public ButtonPinMapping button; public float fadeTime = 0.1f; Vector4 normalColor = new Vector4(1, 1, 1, 0.2f); Vector4 pressedColor = new Vector4(1, 1, 1, 1); Vector4 targetColor; Graphic graphic; Coroutine coroutine; /* private void Update() { if (button == ButtonPinMapping) }*/ private void OnEnable() { graphic = GetComponent(); graphic.color = normalColor; targetColor = normalColor; coroutine = StartCoroutine(ButtonAnimation()); } private void OnDisable() { if (coroutine != null) StopCoroutine(coroutine); coroutine = null; } public void EmulatePress() { graphic.color = pressedColor; } IEnumerator ButtonAnimation() { while (true) { Vector4 oldColor = targetColor; if (active) { targetColor = In.GetButton(button) ? pressedColor : normalColor; } if (oldColor != targetColor && targetColor == pressedColor) { SoundManager.instance.playSwitch(); } Vector4 color = graphic.color; color = Vector4.MoveTowards(color, targetColor, Time.deltaTime / fadeTime); graphic.color = color; yield return null; } } }