SpringPosition.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. using System;
  2. using UnityEngine;
  3. [AddComponentMenu("NGUI/Tween/Spring Position")]
  4. public class SpringPosition : MonoBehaviour
  5. {
  6. private void Start()
  7. {
  8. this.mTrans = base.transform;
  9. if (this.updateScrollView)
  10. {
  11. this.mSv = NGUITools.FindInParents<UIScrollView>(base.gameObject);
  12. }
  13. }
  14. private void Update()
  15. {
  16. float deltaTime = (!this.ignoreTimeScale) ? Time.deltaTime : RealTime.deltaTime;
  17. if (this.worldSpace)
  18. {
  19. if (this.mThreshold == 0f)
  20. {
  21. this.mThreshold = (this.target - this.mTrans.position).sqrMagnitude * 0.001f;
  22. }
  23. this.mTrans.position = NGUIMath.SpringLerp(this.mTrans.position, this.target, this.strength, deltaTime);
  24. if (this.mThreshold >= (this.target - this.mTrans.position).sqrMagnitude)
  25. {
  26. this.mTrans.position = this.target;
  27. this.NotifyListeners();
  28. base.enabled = false;
  29. }
  30. }
  31. else
  32. {
  33. if (this.mThreshold == 0f)
  34. {
  35. this.mThreshold = (this.target - this.mTrans.localPosition).sqrMagnitude * 1E-05f;
  36. }
  37. this.mTrans.localPosition = NGUIMath.SpringLerp(this.mTrans.localPosition, this.target, this.strength, deltaTime);
  38. if (this.mThreshold >= (this.target - this.mTrans.localPosition).sqrMagnitude)
  39. {
  40. this.mTrans.localPosition = this.target;
  41. this.NotifyListeners();
  42. base.enabled = false;
  43. }
  44. }
  45. if (this.mSv != null)
  46. {
  47. this.mSv.UpdateScrollbars(true);
  48. }
  49. }
  50. private void NotifyListeners()
  51. {
  52. SpringPosition.current = this;
  53. if (this.onFinished != null)
  54. {
  55. this.onFinished();
  56. }
  57. if (this.eventReceiver != null && !string.IsNullOrEmpty(this.callWhenFinished))
  58. {
  59. this.eventReceiver.SendMessage(this.callWhenFinished, this, SendMessageOptions.DontRequireReceiver);
  60. }
  61. SpringPosition.current = null;
  62. }
  63. public static SpringPosition Begin(GameObject go, Vector3 pos, float strength)
  64. {
  65. SpringPosition springPosition = go.GetComponent<SpringPosition>();
  66. if (springPosition == null)
  67. {
  68. springPosition = go.AddComponent<SpringPosition>();
  69. }
  70. springPosition.target = pos;
  71. springPosition.strength = strength;
  72. springPosition.onFinished = null;
  73. if (!springPosition.enabled)
  74. {
  75. springPosition.mThreshold = 0f;
  76. springPosition.enabled = true;
  77. }
  78. return springPosition;
  79. }
  80. public static SpringPosition current;
  81. public Vector3 target = Vector3.zero;
  82. public float strength = 10f;
  83. public bool worldSpace;
  84. public bool ignoreTimeScale;
  85. public bool updateScrollView;
  86. public SpringPosition.OnFinished onFinished;
  87. [SerializeField]
  88. [HideInInspector]
  89. private GameObject eventReceiver;
  90. [SerializeField]
  91. [HideInInspector]
  92. public string callWhenFinished;
  93. private Transform mTrans;
  94. private float mThreshold;
  95. private UIScrollView mSv;
  96. public delegate void OnFinished();
  97. }