123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- 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<UIScrollView>(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<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;
- }
- 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();
- }
|