-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUIDragScrollView.cs
More file actions
124 lines (101 loc) · 2.58 KB
/
UIDragScrollView.cs
File metadata and controls
124 lines (101 loc) · 2.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
//----------------------------------------------
// NGUI: Next-Gen UI kit
// Copyright © 2011-2014 Tasharen Entertainment
//----------------------------------------------
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 : MonoBehaviour
{
/// <summary>
/// Reference to the scroll view that will be dragged by the script.
/// </summary>
public UIScrollView scrollView;
// Legacy functionality, kept for backwards compatibility. Use 'scrollView' instead.
[HideInInspector][SerializeField] UIScrollView draggablePanel;
Transform mTrans;
UIScrollView mScroll;
bool mAutoFind = false;
bool mStarted = false;
/// <summary>
/// Automatically find the scroll view if possible.
/// </summary>
void OnEnable ()
{
mTrans = transform;
// Auto-upgrade
if (scrollView == null && draggablePanel != null)
{
scrollView = 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 (scrollView == null)
{
scrollView = sv;
mAutoFind = true;
}
else if (scrollView == sv)
{
mAutoFind = true;
}
mScroll = scrollView;
}
/// <summary>
/// Create a plane on which we will be performing the dragging.
/// </summary>
void OnPress (bool pressed)
{
// If the scroll view has been set manually, don't try to find it again
if (mAutoFind && mScroll != scrollView)
{
mScroll = scrollView;
mAutoFind = false;
}
if (scrollView && enabled && NGUITools.GetActive(gameObject))
{
scrollView.Press(pressed);
if (!pressed && mAutoFind)
{
scrollView = NGUITools.FindInParents<UIScrollView>(mTrans);
mScroll = scrollView;
}
}
}
/// <summary>
/// Drag the object along the plane.
/// </summary>
void OnDrag (Vector2 delta)
{
if (scrollView && NGUITools.GetActive(this))
scrollView.Drag();
}
/// <summary>
/// If the object should support the scroll wheel, do it.
/// </summary>
void OnScroll (float delta)
{
if (scrollView && NGUITools.GetActive(this))
scrollView.Scroll(delta);
}
}