Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- using UnityEngine.SceneManagement;
- public class FadeTransition : MonoBehaviour
- {
- public static FadeTransition instance = null;
- public Image blackFadeImage;
- public Image whiteFadeImage;
- void Awake()
- {
- //Check if instance already exists
- if (instance == null)
- {
- //if not, set instance to this
- instance = this;
- }
- //If instance already exists and it's not this:
- else if (instance != this)
- {
- //Then destroy this. This enforces our singleton pattern, meaning there can only ever be one instance of a GameManager.
- Destroy(gameObject);
- }
- DontDestroyOnLoad(gameObject);
- blackFadeImage.gameObject.SetActive(false);
- whiteFadeImage.gameObject.SetActive(false);
- }
- public void FadeInDark(float time, System.Action action = null)
- {
- StartCoroutine(FadeIn(blackFadeImage, time, 1, action));
- }
- public void FadeInLight(float time, System.Action action = null)
- {
- StartCoroutine(FadeIn(whiteFadeImage, time, 1, action));
- }
- public void FadeOutDark(float time, System.Action action = null)
- {
- StartCoroutine(FadeOut(blackFadeImage, time, 0, action));
- }
- public void FadeOutLight(float time, System.Action action = null)
- {
- StartCoroutine(FadeOut(whiteFadeImage, time, 0, action));
- }
- public IEnumerator FadeIn(Image image, float time, float alpha = 1, System.Action action = null)
- {
- Color color = image.color;
- WaitForEndOfFrame waitForEndOfFrame = new WaitForEndOfFrame();
- image.gameObject.SetActive(true);
- float updatedTime = time;
- color.a = 0;
- image.color = color;
- while (updatedTime > 0.0)
- {
- yield return new WaitForEndOfFrame();
- updatedTime -= Time.deltaTime;
- color.a = alpha * (1 - (updatedTime / time));
- image.color = color;
- }
- if (action != null)
- action();
- }
- public IEnumerator FadeOut(Image image, float time, float alpha = 0, System.Action action = null)
- {
- Color color = image.color;
- WaitForEndOfFrame waitForEndOfFrame = new WaitForEndOfFrame();
- image.gameObject.SetActive(true);
- float updatedTime = time;
- while (updatedTime > 0.0)
- {
- yield return new WaitForEndOfFrame();
- updatedTime -= Time.deltaTime;
- color.a = updatedTime / time;
- image.color = color;
- }
- image.gameObject.SetActive(false);
- if (action != null)
- action();
- }
- public void OnFadeOutComplete()
- {
- int activeSceneBuildIndex = SceneManager.GetActiveScene().buildIndex;
- SceneManager.LoadScene(activeSceneBuildIndex != 0 ? activeSceneBuildIndex - 1 : activeSceneBuildIndex + 1);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement