using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class BackToMainMenu : MonoBehaviour
{
public PlayerCameraMouseLook cammouselook;
// Update is called once per frame
void Update ()
{
if (Input.GetKeyDown(KeyCode.Escape))
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex - 1);
PlayerCameraMouseLook.mouseLookEnable = false;
cammouselook.enabled = true;
}
}
}
Now when pressing the escape key it's loading the main menu scene. And in the main menu I have NEW GAME button but not RESUME.
Instead making a resume button I want that pressing the escape key again when the main menu scene is loaded it will return back to the game to the current position it is. Either if it's in a middle of a cutscene or just idle in the game.
So when pressing the escape key again it will back to the game and continue from the last point.
Another sub question : Should I use : LoadSceneMode.Additive ? Or when switching between the game play scene and the main menu it should remove the current active scene and then load the next one ?
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex - 1, LoadSceneMode.Additive);
The main menu scene is at index 0 the game scene at index 1.
What I tried :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class BackToMainMenu : MonoBehaviour
{
// Variables
private bool _isInMainMenu = false;
public GameObject mainGame;
public PlayerCameraMouseLook cammouselook;
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Escape))
{
if (!_isInMainMenu)
{
SceneManager.LoadScene(0, LoadSceneMode.Additive);
PlayerCameraMouseLook.mouseLookEnable = false;
cammouselook.enabled = true;
// -- Code to freeze the game
mainGame.SetActive(false);
}
else
{
SceneManager.UnloadSceneAsync(0);
// -- Code to unfreeze the game
mainGame.SetActive(true);
}
_isInMainMenu = !_isInMainMenu;
}
}
}
And the Hierarchy :
The script is attached to the Back to main menu gameobject. And all the game objects are under Main Game.
First time when pressing the escape key it's loading to the main menu and main menu scene to the hierarchy. Second time pressing on the escape key it's starting the game over like a new game and removing unloading the main menu.
In both cases the game scene is stay in the hierarchy.