Hi, I’ve followed this video
to create a Scene Manager.
I created a Canvas prefab which makes the transitions between scenes and I added it in my main menu scene (which has another canvas that handles the menu) and I’ve attached to it the next script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class SceneController : MonoBehaviour {
public static SceneController MyInstance;
//It's a static instance thnt won't be destroyed over scenes
public float TransitionTimeDelay=0.1f;
public string TargetScene;
CanvasGroup MyCanvasGroup;
bool IsAddictive;
// Use this for initialization
void Awake () {
//Check if we have duplicated instances of the same class and in case we do, delete one
if (MyInstance == null) {
MyInstance = this;
DontDestroyOnLoad (gameObject); //The gameObject is the Canvas
MyCanvasGroup=GetComponent<CanvasGroup>();
} else
Destroy (gameObject);
}
// Update is called once per frame
void Update () {
//That's only for test
if (Input.GetKey (KeyCode.Q))
SetTargetScene ("Scena", false);
}
public void SetTargetScene(string SceneToLoad, bool Addictive)
{
//If we're pointing to the same scene, quit the function
if (SceneManager.GetActiveScene ().name == SceneToLoad)
return;
TargetScene = SceneToLoad;
IsAddictive = Addictive;
StopCoroutine ("TransitionWithFade");
StartCoroutine ("TransitionWithFade", 1);
//Set the target alpha to 1, so to be completely opaque
}
//Coroutine that operates while loading the next scene
IEnumerator TransitionWithFade(float targetAlpha)
{
float AlphaDifference = Mathf.Abs (MyCanvasGroup.alpha - targetAlpha);
float TransitionRate = 0;
while (AlphaDifference > 0.045f) {
//Smoothen the transition between the two scenes.
//Syntaxis: current alpha ! target alpha | current velocity | transition time
MyCanvasGroup.alpha = Mathf.SmoothDamp (MyCanvasGroup.alpha, targetAlpha, ref TransitionRate, TransitionRate);
AlphaDifference = MyCanvasGroup.alpha - targetAlpha; //Update Alpha difference between scenes/image
yield return null; //Interrups coroutine
}
MyCanvasGroup.alpha = targetAlpha;
//When MyCanvasGroup.alpha is 1 (fully opaque), it means we have to move to the next scene, so we need to call the actual method doing that.
if (targetAlpha == 1) {
StartCoroutine ("LoadScene");
}
}
IEnumerator LoadScene()
{
if(IsAddictive==false)
SceneManager.LoadScene (TargetScene); //Load the correct scene
else
SceneManager.LoadScene (TargetScene, LoadSceneMode.Additive);
string ActiveScene=SceneManager.GetActiveScene().name; //Refresh the current scene
//If the scene is not loaded yet, so it means our current scene is not the target scene (because of slow PC performance, troubles etc...)
//Continue with the coroutine until the desired scene is completely loaded
while (ActiveScene != TargetScene) {
ActiveScene = SceneManager.GetActiveScene ().name;
yield return null;
}
StartCoroutine ("TransitionWithFade", 0); //When the scene is loaded, turn the canvas group alpha to be 0
}
}
I have some scenes in my assets and I will create new ones.
Unfortunately in the main scene where I have a “Play” button, if I add an OnClick event, I drag the canvas element that handles the scene management, I cannot see my “SetTargetScene method” even if it is public.
So I cannot set what scene to upload when pressing the button.
What can I do?
Thank you for any help