-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUIDragResize.cs
More file actions
121 lines (94 loc) · 2.7 KB
/
UIDragResize.cs
File metadata and controls
121 lines (94 loc) · 2.7 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
//----------------------------------------------
// NGUI: Next-Gen UI kit
// Copyright © 2011-2014 Tasharen Entertainment
//----------------------------------------------
using UnityEngine;
/// <summary>
/// This script makes it possible to resize the specified widget by dragging on the object this script is attached to.
/// </summary>
[AddComponentMenu("NGUI/Interaction/Drag-Resize Widget")]
public class UIDragResize : MonoBehaviour
{
/// <summary>
/// Widget that will be dragged.
/// </summary>
public UIWidget target;
/// <summary>
/// Widget's pivot that will be dragged
/// </summary>
public UIWidget.Pivot pivot = UIWidget.Pivot.BottomRight;
/// <summary>
/// Minimum width the widget will be allowed to shrink to when resizing.
/// </summary>
public int minWidth = 100;
/// <summary>
/// Minimum height the widget will be allowed to shrink to when resizing.
/// </summary>
public int minHeight = 100;
/// <summary>
/// Maximum width the widget will be allowed to expand to when resizing.
/// </summary>
public int maxWidth = 100000;
/// <summary>
/// Maximum height the widget will be allowed to expand to when resizing.
/// </summary>
public int maxHeight = 100000;
Plane mPlane;
Vector3 mRayPos;
Vector3 mLocalPos;
int mWidth = 0;
int mHeight = 0;
bool mDragging = false;
/// <summary>
/// Start the dragging operation.
/// </summary>
void OnDragStart ()
{
if (target != null)
{
Vector3[] corners = target.worldCorners;
mPlane = new Plane(corners[0], corners[1], corners[3]);
Ray ray = UICamera.currentRay;
float dist;
if (mPlane.Raycast(ray, out dist))
{
mRayPos = ray.GetPoint(dist);
mLocalPos = target.cachedTransform.localPosition;
mWidth = target.width;
mHeight = target.height;
mDragging = true;
}
}
}
/// <summary>
/// Adjust the widget's dimensions.
/// </summary>
void OnDrag (Vector2 delta)
{
if (mDragging && target != null)
{
float dist;
Ray ray = UICamera.currentRay;
if (mPlane.Raycast(ray, out dist))
{
Transform t = target.cachedTransform;
t.localPosition = mLocalPos;
target.width = mWidth;
target.height = mHeight;
// Move the widget
Vector3 worldDelta = ray.GetPoint(dist) - mRayPos;
t.position = t.position + worldDelta;
// Calculate the final delta
Vector3 localDelta = Quaternion.Inverse(t.localRotation) * (t.localPosition - mLocalPos);
// Restore the position
t.localPosition = mLocalPos;
// Adjust the widget
NGUIMath.ResizeWidget(target, pivot, localDelta.x, localDelta.y, minWidth, minHeight, maxWidth, maxHeight);
}
}
}
/// <summary>
/// End the resize operation.
/// </summary>
void OnDragEnd () { mDragging = false; }
}