using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Events; using UnityEngine.UI; public class Toggle : MonoBehaviour { [SerializeField] private List ToggleGraphics; [SerializeField] private RectTransform ToggleRect; [SerializeField] private Color EnabledColor = new Vector4(1, 1, 1, 1); [SerializeField] private Color DisabledColor = new Vector4(0.5f, 0.5f, 0.5f, 1); [SerializeField] private Vector2 EnabledPosition; [SerializeField] private Vector2 DisabledPosition; [SerializeField] private AudioClip EnableSound; [SerializeField] private AudioClip DisableSound; [SerializeField] private float speed = 500f; bool value = true; public UnityEvent OnChangeReady { get; } public void SetValue(bool newValue, bool sounded) { if (newValue == value) return; value = newValue; StopAllCoroutines(); StartCoroutine(Display(value, speed, sounded: sounded)); } IEnumerator Display(bool value, float movespeed, bool sounded) { Vector2 targetPosition = value ? EnabledPosition : DisabledPosition; Vector2 startPosition = value ? DisabledPosition : EnabledPosition; Vector4 targetColor = value ? EnabledColor : DisabledColor; Color startColor = ToggleGraphics[0].color; if (sounded) { AudioClip clip = value ? EnableSound : DisableSound; SoundManager.instance.playClip(clip); } while (ToggleRect.anchoredPosition != targetPosition) { ToggleRect.anchoredPosition = Vector2.MoveTowards(ToggleRect.anchoredPosition, targetPosition, Time.deltaTime * movespeed); //цвет float t = Vector2.Distance(ToggleRect.anchoredPosition, startPosition) / Vector2.Distance(startPosition, targetPosition); Color color = ToggleGraphics[0].color; color = Vector4.Lerp(startColor, targetColor, t); foreach (Graphic g in ToggleGraphics) { g.color = targetColor; } yield return null; } } }