using System; using UnityEngine; public class Controller { public bool AnyTouch { get { return this.Touches[0].IsDown || this.Touches[1].IsDown; } } public bool AnyTouchDown { get { return this.Touches[0].TouchStart || this.Touches[1].TouchStart; } } public bool AnyTouchUp { get { return this.Touches[0].TouchEnd || this.Touches[1].TouchEnd; } } public bool FirstDown { get { return this.Touches[0].TouchStart; } } public Vector2 DragPosition { get { return this.Touches[this.touchId].Position; } } public Vector2 DragStartPosition { get { return this.Touches[this.touchId].DownAt; } } public readonly Controller.TouchState[] Touches = new Controller.TouchState[2]; private Collider2D amTouching; private int touchId = -1; public class TouchState { public Vector2 DownAt; public Vector2 Position; public bool WasDown; public bool IsDown; public bool TouchStart; public bool TouchEnd; } public Controller() { for (int i = 0; i < this.Touches.Length; i++) { this.Touches[i] = new Controller.TouchState(); } } public DragState CheckDrag(Collider2D coll, bool firstOnly = false) { if (!coll) { return DragState.NoTouch; } if (this.touchId <= -1) { if (firstOnly) { Controller.TouchState touchState = this.Touches[0]; if (touchState.TouchStart && coll.OverlapPoint(touchState.Position)) { this.amTouching = coll; this.touchId = 0; return DragState.TouchStart; } } else { for (int i = 0; i < this.Touches.Length; i++) { Controller.TouchState touchState2 = this.Touches[i]; if (touchState2.TouchStart && coll.OverlapPoint(touchState2.Position)) { this.amTouching = coll; this.touchId = i; return DragState.TouchStart; } } } return DragState.NoTouch; } if (coll != this.amTouching) { return DragState.NoTouch; } if (this.Touches[this.touchId].IsDown) { return DragState.Dragging; } this.amTouching = null; this.touchId = -1; return DragState.Released; } public void Update() { Controller.TouchState touchState = this.Touches[0]; bool mouseButton = Input.GetMouseButton(0); touchState.Position = Camera.main.ScreenToWorldPoint(Input.mousePosition); touchState.TouchStart = (!touchState.IsDown && mouseButton); if (touchState.TouchStart) { touchState.DownAt = touchState.Position; } touchState.TouchEnd = (touchState.IsDown && !mouseButton); touchState.IsDown = mouseButton; } public void Reset() { for (int i = 0; i < this.Touches.Length; i++) { this.Touches[i] = new Controller.TouchState(); } this.touchId = -1; this.amTouching = null; } public Controller.TouchState GetTouch(int i) { return this.Touches[i]; } }