TweenScale.cs 1.8 KB

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