// Function to be called to fade to another scene using the scene's name // Note: this only works if the scene has been added to the project's Build Settings public void FadeToScene(string sceneName) { // Create a new TempFader GameObject, which will take care of the fade animation and then destroy itself. TempFader tempFader = TempFader.CreateTempFader(fadeColor, duration, delay); if (tempFader != null) { StartCoroutine(tempFader.FadeToSceneCR(() => { SceneManager.LoadScene(sceneName); })); } }
void Awake() { // If there is already another TempFader components, this one is not needed. // Destroy it. This way, there are never multiple TempFader components present in the scene. if (instance != null) { Destroy(gameObject); return; } instance = this; }
void Start() { // If this is the main FadeInOnStart component, start the fade in if (this == instance) { // Create a new TempFader GameObject, which will take care of the fade animation and then destroy itself TempFader tempFader = TempFader.CreateTempFader(fadeColor, duration, delay); if (tempFader != null) { StartCoroutine(tempFader.FadeInCurrentSceneCR()); } } }
// Helper function to create a TempFader GameObject public static TempFader CreateTempFader(Color color, float duration, float delay) { var go = new GameObject("TempFader"); TempFader tempFader = go.AddComponent <TempFader> (); if (tempFader == null) { return(null); } else { tempFader.SetProperties(color, duration, delay); return(tempFader); } }