Advertisement
Guest User

Untitled

a guest
Jan 16th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.76 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using UnityEngine.SceneManagement;
  6.  
  7. public class FadeTransition : MonoBehaviour
  8. {
  9. public static FadeTransition instance = null;
  10.  
  11. public Image blackFadeImage;
  12. public Image whiteFadeImage;
  13.  
  14. void Awake()
  15. {
  16. //Check if instance already exists
  17. if (instance == null)
  18. {
  19. //if not, set instance to this
  20. instance = this;
  21. }
  22. //If instance already exists and it's not this:
  23. else if (instance != this)
  24. {
  25. //Then destroy this. This enforces our singleton pattern, meaning there can only ever be one instance of a GameManager.
  26. Destroy(gameObject);
  27. }
  28.  
  29. DontDestroyOnLoad(gameObject);
  30.  
  31. blackFadeImage.gameObject.SetActive(false);
  32. whiteFadeImage.gameObject.SetActive(false);
  33. }
  34.  
  35. public void FadeInDark(float time, System.Action action = null)
  36. {
  37. StartCoroutine(FadeIn(blackFadeImage, time, 1, action));
  38. }
  39.  
  40. public void FadeInLight(float time, System.Action action = null)
  41. {
  42. StartCoroutine(FadeIn(whiteFadeImage, time, 1, action));
  43. }
  44.  
  45. public void FadeOutDark(float time, System.Action action = null)
  46. {
  47. StartCoroutine(FadeOut(blackFadeImage, time, 0, action));
  48. }
  49.  
  50. public void FadeOutLight(float time, System.Action action = null)
  51. {
  52. StartCoroutine(FadeOut(whiteFadeImage, time, 0, action));
  53. }
  54.  
  55. public IEnumerator FadeIn(Image image, float time, float alpha = 1, System.Action action = null)
  56. {
  57. Color color = image.color;
  58. WaitForEndOfFrame waitForEndOfFrame = new WaitForEndOfFrame();
  59.  
  60. image.gameObject.SetActive(true);
  61. float updatedTime = time;
  62. color.a = 0;
  63. image.color = color;
  64.  
  65. while (updatedTime > 0.0)
  66. {
  67. yield return new WaitForEndOfFrame();
  68. updatedTime -= Time.deltaTime;
  69. color.a = alpha * (1 - (updatedTime / time));
  70. image.color = color;
  71. }
  72.  
  73. if (action != null)
  74. action();
  75. }
  76.  
  77. public IEnumerator FadeOut(Image image, float time, float alpha = 0, System.Action action = null)
  78. {
  79. Color color = image.color;
  80. WaitForEndOfFrame waitForEndOfFrame = new WaitForEndOfFrame();
  81.  
  82. image.gameObject.SetActive(true);
  83. float updatedTime = time;
  84.  
  85. while (updatedTime > 0.0)
  86. {
  87. yield return new WaitForEndOfFrame();
  88. updatedTime -= Time.deltaTime;
  89. color.a = updatedTime / time;
  90. image.color = color;
  91. }
  92. image.gameObject.SetActive(false);
  93.  
  94. if (action != null)
  95. action();
  96. }
  97.  
  98.  
  99. public void OnFadeOutComplete()
  100. {
  101. int activeSceneBuildIndex = SceneManager.GetActiveScene().buildIndex;
  102.  
  103. SceneManager.LoadScene(activeSceneBuildIndex != 0 ? activeSceneBuildIndex - 1 : activeSceneBuildIndex + 1);
  104. }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement