TweenTransform.cs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. using System;
  2. using UnityEngine;
  3. [AddComponentMenu("NGUI/Tween/Tween Transform")]
  4. public class TweenTransform : UITweener
  5. {
  6. protected override void OnUpdate(float factor, bool isFinished)
  7. {
  8. if (this.to != null)
  9. {
  10. if (this.mTrans == null)
  11. {
  12. this.mTrans = base.transform;
  13. this.mPos = this.mTrans.position;
  14. this.mRot = this.mTrans.rotation;
  15. this.mScale = this.mTrans.localScale;
  16. }
  17. if (this.from != null)
  18. {
  19. this.mTrans.position = this.from.position * (1f - factor) + this.to.position * factor;
  20. this.mTrans.localScale = this.from.localScale * (1f - factor) + this.to.localScale * factor;
  21. this.mTrans.rotation = Quaternion.Slerp(this.from.rotation, this.to.rotation, factor);
  22. }
  23. else
  24. {
  25. this.mTrans.position = this.mPos * (1f - factor) + this.to.position * factor;
  26. this.mTrans.localScale = this.mScale * (1f - factor) + this.to.localScale * factor;
  27. this.mTrans.rotation = Quaternion.Slerp(this.mRot, this.to.rotation, factor);
  28. }
  29. if (this.parentWhenFinished && isFinished)
  30. {
  31. this.mTrans.parent = this.to;
  32. }
  33. }
  34. }
  35. public static TweenTransform Begin(GameObject go, float duration, Transform to)
  36. {
  37. return TweenTransform.Begin(go, duration, null, to);
  38. }
  39. public static TweenTransform Begin(GameObject go, float duration, Transform from, Transform to)
  40. {
  41. TweenTransform tweenTransform = UITweener.Begin<TweenTransform>(go, duration);
  42. tweenTransform.from = from;
  43. tweenTransform.to = to;
  44. if (duration <= 0f)
  45. {
  46. tweenTransform.Sample(1f, true);
  47. tweenTransform.enabled = false;
  48. }
  49. return tweenTransform;
  50. }
  51. public Transform from;
  52. public Transform to;
  53. public bool parentWhenFinished;
  54. private Transform mTrans;
  55. private Vector3 mPos;
  56. private Quaternion mRot;
  57. private Vector3 mScale;
  58. }