Fade.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. using System;
  2. using System.Collections;
  3. using UnityEngine;
  4. public class Fade : MonoBehaviour
  5. {
  6. private void Start()
  7. {
  8. this.Init();
  9. this.fade.Range = this.cutoutRange;
  10. }
  11. private void Init()
  12. {
  13. this.fade = base.GetComponent<IFadeCam>();
  14. }
  15. private void OnValidate()
  16. {
  17. this.Init();
  18. this.fade.Range = this.cutoutRange;
  19. }
  20. private IEnumerator FadeoutCoroutine(float time, Action action)
  21. {
  22. float endTime = Time.timeSinceLevelLoad + time * this.cutoutRange;
  23. WaitForEndOfFrame endFrame = new WaitForEndOfFrame();
  24. while (Time.timeSinceLevelLoad <= endTime)
  25. {
  26. this.cutoutRange = (endTime - Time.timeSinceLevelLoad) / time;
  27. this.fade.Range = this.cutoutRange;
  28. yield return endFrame;
  29. }
  30. this.cutoutRange = 0f;
  31. this.fade.Range = this.cutoutRange;
  32. if (action != null)
  33. {
  34. action();
  35. }
  36. yield break;
  37. }
  38. private IEnumerator FadeinCoroutine(float time, Action action)
  39. {
  40. float endTime = Time.timeSinceLevelLoad + time * (1f - this.cutoutRange);
  41. WaitForEndOfFrame endFrame = new WaitForEndOfFrame();
  42. while (Time.timeSinceLevelLoad <= endTime)
  43. {
  44. this.cutoutRange = 1f - (endTime - Time.timeSinceLevelLoad) / time;
  45. this.fade.Range = this.cutoutRange;
  46. yield return endFrame;
  47. }
  48. this.cutoutRange = 1f;
  49. this.fade.Range = this.cutoutRange;
  50. if (action != null)
  51. {
  52. action();
  53. }
  54. yield break;
  55. }
  56. public Coroutine FadeOut(float time, Action action)
  57. {
  58. base.StopAllCoroutines();
  59. return base.StartCoroutine(this.FadeoutCoroutine(time, action));
  60. }
  61. public Coroutine FadeOut(float time)
  62. {
  63. return this.FadeOut(time, null);
  64. }
  65. public Coroutine FadeIn(float time, Action action)
  66. {
  67. base.StopAllCoroutines();
  68. return base.StartCoroutine(this.FadeinCoroutine(time, action));
  69. }
  70. public Coroutine FadeIn(float time)
  71. {
  72. return this.FadeIn(time, null);
  73. }
  74. private IFadeCam fade;
  75. private float cutoutRange;
  76. }