TweenPosition.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. //----------------------------------------------
  2. // NGUI: Next-Gen UI kit
  3. // Copyright © 2011-2014 Tasharen Entertainment
  4. //----------------------------------------------
  5. using UnityEngine;
  6. using UnityEngine.UI;
  7. /// <summary>
  8. /// Tween the object's position.
  9. /// </summary>
  10. public class TweenPosition : Tweener
  11. {
  12. public Vector3 from;
  13. public Vector3 to;
  14. [HideInInspector]
  15. public bool worldSpace = false;
  16. Transform mTrans;
  17. RectTransform mRectTrans;
  18. public Transform cachedTransform { get { if (mTrans == null) mTrans = transform; return mTrans; } }
  19. public RectTransform cachedRectTransform { get { if (mRectTrans == null) mRectTrans = GetComponent<RectTransform>(); return mRectTrans; } }
  20. /// <summary>
  21. /// Tween's current value.
  22. /// </summary>
  23. public Vector3 value
  24. {
  25. get
  26. {
  27. return worldSpace ? cachedTransform.position : cachedTransform.localPosition;
  28. }
  29. set
  30. {
  31. if (worldSpace) cachedTransform.position = value;
  32. else cachedTransform.localPosition = value;
  33. }
  34. }
  35. /// <summary>
  36. /// Tween the value.
  37. /// </summary>
  38. protected override void OnUpdate (float factor, bool isFinished) { value = from * (1f - factor) + to * factor; }
  39. /// <summary>
  40. /// Start the tweening operation.
  41. /// </summary>
  42. static public TweenPosition Begin (GameObject go, float duration, Vector3 pos)
  43. {
  44. TweenPosition comp = Tweener.Begin<TweenPosition>(go, duration);
  45. comp.from = comp.value;
  46. comp.to = pos;
  47. if (duration <= 0f)
  48. {
  49. comp.Sample(1f, true);
  50. comp.enabled = false;
  51. }
  52. return comp;
  53. }
  54. /// <summary>
  55. /// Start the tweening operation.
  56. /// </summary>
  57. static public TweenPosition Begin (GameObject go, float duration, Vector3 pos, bool worldSpace)
  58. {
  59. TweenPosition comp = Tweener.Begin<TweenPosition>(go, duration);
  60. comp.worldSpace = worldSpace;
  61. comp.from = comp.value;
  62. comp.to = pos;
  63. if (duration <= 0f)
  64. {
  65. comp.Sample(1f, true);
  66. comp.enabled = false;
  67. }
  68. return comp;
  69. }
  70. [ContextMenu("Set 'From' to current value")]
  71. public override void SetStartToCurrentValue () { from = value; }
  72. [ContextMenu("Set 'To' to current value")]
  73. public override void SetEndToCurrentValue () { to = value; }
  74. [ContextMenu("Assume value of 'From'")]
  75. void SetCurrentValueToStart () { value = from; }
  76. [ContextMenu("Assume value of 'To'")]
  77. void SetCurrentValueToEnd () { value = to; }
  78. }