| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- //----------------------------------------------
- // NGUI: Next-Gen UI kit
- // Copyright © 2011-2014 Tasharen Entertainment
- //----------------------------------------------
- using UnityEngine;
- using System.Collections.Generic;
- /// <summary>
- /// Spring-like motion -- the farther away the object is from the target, the stronger the pull.
- /// </summary>
- public class SpringPosition : MonoBehaviour
- {
- static public SpringPosition current;
- /// <summary>
- /// Target position to tween to.
- /// </summary>
- public Vector3 target = Vector3.zero;
- /// <summary>
- /// Strength of the spring. The higher the value, the faster the movement.
- /// </summary>
- public float strength = 10f;
- /// <summary>
- /// Is the calculation done in world space or local space?
- /// </summary>
- public bool worldSpace = false;
- /// <summary>
- /// Whether the time scale will be ignored. Generally UI components should set it to 'true'.
- /// </summary>
- public bool ignoreTimeScale = false;
- /// <summary>
- /// Whether the parent scroll view will be updated as the object moves.
- /// </summary>
- public bool updateScrollView = false;
- public delegate void OnFinished ();
- /// <summary>
- /// Delegate to trigger when the spring finishes.
- /// </summary>
- public OnFinished onFinished;
- // Deprecated functionality
- [SerializeField][HideInInspector] GameObject eventReceiver = null;
- [SerializeField][HideInInspector] public string callWhenFinished;
- Transform mTrans;
- float mThreshold = 0f;
- /// <summary>
- /// Cache the transform.
- /// </summary>
- void Start ()
- {
- mTrans = transform;
- }
- /// <summary>
- /// Advance toward the target position.
- /// </summary>
- void Update ()
- {
- float delta = ignoreTimeScale ? Time.unscaledDeltaTime : Time.deltaTime;
- if (worldSpace)
- {
- if (mThreshold == 0f) mThreshold = (target - mTrans.position).sqrMagnitude * 0.001f;
- mTrans.position = Tweener.SpringLerp(mTrans.position, target, strength, delta);
- if (mThreshold >= (target - mTrans.position).sqrMagnitude)
- {
- mTrans.position = target;
- NotifyListeners();
- enabled = false;
- }
- }
- else
- {
- if (mThreshold == 0f) mThreshold = (target - mTrans.localPosition).sqrMagnitude * 0.00001f;
-
- mTrans.localPosition = Tweener.SpringLerp(mTrans.localPosition, target, strength, delta);
- if (mThreshold >= (target - mTrans.localPosition).sqrMagnitude)
- {
- mTrans.localPosition = target;
- NotifyListeners();
- enabled = false;
- }
- }
- }
- /// <summary>
- /// Notify all finished event listeners.
- /// </summary>
- void NotifyListeners ()
- {
- current = this;
- if (onFinished != null) onFinished();
- if (eventReceiver != null && !string.IsNullOrEmpty(callWhenFinished))
- eventReceiver.SendMessage(callWhenFinished, this, SendMessageOptions.DontRequireReceiver);
- current = null;
- }
- /// <summary>
- /// Start the tweening process.
- /// </summary>
- static public SpringPosition Begin (GameObject go, Vector3 pos, float strength)
- {
- SpringPosition sp = go.GetComponent<SpringPosition>();
- if (sp == null) sp = go.AddComponent<SpringPosition>();
- sp.target = pos;
- sp.strength = strength;
- sp.onFinished = null;
- if (!sp.enabled)
- {
- sp.mThreshold = 0f;
- sp.enabled = true;
- }
- return sp;
- }
- }
|