TweenPosition.cs 1.7 KB

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