FadeInOut.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. using System.Collections;
  2. using UnityEngine;
  3. using UnityEngine.UI;
  4. namespace Mirror.Examples.AdditiveLevels
  5. {
  6. public class FadeInOut : MonoBehaviour
  7. {
  8. // set these in the inspector
  9. [Range(1, 100), Tooltip("Speed of fade in / out: lower is slower")]
  10. public byte speed = 1;
  11. [Tooltip("Reference to Image component on child panel")]
  12. public Image fadeImage;
  13. [Tooltip("Color to use during scene transition")]
  14. public Color fadeColor = Color.black;
  15. WaitForSeconds waitForSeconds;
  16. void Awake()
  17. {
  18. waitForSeconds = new WaitForSeconds(speed * 0.01f);
  19. }
  20. public IEnumerator FadeIn()
  21. {
  22. //Debug.Log($"{System.DateTime.Now:HH:mm:ss:fff} FadeIn - fading image in {fadeImage.color.a}");
  23. float alpha = fadeImage.color.a;
  24. while (alpha < 1)
  25. {
  26. yield return waitForSeconds;
  27. alpha += 0.01f;
  28. fadeColor.a = alpha;
  29. fadeImage.color = fadeColor;
  30. }
  31. //Debug.Log($"{System.DateTime.Now:HH:mm:ss:fff} FadeIn - done fading");
  32. }
  33. public IEnumerator FadeOut()
  34. {
  35. //Debug.Log($"{System.DateTime.Now:HH:mm:ss:fff} FadeOut - fading image out {fadeImage.color.a}");
  36. float alpha = fadeImage.color.a;
  37. while (alpha > 0)
  38. {
  39. yield return waitForSeconds;
  40. alpha -= 0.01f;
  41. fadeColor.a = alpha;
  42. fadeImage.color = fadeColor;
  43. }
  44. //Debug.Log($"{System.DateTime.Now:HH:mm:ss:fff} FadeOut - done fading");
  45. }
  46. }
  47. }