TweenRotation.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 rotation.
  9. /// </summary>
  10. public class TweenRotation : Tweener
  11. {
  12. public Vector3 from;
  13. public Vector3 to;
  14. Transform mTrans;
  15. public Transform cachedTransform { get { if (mTrans == null) mTrans = transform; return mTrans; } }
  16. [System.Obsolete("Use 'value' instead")]
  17. public Quaternion rotation { get { return this.value; } set { this.value = value; } }
  18. /// <summary>
  19. /// Tween's current value.
  20. /// </summary>
  21. public Quaternion value { get { return cachedTransform.localRotation; } set { cachedTransform.localRotation = value; } }
  22. /// <summary>
  23. /// Tween the value.
  24. /// </summary>
  25. protected override void OnUpdate (float factor, bool isFinished)
  26. {
  27. value = Quaternion.Euler(new Vector3(
  28. Mathf.Lerp(from.x, to.x, factor),
  29. Mathf.Lerp(from.y, to.y, factor),
  30. Mathf.Lerp(from.z, to.z, factor)));
  31. }
  32. /// <summary>
  33. /// Start the tweening operation.
  34. /// </summary>
  35. static public TweenRotation Begin (GameObject go, float duration, Quaternion rot)
  36. {
  37. TweenRotation comp = Tweener.Begin<TweenRotation>(go, duration);
  38. comp.from = comp.value.eulerAngles;
  39. comp.to = rot.eulerAngles;
  40. if (duration <= 0f)
  41. {
  42. comp.Sample(1f, true);
  43. comp.enabled = false;
  44. }
  45. return comp;
  46. }
  47. [ContextMenu("Set 'From' to current value")]
  48. public override void SetStartToCurrentValue () { from = value.eulerAngles; }
  49. [ContextMenu("Set 'To' to current value")]
  50. public override void SetEndToCurrentValue () { to = value.eulerAngles; }
  51. [ContextMenu("Assume value of 'From'")]
  52. void SetCurrentValueToStart () { value = Quaternion.Euler(from); }
  53. [ContextMenu("Assume value of 'To'")]
  54. void SetCurrentValueToEnd () { value = Quaternion.Euler(to); }
  55. }