FadeAction.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. namespace CommonUnity3D.UGUIAction
  5. {
  6. public class FadeAction :ActionBase
  7. {
  8. public const string ACTIONTYPE = "FadeAction";
  9. private float mTargetAlpha = 1.0f;
  10. private float mStartAlpha = 1.0f;
  11. private float mTotalTime = 0.0f;
  12. private float mCurrentTime = 0.0f;
  13. private int mCurrentCycle = 0;
  14. private bool mReverse = false;
  15. public float TargetAlpha
  16. {
  17. get { return mTargetAlpha; }
  18. set
  19. {
  20. value = Math.Min(Math.Max(0.0f, value), 1.0f);
  21. mTargetAlpha = value;
  22. }
  23. }
  24. public float Duration
  25. {
  26. get { return mTotalTime; }
  27. set { mTotalTime = value; }
  28. }
  29. public override void onUpdate(IActionCompment unit, float deltaTime)
  30. {
  31. if (deltaTime == 0 || (mCurrentTime == mTotalTime)) return;
  32. float previousTime = mCurrentTime;
  33. float restTime = mTotalTime - mCurrentTime;
  34. float carryOverTime = deltaTime > restTime ? deltaTime - restTime : 0.0f;
  35. mCurrentTime = Math.Min(mTotalTime, mCurrentTime + deltaTime);
  36. if (mCurrentCycle < 0 && previousTime <= 0 && mCurrentTime > 0)
  37. {
  38. mCurrentCycle++;
  39. }
  40. float ratio = mCurrentTime / mTotalTime;
  41. bool reversed = mReverse && (mCurrentCycle % 2 == 1);
  42. float deltaX = mTargetAlpha - mStartAlpha;
  43. float transitionValue = 0.0f;
  44. if (reversed == true)
  45. {
  46. transitionValue = EaseManager.EasingFromType(0, 1, (float)(1.0 - ratio), mEaseType);
  47. }
  48. else
  49. {
  50. transitionValue = EaseManager.EasingFromType(0, 1, ratio, mEaseType);
  51. }
  52. float currentValue = mStartAlpha + transitionValue * deltaX;
  53. unit.Alpha= currentValue;
  54. if (previousTime < mTotalTime && mCurrentTime >= mTotalTime) { mIsEnd = true; }
  55. }
  56. public override void onStart(IActionCompment unit)
  57. {
  58. mStartAlpha = unit.Alpha;
  59. }
  60. public override bool IsEnd()
  61. {
  62. return mIsEnd;
  63. }
  64. public override string GetActionType()
  65. {
  66. return ACTIONTYPE;
  67. }
  68. }
  69. }