using System; using UnityEngine; [AddComponentMenu("NGUI/Tween/Spring Position")] public class SpringPosition : MonoBehaviour { private void Start() { this.mTrans = base.transform; if (this.updateScrollView) { this.mSv = NGUITools.FindInParents(base.gameObject); } } private void Update() { float deltaTime = (!this.ignoreTimeScale) ? Time.deltaTime : RealTime.deltaTime; if (this.worldSpace) { if (this.mThreshold == 0f) { this.mThreshold = (this.target - this.mTrans.position).sqrMagnitude * 0.001f; } this.mTrans.position = NGUIMath.SpringLerp(this.mTrans.position, this.target, this.strength, deltaTime); if (this.mThreshold >= (this.target - this.mTrans.position).sqrMagnitude) { this.mTrans.position = this.target; this.NotifyListeners(); base.enabled = false; } } else { if (this.mThreshold == 0f) { this.mThreshold = (this.target - this.mTrans.localPosition).sqrMagnitude * 1E-05f; } this.mTrans.localPosition = NGUIMath.SpringLerp(this.mTrans.localPosition, this.target, this.strength, deltaTime); if (this.mThreshold >= (this.target - this.mTrans.localPosition).sqrMagnitude) { this.mTrans.localPosition = this.target; this.NotifyListeners(); base.enabled = false; } } if (this.mSv != null) { this.mSv.UpdateScrollbars(true); } } private void NotifyListeners() { SpringPosition.current = this; if (this.onFinished != null) { this.onFinished(); } if (this.eventReceiver != null && !string.IsNullOrEmpty(this.callWhenFinished)) { this.eventReceiver.SendMessage(this.callWhenFinished, this, SendMessageOptions.DontRequireReceiver); } SpringPosition.current = null; } public static SpringPosition Begin(GameObject go, Vector3 pos, float strength) { SpringPosition springPosition = go.GetComponent(); if (springPosition == null) { springPosition = go.AddComponent(); } springPosition.target = pos; springPosition.strength = strength; springPosition.onFinished = null; if (!springPosition.enabled) { springPosition.mThreshold = 0f; springPosition.enabled = true; } return springPosition; } public static SpringPosition current; public Vector3 target = Vector3.zero; public float strength = 10f; public bool worldSpace; public bool ignoreTimeScale; public bool updateScrollView; public SpringPosition.OnFinished onFinished; [SerializeField] [HideInInspector] private GameObject eventReceiver; [SerializeField] [HideInInspector] public string callWhenFinished; private Transform mTrans; private float mThreshold; private UIScrollView mSv; public delegate void OnFinished(); }