Hi
Right now, I’m developing iOS game with Unity 2D Platformer.
I would like to know how to switch between active scenes properly. Just like UITabBar in iOS
What I did :
-
Create “VeryFirstScene” which has main UI with four buttons to change scenes and set main UI “DontDestroyOnLoad”
-
Add listener to buttons to set each scene active.
-
Load four scenes with LoadSceneMode.Additive when game starts.
-
Click buttons to switch between added scenes.
My code :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class MainUIController : MonoBehaviour {
public Button characterButton;
public Button dessertButton;
public Button ovenButton;
public Button fridgeButton;
private Scene sceneToActive;
void Awake() {
DontDestroyOnLoad (transform.gameObject);
}
void Start () {
characterButton.onClick.AddListener (CharacterScene);
dessertButton.onClick.AddListener (DessertScene);
ovenButton.onClick.AddListener (OvenScene);
fridgeButton.onClick.AddListener (FridgeScene);
SceneManager.LoadScene ("CharacterScene", LoadSceneMode.Additive);
SceneManager.LoadScene ("DessertScene", LoadSceneMode.Additive);
SceneManager.LoadScene ("OvenScene", LoadSceneMode.Additive);
SceneManager.LoadScene ("FridgeScene", LoadSceneMode.Additive);
}
void CharacterScene (){
sceneToActive = SceneManager.GetSceneByName ("CharacterScene");
SceneManager.SetActiveScene (sceneToActive);
}
void DessertScene () {
sceneToActive = SceneManager.GetSceneByName ("DessertScene");
SceneManager.SetActiveScene (sceneToActive);
}
void OvenScene() {
sceneToActive = SceneManager.GetSceneByName ("OvenScene");
SceneManager.SetActiveScene (sceneToActive);
}
void FridgeScene() {
sceneToActive = SceneManager.GetSceneByName ("FridgeScene");
SceneManager.SetActiveScene (sceneToActive);
}
}
Result :
When game starts, it has scene hierarchy like this (bold one is active):
- VeryFirstScene
- CharacterScene
- DessertScene
- OvenScene
- FridgeScene
- DontDestroyOnLoad
It shows FridgeScene and Main UI which set to be “DontDestroyOnLoad”.
After I clicked CharacterScene button to set CharacterScene active,
scene hierarchy changed like this :
- VeryFirstScene
- CharacterScene
- DessertScene
- OvenScene
- FridgeScene
- DontDestroyOnLoad
and no changes in the game view. It shows FridgeScene and main UI.
How can I make active scene always on the top of other scenes so that hierarchy can change like this when I click the button :
- VeryFirstScene
- DessertScene
- OvenScene
- FridgeScene
- CharacterScene
- DontDestroyOnLoad
I’ve been googling for 3 days but I can’t find answer…