AlphaEffect.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. using UnityEngine;
  2. namespace WXB
  3. {
  4. public class AlphaEffect : IEffect
  5. {
  6. protected float last_update_time = -1f;
  7. protected bool isFoward = false; // 是否递增
  8. protected float space_timer = 0.05f;
  9. protected float alpha = 1f;
  10. public void UpdateEffect(Draw draw, float deltaTime)
  11. {
  12. if (last_update_time == -1f)
  13. {
  14. last_update_time = Time.realtimeSinceStartup;
  15. return;
  16. }
  17. float realtimeSinceStartup = Time.realtimeSinceStartup;
  18. float delta = realtimeSinceStartup - last_update_time;
  19. if (delta <= space_timer)
  20. return;
  21. space_timer = 0.05f;
  22. delta = space_timer;
  23. last_update_time = realtimeSinceStartup;
  24. if (isFoward)
  25. {
  26. alpha += delta;
  27. if (alpha > 1f)
  28. {
  29. alpha = 1f;
  30. isFoward = false;
  31. space_timer = 0.5f;
  32. }
  33. }
  34. else
  35. {
  36. alpha -= delta;
  37. if (alpha < 0f)
  38. {
  39. alpha = -alpha;
  40. isFoward = true;
  41. }
  42. }
  43. draw.canvasRenderer.SetAlpha(alpha);
  44. }
  45. public void Release()
  46. {
  47. last_update_time = -1f;
  48. isFoward = false; // 是否递增
  49. space_timer = 0.05f;
  50. alpha = 1f;
  51. }
  52. }
  53. }