using System; using UnityEngine; using UnityEngine.Events; public class SlideBar : MonoBehaviour { public TextRenderer Title; public SpriteRenderer Bar; public Collider2D HitBox; public SpriteRenderer Dot; public FloatRange Range; public bool Vertical; public float Value; public UnityEvent OnValueChange; public void OnEnable() { if (this.Title) { this.Title.Color = Color.white; } this.Bar.color = Color.white; this.Dot.color = Color.white; } public void OnDisable() { if (this.Title) { this.Title.Color = Color.gray; } this.Bar.color = Color.gray; this.Dot.color = Color.gray; } public void Update() { Vector3 localPosition = this.Dot.transform.localPosition; switch (DestroyableSingleton.Instance.Controller.CheckDrag(this.HitBox, false)) { case DragState.Dragging: { Vector2 vector = DestroyableSingleton.Instance.Controller.DragPosition - this.Bar.transform.position; if (this.Vertical) { localPosition.y = this.Range.Clamp(vector.y); this.Value = this.Range.ReverseLerp(localPosition.y); } else { localPosition.x = this.Range.Clamp(vector.x); this.Value = this.Range.ReverseLerp(localPosition.x); } this.OnValueChange.Invoke(); break; } case DragState.Released: this.OnValueChange.Invoke(); break; } if (this.Vertical) { localPosition.y = this.Range.Lerp(this.Value); } else { localPosition.x = this.Range.Lerp(this.Value); } this.Dot.transform.localPosition = localPosition; } }