-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathUIDragResize.cs
72 lines (57 loc) · 1.68 KB
/
UIDragResize.cs
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
using UnityEngine;
[AddComponentMenu("NGUI/Interaction/Drag-Resize Widget")]
public class UIDragResize : MonoBehaviour
{
public UIWidget target;
public UIWidget.Pivot pivot = UIWidget.Pivot.BottomRight;
public int minWidth = 100;
public int minHeight = 100;
public int maxWidth = 100000;
public int maxHeight = 100000;
private Plane mPlane;
private Vector3 mRayPos;
private Vector3 mLocalPos;
private int mWidth;
private int mHeight;
private bool mDragging;
private void OnDragStart()
{
if (target != null)
{
Vector3[] worldCorners = target.worldCorners;
mPlane = new Plane(worldCorners[0], worldCorners[1], worldCorners[3]);
Ray currentRay = UICamera.currentRay;
if (mPlane.Raycast(currentRay, out float enter))
{
mRayPos = currentRay.GetPoint(enter);
mLocalPos = target.cachedTransform.localPosition;
mWidth = target.width;
mHeight = target.height;
mDragging = true;
}
}
}
private void OnDrag(Vector2 delta)
{
if (mDragging && target != null)
{
Ray currentRay = UICamera.currentRay;
if (mPlane.Raycast(currentRay, out float enter))
{
Transform cachedTransform = target.cachedTransform;
cachedTransform.localPosition = mLocalPos;
target.width = mWidth;
target.height = mHeight;
Vector3 vector = currentRay.GetPoint(enter) - mRayPos;
cachedTransform.position += vector;
Vector3 vector2 = Quaternion.Inverse(cachedTransform.localRotation) * (cachedTransform.localPosition - mLocalPos);
cachedTransform.localPosition = mLocalPos;
NGUIMath.ResizeWidget(target, pivot, vector2.x, vector2.y, minWidth, minHeight, maxWidth, maxHeight);
}
}
}
private void OnDragEnd()
{
mDragging = false;
}
}