//------------------------------------------------- // NGUI: Next-Gen UI kit // Copyright © 2011-2019 Tasharen Entertainment Inc //------------------------------------------------- using UnityEngine; using System.Collections; /// /// Allows dragging of the specified scroll view by mouse or touch. /// [AddComponentMenu("NGUI/Interaction/Drag Scroll View")] public class UIDragScrollView_Ex : MonoBehaviour { /// /// Reference to the scroll view that will be dragged by the script. /// //UIScrollView scrollView; public UIScrollView scrollView_W; public UIScrollView scrollView_H; public UICenterOnChild centerOn; // Legacy functionality, kept for backwards compatibility. Use 'scrollView' instead. [HideInInspector] [SerializeField] UIScrollView draggablePanel; Transform mTrans; UIScrollView mScroll; bool mAutoFind = false; bool mStarted = false; int iDirection = 0; Vector2 vTset2 = Vector2.zero; /// /// Automatically find the scroll view if possible. /// void OnEnable() { mTrans = transform; // Auto-upgrade if (scrollView_W == null && draggablePanel != null) { scrollView_W = draggablePanel; draggablePanel = null; } if (mStarted && (mAutoFind || mScroll == null)) FindScrollView(); } /// /// Find the scroll view. /// void Start() { mStarted = true; FindScrollView(); } /// /// Find the scroll view to work with. /// void FindScrollView() { // If the scroll view is on a parent, don't try to remember it (as we want it to be dynamic in case of re-parenting) UIScrollView sv = NGUITools.FindInParents(mTrans); if (sv != null && (scrollView_H == null || (mAutoFind && sv != scrollView_H))) { scrollView_H = sv; mAutoFind = true; if (scrollView_W == null) { sv = NGUITools.FindInParents(scrollView_H.gameObject.transform.parent.gameObject); scrollView_W = sv; } if (centerOn == null) { centerOn = NGUITools.FindInParents(mTrans); } } else if (scrollView_H == sv) { mAutoFind = true; } mScroll = scrollView_H; if (scrollView_H == null || scrollView_H == null || centerOn == null) { enabled = false; // GameObject.Destroy(this); } } [System.NonSerialized] bool mPressed = false; /// /// Stop the active dragging operation. /// void OnDisable() { if (mPressed && mScroll != null && mScroll.GetComponentInChildren() == null) { mScroll.Press(false); mScroll = null; } } /// /// Create a plane on which we will be performing the dragging. /// void OnPress(bool pressed) { if (centerOn == null) return; iDirection = 0; vTset2 = Vector2.zero; mPressed = pressed; //if (pressed) //centerOn.bCenterOn = false; // If the scroll view has been set manually, don't try to find it again if (mAutoFind && mScroll != scrollView_W) { mScroll = scrollView_W; mAutoFind = false; } if (scrollView_W && enabled && NGUITools.GetActive(gameObject)) { scrollView_W.Press(pressed); scrollView_H.Press(pressed); if (!pressed && mAutoFind) { scrollView_W = NGUITools.FindInParents(mTrans); mScroll = scrollView_W; } } } /// /// Drag the object along the plane. /// void OnDrag(Vector2 delta) { if (iDirection == 0) { vTset2 += delta; if (Mathf.Abs(vTset2.x) - Mathf.Abs(vTset2.y) > 10) { centerOn.bCenterOn = true; iDirection = 1; } else if (Mathf.Abs(vTset2.y) - Mathf.Abs(vTset2.x) > 10) { // centerOn.bCenterOn = false; iDirection = 2; } } if (scrollView_W && NGUITools.GetActive(this)) { if (iDirection == 1) scrollView_W.Drag(); else if (iDirection == 2) scrollView_H.Drag(); } } /// /// If the object should support the scroll wheel, do it. /// void OnScroll(float delta) { if (scrollView_W && NGUITools.GetActive(this)) { if (iDirection == 1) scrollView_W.Scroll(delta); else if (iDirection == 2) scrollView_H.Scroll(delta); } } /// /// Pan the scroll view. /// public void OnPan(Vector2 delta) { if (scrollView_W && NGUITools.GetActive(this)) { if (iDirection == 1) scrollView_W.OnPan(delta); else if (iDirection == 2) scrollView_H.OnPan(delta); } } }