using System; using UnityEngine; public class Scroller : MonoBehaviour { public bool AtTop { get { return this.Inner.localPosition.y <= this.YBounds.min + 0.25f; } } public bool AtBottom { get { return this.Inner.localPosition.y >= this.YBounds.max - 0.25f; } } public Transform Inner; public Collider2D HitBox; private Controller myController = new Controller(); private Vector3 origin; public bool allowX; public FloatRange XBounds = new FloatRange(-10f, 10f); public bool allowY; public FloatRange YBounds = new FloatRange(-10f, 10f); public float YBoundPerItem; public void FixedUpdate() { if (!this.Inner) { return; } Vector2 mouseScrollDelta = Input.mouseScrollDelta; mouseScrollDelta.y = -mouseScrollDelta.y; this.DoScroll(this.Inner.transform.localPosition, mouseScrollDelta); this.myController.Update(); DragState dragState = this.myController.CheckDrag(this.HitBox, false); if (dragState == DragState.TouchStart) { this.origin = this.Inner.transform.localPosition; return; } if (dragState != DragState.Dragging) { return; } Vector2 del = this.myController.DragPosition - this.myController.DragStartPosition; this.DoScroll(this.origin, del); } private void DoScroll(Vector3 origin, Vector2 del) { if (del.magnitude < 0.05f) { return; } if (!this.allowX) { del.x = 0f; } if (!this.allowY) { del.y = 0f; } Vector3 vector = origin + del; vector.x = this.XBounds.Clamp(vector.x); int childCount = this.Inner.transform.childCount; float max = Mathf.Max(this.YBounds.min, this.YBounds.max + this.YBoundPerItem * (float)childCount); vector.y = Mathf.Clamp(vector.y, this.YBounds.min, max); this.Inner.transform.localPosition = vector; } }