-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathSpringPosition.cs
113 lines (96 loc) · 2.43 KB
/
SpringPosition.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
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
using UnityEngine;
[AddComponentMenu("NGUI/Tween/Spring Position")]
public class SpringPosition : MonoBehaviour
{
public delegate void OnFinished();
public static SpringPosition current;
public Vector3 target = Vector3.zero;
public float strength = 10f;
public bool worldSpace;
public bool ignoreTimeScale;
public bool updateScrollView;
public OnFinished onFinished;
[HideInInspector]
[SerializeField]
private GameObject eventReceiver;
[HideInInspector]
[SerializeField]
public string callWhenFinished;
private Transform mTrans;
private float mThreshold;
private UIScrollView mSv;
private void Start()
{
mTrans = base.transform;
if (updateScrollView)
{
mSv = NGUITools.FindInParents<UIScrollView>(base.gameObject);
}
}
private void Update()
{
float deltaTime = (!ignoreTimeScale) ? Time.deltaTime : RealTime.deltaTime;
if (worldSpace)
{
if (mThreshold == 0f)
{
mThreshold = (target - mTrans.position).sqrMagnitude * 0.001f;
}
mTrans.position = NGUIMath.SpringLerp(mTrans.position, target, strength, deltaTime);
if (mThreshold >= (target - mTrans.position).sqrMagnitude)
{
mTrans.position = target;
NotifyListeners();
base.enabled = false;
}
}
else
{
if (mThreshold == 0f)
{
mThreshold = (target - mTrans.localPosition).sqrMagnitude * 1E-05f;
}
mTrans.localPosition = NGUIMath.SpringLerp(mTrans.localPosition, target, strength, deltaTime);
if (mThreshold >= (target - mTrans.localPosition).sqrMagnitude)
{
mTrans.localPosition = target;
NotifyListeners();
base.enabled = false;
}
}
if (mSv != null)
{
mSv.UpdateScrollbars(recalculateBounds: true);
}
}
private void NotifyListeners()
{
current = this;
if (onFinished != null)
{
onFinished();
}
if (eventReceiver != null && !string.IsNullOrEmpty(callWhenFinished))
{
eventReceiver.SendMessage(callWhenFinished, this, SendMessageOptions.DontRequireReceiver);
}
current = null;
}
public static SpringPosition Begin(GameObject go, Vector3 pos, float strength)
{
SpringPosition springPosition = go.GetComponent<SpringPosition>();
if (springPosition == null)
{
springPosition = go.AddComponent<SpringPosition>();
}
springPosition.target = pos;
springPosition.strength = strength;
springPosition.onFinished = null;
if (!springPosition.enabled)
{
springPosition.mThreshold = 0f;
springPosition.enabled = true;
}
return springPosition;
}
}