SpringPosition.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. //----------------------------------------------
  2. // NGUI: Next-Gen UI kit
  3. // Copyright © 2011-2014 Tasharen Entertainment
  4. //----------------------------------------------
  5. using UnityEngine;
  6. using System.Collections.Generic;
  7. /// <summary>
  8. /// Spring-like motion -- the farther away the object is from the target, the stronger the pull.
  9. /// </summary>
  10. public class SpringPosition : MonoBehaviour
  11. {
  12. static public SpringPosition current;
  13. /// <summary>
  14. /// Target position to tween to.
  15. /// </summary>
  16. public Vector3 target = Vector3.zero;
  17. /// <summary>
  18. /// Strength of the spring. The higher the value, the faster the movement.
  19. /// </summary>
  20. public float strength = 10f;
  21. /// <summary>
  22. /// Is the calculation done in world space or local space?
  23. /// </summary>
  24. public bool worldSpace = false;
  25. /// <summary>
  26. /// Whether the time scale will be ignored. Generally UI components should set it to 'true'.
  27. /// </summary>
  28. public bool ignoreTimeScale = false;
  29. /// <summary>
  30. /// Whether the parent scroll view will be updated as the object moves.
  31. /// </summary>
  32. public bool updateScrollView = false;
  33. public delegate void OnFinished ();
  34. /// <summary>
  35. /// Delegate to trigger when the spring finishes.
  36. /// </summary>
  37. public OnFinished onFinished;
  38. // Deprecated functionality
  39. [SerializeField][HideInInspector] GameObject eventReceiver = null;
  40. [SerializeField][HideInInspector] public string callWhenFinished;
  41. Transform mTrans;
  42. float mThreshold = 0f;
  43. /// <summary>
  44. /// Cache the transform.
  45. /// </summary>
  46. void Start ()
  47. {
  48. mTrans = transform;
  49. }
  50. /// <summary>
  51. /// Advance toward the target position.
  52. /// </summary>
  53. void Update ()
  54. {
  55. float delta = ignoreTimeScale ? Time.unscaledDeltaTime : Time.deltaTime;
  56. if (worldSpace)
  57. {
  58. if (mThreshold == 0f) mThreshold = (target - mTrans.position).sqrMagnitude * 0.001f;
  59. mTrans.position = Tweener.SpringLerp(mTrans.position, target, strength, delta);
  60. if (mThreshold >= (target - mTrans.position).sqrMagnitude)
  61. {
  62. mTrans.position = target;
  63. NotifyListeners();
  64. enabled = false;
  65. }
  66. }
  67. else
  68. {
  69. if (mThreshold == 0f) mThreshold = (target - mTrans.localPosition).sqrMagnitude * 0.00001f;
  70. mTrans.localPosition = Tweener.SpringLerp(mTrans.localPosition, target, strength, delta);
  71. if (mThreshold >= (target - mTrans.localPosition).sqrMagnitude)
  72. {
  73. mTrans.localPosition = target;
  74. NotifyListeners();
  75. enabled = false;
  76. }
  77. }
  78. }
  79. /// <summary>
  80. /// Notify all finished event listeners.
  81. /// </summary>
  82. void NotifyListeners ()
  83. {
  84. current = this;
  85. if (onFinished != null) onFinished();
  86. if (eventReceiver != null && !string.IsNullOrEmpty(callWhenFinished))
  87. eventReceiver.SendMessage(callWhenFinished, this, SendMessageOptions.DontRequireReceiver);
  88. current = null;
  89. }
  90. /// <summary>
  91. /// Start the tweening process.
  92. /// </summary>
  93. static public SpringPosition Begin (GameObject go, Vector3 pos, float strength)
  94. {
  95. SpringPosition sp = go.GetComponent<SpringPosition>();
  96. if (sp == null) sp = go.AddComponent<SpringPosition>();
  97. sp.target = pos;
  98. sp.strength = strength;
  99. sp.onFinished = null;
  100. if (!sp.enabled)
  101. {
  102. sp.mThreshold = 0f;
  103. sp.enabled = true;
  104. }
  105. return sp;
  106. }
  107. }