Project_WL/Assets/ThirdParty/NGUI/Scripts/Interaction/UIDragScrollView_Ex.cs

210 lines
5.4 KiB
C#
Raw Normal View History

2024-12-15 01:39:39 +00:00
//-------------------------------------------------
// NGUI: Next-Gen UI kit
// Copyright © 2011-2019 Tasharen Entertainment Inc
//-------------------------------------------------
using UnityEngine;
using System.Collections;
/// <summary>
/// Allows dragging of the specified scroll view by mouse or touch.
/// </summary>
[AddComponentMenu("NGUI/Interaction/Drag Scroll View")]
public class UIDragScrollView_Ex : MonoBehaviour
{
/// <summary>
/// Reference to the scroll view that will be dragged by the script.
/// </summary>
//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;
/// <summary>
/// Automatically find the scroll view if possible.
/// </summary>
void OnEnable()
{
mTrans = transform;
// Auto-upgrade
if (scrollView_W == null && draggablePanel != null)
{
scrollView_W = draggablePanel;
draggablePanel = null;
}
if (mStarted && (mAutoFind || mScroll == null))
FindScrollView();
}
/// <summary>
/// Find the scroll view.
/// </summary>
void Start()
{
mStarted = true;
FindScrollView();
}
/// <summary>
/// Find the scroll view to work with.
/// </summary>
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<UIScrollView>(mTrans);
if (sv != null && (scrollView_H == null || (mAutoFind && sv != scrollView_H)))
{
scrollView_H = sv;
mAutoFind = true;
if (scrollView_W == null)
{
sv = NGUITools.FindInParents<UIScrollView>(scrollView_H.gameObject.transform.parent.gameObject);
scrollView_W = sv;
}
if (centerOn == null)
{
centerOn = NGUITools.FindInParents<UICenterOnChild>(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;
/// <summary>
/// Stop the active dragging operation.
/// </summary>
void OnDisable()
{
if (mPressed && mScroll != null && mScroll.GetComponentInChildren<UIWrapContent>() == null)
{
mScroll.Press(false);
mScroll = null;
}
}
/// <summary>
/// Create a plane on which we will be performing the dragging.
/// </summary>
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<UIScrollView>(mTrans);
mScroll = scrollView_W;
}
}
}
/// <summary>
/// Drag the object along the plane.
/// </summary>
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();
}
}
/// <summary>
/// If the object should support the scroll wheel, do it.
/// </summary>
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);
}
}
/// <summary>
/// Pan the scroll view.
/// </summary>
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);
}
}
}