TweenPosition.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. using System;
  2. using UnityEngine;
  3. [AddComponentMenu("NGUI/Tween/Tween Position")]
  4. public class TweenPosition : UITweener
  5. {
  6. public Transform cachedTransform
  7. {
  8. get
  9. {
  10. if (this.mTrans == null)
  11. {
  12. this.mTrans = base.transform;
  13. }
  14. return this.mTrans;
  15. }
  16. }
  17. [Obsolete("Use 'value' instead")]
  18. public Vector3 position
  19. {
  20. get
  21. {
  22. return this.value;
  23. }
  24. set
  25. {
  26. this.value = value;
  27. }
  28. }
  29. public Vector3 value
  30. {
  31. get
  32. {
  33. return (!this.worldSpace) ? this.cachedTransform.localPosition : this.cachedTransform.position;
  34. }
  35. set
  36. {
  37. if (this.mRect == null || !this.mRect.isAnchored || this.worldSpace)
  38. {
  39. if (this.worldSpace)
  40. {
  41. this.cachedTransform.position = value;
  42. }
  43. else
  44. {
  45. this.cachedTransform.localPosition = value;
  46. }
  47. }
  48. else
  49. {
  50. value -= this.cachedTransform.localPosition;
  51. NGUIMath.MoveRect(this.mRect, value.x, value.y);
  52. }
  53. }
  54. }
  55. private void Awake()
  56. {
  57. this.mRect = base.GetComponent<UIRect>();
  58. }
  59. protected override void OnUpdate(float factor, bool isFinished)
  60. {
  61. this.value = this.from * (1f - factor) + this.to * factor;
  62. }
  63. public static TweenPosition Begin(GameObject go, float duration, Vector3 pos)
  64. {
  65. TweenPosition tweenPosition = UITweener.Begin<TweenPosition>(go, duration);
  66. tweenPosition.from = tweenPosition.value;
  67. tweenPosition.to = pos;
  68. if (duration <= 0f)
  69. {
  70. tweenPosition.Sample(1f, true);
  71. tweenPosition.enabled = false;
  72. }
  73. return tweenPosition;
  74. }
  75. public static TweenPosition Begin(GameObject go, float duration, Vector3 pos, bool worldSpace)
  76. {
  77. TweenPosition tweenPosition = UITweener.Begin<TweenPosition>(go, duration);
  78. tweenPosition.worldSpace = worldSpace;
  79. tweenPosition.from = tweenPosition.value;
  80. tweenPosition.to = pos;
  81. if (duration <= 0f)
  82. {
  83. tweenPosition.Sample(1f, true);
  84. tweenPosition.enabled = false;
  85. }
  86. return tweenPosition;
  87. }
  88. [ContextMenu("Set 'From' to current value")]
  89. public override void SetStartToCurrentValue()
  90. {
  91. this.from = this.value;
  92. }
  93. [ContextMenu("Set 'To' to current value")]
  94. public override void SetEndToCurrentValue()
  95. {
  96. this.to = this.value;
  97. }
  98. [ContextMenu("Assume value of 'From'")]
  99. private void SetCurrentValueToStart()
  100. {
  101. this.value = this.from;
  102. }
  103. [ContextMenu("Assume value of 'To'")]
  104. private void SetCurrentValueToEnd()
  105. {
  106. this.value = this.to;
  107. }
  108. public Vector3 from;
  109. public Vector3 to;
  110. [HideInInspector]
  111. public bool worldSpace;
  112. private Transform mTrans;
  113. private UIRect mRect;
  114. }