点击返回键返回上个场景源码
using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement; public class SceneManagers : MonoBehaviour { public static SceneManagers Singleton; private static List<string> LastScene = new List<string>(); void Awake() { Singleton = this; } public void LoadScene(string Str) { LastScene.Add(SceneManager.GetActiveScene().name);//保存当前场景名字 SceneManager.LoadScene(Str);//加载新场景 } public void LoadLastScene() { if (LastScene.Count > 0) { SceneManager.LoadScene(LastScene[LastScene.Count - 1]); LastScene.RemoveAt(LastScene.Count - 1); } else { Debug.Log("已是最初场景,无法后退"); } } }