OVRScreenFade.cs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. using System;
  2. using System.Collections;
  3. using UnityEngine;
  4. public class OVRScreenFade : MonoBehaviour
  5. {
  6. private void Awake()
  7. {
  8. this.fadeMaterial = new Material(Shader.Find("Oculus/Unlit Transparent Color"));
  9. }
  10. private void OnEnable()
  11. {
  12. base.StartCoroutine(this.FadeIn());
  13. }
  14. private void OnLevelWasLoaded(int level)
  15. {
  16. base.StartCoroutine(this.FadeIn());
  17. }
  18. private void OnDestroy()
  19. {
  20. if (this.fadeMaterial != null)
  21. {
  22. UnityEngine.Object.Destroy(this.fadeMaterial);
  23. }
  24. }
  25. private IEnumerator FadeIn()
  26. {
  27. float elapsedTime = 0f;
  28. this.fadeMaterial.color = this.fadeColor;
  29. Color color = this.fadeColor;
  30. this.isFading = true;
  31. while (elapsedTime < this.fadeTime)
  32. {
  33. yield return this.fadeInstruction;
  34. elapsedTime += Time.deltaTime;
  35. color.a = 1f - Mathf.Clamp01(elapsedTime / this.fadeTime);
  36. this.fadeMaterial.color = color;
  37. }
  38. this.isFading = false;
  39. yield break;
  40. }
  41. private void OnPostRender()
  42. {
  43. if (this.isFading)
  44. {
  45. this.fadeMaterial.SetPass(0);
  46. GL.PushMatrix();
  47. GL.LoadOrtho();
  48. GL.Color(this.fadeMaterial.color);
  49. GL.Begin(7);
  50. GL.Vertex3(0f, 0f, -12f);
  51. GL.Vertex3(0f, 1f, -12f);
  52. GL.Vertex3(1f, 1f, -12f);
  53. GL.Vertex3(1f, 0f, -12f);
  54. GL.End();
  55. GL.PopMatrix();
  56. }
  57. }
  58. public float fadeTime = 2f;
  59. public Color fadeColor = new Color(0.01f, 0.01f, 0.01f, 1f);
  60. private Material fadeMaterial;
  61. private bool isFading;
  62. private YieldInstruction fadeInstruction = new WaitForEndOfFrame();
  63. }