在Unity中,我通过单击设备Android后退按钮设法返回到最后一个场景,但是问题是当我在第三个场景中时,我想回到第二个,然后回到第一个场景。
从第3场景到第2场景,当我单击“后退”按钮时,它成功运行,但是当我再次从第2场景到第1场景按后退按钮时,它没有运行。后退按钮似乎仅在第一次使用。
这是我的C#代码:
public class SceneLoader : MonoBehaviour
{
private float seconds = 0.5f;
private static int lastScene;
private int currentScene;
private void Awake()
{
currentScene = SceneManager.GetActiveScene().buildIndex;
}
private void Update()
{
BackButtonPressed();
}
public void LoadNextScene(int numberOfSceneToLoad)
{
StartCoroutine(LoadScene(numberOfSceneToLoad));
}
private IEnumerator LoadScene(int numberOfScene)
{
currentScene = SceneManager.GetActiveScene().buildIndex;
SetLastScene(currentScene);
yield return new WaitForSeconds(seconds);
SceneManager.LoadScene(numberOfScene);
}
private void BackButtonPressed()
{
if (Input.GetKey(KeyCode.Escape))
{
Debug.Log("Current scene: " + currentScene);
Debug.Log("Last Scene (scene to load): " + GetLastScene());
SceneManager.LoadScene(GetLastScene());
currentScene = GetLastScene();
Debug.Log("Now the Current scene is: " + currentScene);
}
}
public static void SetLastScene(int currentSceneToLastScene)
{
lastScene = currentSceneToLastScene;
}
public static int GetLastScene()
{
return lastScene;
}
public void QuitGame()
{
Application.Quit();
}
}
谢谢。
答案 0 :(得分:3)
返回BackToLastScene时,您设置了currentScene = GetLastScene();
,但从不更改lastScene
的值。
我建议您使用Stack
中的System.Collections
之类的LIFO数据结构来跟踪场景。
这是一个示例伪代码:
define scene_stack;
function LoadNewScene(new_scene){
current_scene = GetCurrentScene();
scene_stack.Push(current_scene);
LoadScene(new_scene);
}
function LoadOldScene(){
old_scene = scene_stack.Pop();
LoadScene(old_scene);
}
答案 1 :(得分:1)
在@Damiano的帮助下,我像这样更改了我的代码,它可以正常工作:
public class SceneLoader : MonoBehaviour
{
private float secondsToLoadNextScene = 0.5f;
private static int lastScene;
private int mainScene = 1;
private int currentScene;
public static Stack<int> sceneStack = new Stack<int>();
private void Awake()
{
currentScene = SceneManager.GetActiveScene().buildIndex;
}
private void Update()
{
BackButtonPressed();
}
public void LoadNextScene(int numberOfSceneToLoad)
{
StartCoroutine(LoadScene(numberOfSceneToLoad));
}
private IEnumerator LoadScene(int numberOfScene)
{
SetLastScene(currentScene);
yield return new WaitForSeconds(secondsToLoadNextScene);
LoadNewScene(numberOfScene);
}
public void BackButtonPressed()
{
if (Input.GetKey(KeyCode.Escape) && currentScene > mainScene)
{
if (lastScene == 0)
{
Debug.Log("Last scene was Splash Screen so load Main Scene instead.");
SceneManager.LoadScene(mainScene);
}
else
{
LoadLastScene();
}
}
}
public void LoadNewScene(int sceneToLoad)
{
currentScene = SceneManager.GetActiveScene().buildIndex;
sceneStack.Push(currentScene);
SceneManager.LoadScene(sceneToLoad);
}
public void LoadLastScene()
{
lastScene = sceneStack.Pop();
SceneManager.LoadScene(lastScene);
}
public static void SetLastScene(int makeCurrentSceneTheLastScene)
{
lastScene = makeCurrentSceneTheLastScene;
}
public static int GetLastScene()
{
return lastScene;
}
}
答案 2 :(得分:1)
这对我有用:
public class ChangeScene : MonoBehaviour
{
public static Stack<int> scenes = new Stack<int>();
int numScenes = scenes.Count;
public void loadScene(int newScene)
{
scenes.Push(newScene);
SceneManager.LoadScene(newScene);
}
public void previousScene()
{
if (numScenes != 0 && numScenes != 1)
{
scenes.Pop();
SceneManager.LoadScene(scenes.Peek());
}
else if (numScenes == 1)
{
//go back to main Scene
loadScene(0);
}
}
}
答案 3 :(得分:0)
我花了很多时间搜索这个主题,所以我想到了这个实现,因为我发现大多数解决方案都很复杂。我来自本地android背景,因此我实现了本地android所做的工作,即使用堆栈进行打开的活动。在主屏幕中按返回按钮时,此代码也会退出应用程序。
public class navigation : MonoBehaviour
{
public static Stack<string> scenes = new Stack<string>();
void Start()
{
if (scenes.Count == 0)
{
scenes.Push(SceneManager.GetActiveScene().name);
}
}
void Update()
{
previousScene();
}
public void previousScene()
{
if (Input.GetKeyDown(KeyCode.Escape))
{
if (scenes.Count == 1)
{
Application.Quit();
}
else
{
scenes.Pop();
string sceneToBuild = scenes.Pop();
loadScene (sceneToBuild);
}
}
}
public void loadScene(string newScene)
{
scenes.Push (newScene);
SceneManager.LoadScene (newScene);
}
}