TweenTransform.cs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. //----------------------------------------------
  2. // NGUI: Next-Gen UI kit
  3. // Copyright © 2011-2014 Tasharen Entertainment
  4. //----------------------------------------------
  5. using UnityEngine;
  6. /// <summary>
  7. /// Tween the object's position, rotation and scale.
  8. /// </summary>
  9. public class TweenTransform : Tweener
  10. {
  11. public Transform from;
  12. public Transform to;
  13. public bool parentWhenFinished = false;
  14. Transform mTrans;
  15. Vector3 mPos;
  16. Quaternion mRot;
  17. Vector3 mScale;
  18. /// <summary>
  19. /// Interpolate the position, scale, and rotation.
  20. /// </summary>
  21. protected override void OnUpdate (float factor, bool isFinished)
  22. {
  23. if (to != null)
  24. {
  25. if (mTrans == null)
  26. {
  27. mTrans = transform;
  28. mPos = mTrans.position;
  29. mRot = mTrans.rotation;
  30. mScale = mTrans.localScale;
  31. }
  32. if (from != null)
  33. {
  34. mTrans.position = from.position * (1f - factor) + to.position * factor;
  35. mTrans.localScale = from.localScale * (1f - factor) + to.localScale * factor;
  36. mTrans.rotation = Quaternion.Slerp(from.rotation, to.rotation, factor);
  37. }
  38. else
  39. {
  40. mTrans.position = mPos * (1f - factor) + to.position * factor;
  41. mTrans.localScale = mScale * (1f - factor) + to.localScale * factor;
  42. mTrans.rotation = Quaternion.Slerp(mRot, to.rotation, factor);
  43. }
  44. // Change the parent when finished, if requested
  45. if (parentWhenFinished && isFinished) mTrans.parent = to;
  46. }
  47. }
  48. /// <summary>
  49. /// Start the tweening operation from the current position/rotation/scale to the target transform.
  50. /// </summary>
  51. static public TweenTransform Begin (GameObject go, float duration, Transform to) { return Begin(go, duration, null, to); }
  52. /// <summary>
  53. /// Start the tweening operation.
  54. /// </summary>
  55. static public TweenTransform Begin (GameObject go, float duration, Transform from, Transform to)
  56. {
  57. TweenTransform comp = Tweener.Begin<TweenTransform>(go, duration);
  58. comp.from = from;
  59. comp.to = to;
  60. if (duration <= 0f)
  61. {
  62. comp.Sample(1f, true);
  63. comp.enabled = false;
  64. }
  65. return comp;
  66. }
  67. }