简体   繁体   中英

Unity - How to go to the previous scene

So I'm making a game and I made a Store in it, and I need a script to put in a button in the store scene, that go to the previous loaded scene. For example, I completed the level one and now I'm in the level selection menu, I want to go to the store so I click in “store” button. Now, in the store, I want to go back, so I click in the “back” button and instead of going back to the level selection menu, I go to the start menu, so I need a script to put on the “back” button that brings me to the previous loaded scene. Can you help me?

put this in any class

static List<string> sceneList = new List<string>();

when you load a new scene, right before you call sceneManager

<classyouputscenelistin>.sceneList.Add("<scenename>");

when you go back

SceneManager.LoadScene(<classyouputscenelistin>.sceneList[<classyouputscenelistin.sceneList.Count -2], LoadSceneMode.Single);

replace to the class name you added scene list to

replace with the name of the scene you are loading

edit: go back to scene before last

SceneManager.LoadScene(<classyouputscenelistin>.sceneList[<classyouputscenelistin.sceneList.Count -3], LoadSceneMode.Single);

Basically Count - 2 is previous scene anything before that (-3, -4) load the scenes before it, if you don't want a scene to be tracked in this list don't do this:

<classyouputscenelistin>.sceneList.Add("<scenename>");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM