ScaleAction.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. using System;
  2. using UnityEngine;
  3. namespace CommonUnity3D.UGUIAction
  4. {
  5. /// <summary>
  6. /// 缩放动作.
  7. /// eg:
  8. /// ScaleAction scale = new ScaleAction();
  9. /// scale.ScaleX = 1.5f;
  10. /// scale.ScaleY = 1.5f;
  11. /// scale.Duration = 1;
  12. /// scale.ActionEaseType = CommonUnity3D.UGUIAction.EaseType.easeInOutBack;
  13. /// scale.ActionFinishCallBack = MoveActionCallBack;
  14. /// cvs.AddAction(scale);
  15. /// </summary>
  16. public class ScaleAction : ActionBase
  17. {
  18. public const string ACTIONTYPE = "ScaleAction";
  19. private float mTargetX = 0.0f;
  20. private float mTargetY = 0.0f;
  21. private float mStartX = 0.0f;
  22. private float mStartY = 0.0f;
  23. private float mTotalTime = 0.0f;
  24. private float mCurrentTime = 0.0f;
  25. private int mCurrentCycle = 0;
  26. private bool mReverse = false;
  27. private Vector2 mV = Vector2.zero;
  28. public float ScaleX
  29. {
  30. get { return mTargetX; }
  31. set { mTargetX = value; }
  32. }
  33. public float ScaleY
  34. {
  35. get { return mTargetY; }
  36. set { mTargetY = value; }
  37. }
  38. public float Duration
  39. {
  40. get { return mTotalTime; }
  41. set { mTotalTime = value; }
  42. }
  43. public override void onUpdate(IActionCompment unit, float deltaTime)
  44. {
  45. if (deltaTime == 0 || (mCurrentTime == mTotalTime)) return;
  46. float previousTime = mCurrentTime;
  47. float restTime = mTotalTime - mCurrentTime;
  48. float carryOverTime = deltaTime > restTime ? deltaTime - restTime : 0.0f;
  49. mCurrentTime = Math.Min(mTotalTime, mCurrentTime + deltaTime);
  50. if (mCurrentCycle < 0 && previousTime <= 0 && mCurrentTime > 0)
  51. {
  52. mCurrentCycle++;
  53. }
  54. float ratio = mCurrentTime / mTotalTime;
  55. bool reversed = mReverse && (mCurrentCycle % 2 == 1);
  56. float deltaX = mTargetX - mStartX;
  57. float deltaY = mTargetY - mStartY;
  58. float transitionValue = 0.0f;
  59. if (reversed == true)
  60. {
  61. transitionValue = EaseManager.EasingFromType(0, 1, (float)(1.0 - ratio), mEaseType);
  62. }
  63. else
  64. {
  65. transitionValue = EaseManager.EasingFromType(0, 1, ratio, mEaseType);
  66. }
  67. float currentValueX = mStartX + transitionValue * deltaX;
  68. float currentValueY = mStartY + transitionValue * deltaY;
  69. mV.x = currentValueX;
  70. mV.y = currentValueY;
  71. unit.Scale = mV;
  72. if (previousTime < mTotalTime && mCurrentTime >= mTotalTime) { mIsEnd = true; }
  73. }
  74. public override void onStart(IActionCompment unit)
  75. {
  76. Vector2 OldScale = unit.Scale;
  77. mStartX = OldScale.x;
  78. mStartY = OldScale.y;
  79. }
  80. public override bool IsEnd()
  81. {
  82. return mIsEnd;
  83. }
  84. public override string GetActionType()
  85. {
  86. return ACTIONTYPE;
  87. }
  88. }
  89. }