当我点击"Credits“按钮时,6号场景就被加载了。当我点击场景6中的后退按钮时,应用程序应该返回到之前加载的场景。例如,当前我在场景3中,当我点击credits按钮时,当前场景(在本例中为3)将被保存在变量currentLevel中。当我的应用程序在场景6中,按下后退按钮时,它应该返回到3,但它却返回到场景0。我不知道为什么它不能工作。任何帮助都是非常感谢的。谢谢。
int currentLevel;
void OnMouseDown()
{
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
RaycastHit2D hit = Physics2D.Raycast (ray.origin, ray.direction);
if (hit.collider != null) {
if(hit.collider.name == "Credits")
{
currentLevel = Application.loadedLevel;
Debug.Log (currentLevel);
Application.LoadLevel(6);
}
if(hit.collider.name == "BackButton")
{ Debug.Log ("Current level" + currentLevel);
Application.LoadLevel(currentLevel);
}
}
}发布于 2015-08-16 15:17:35
您需要一个管理器/单例来维护currentLevel字段的状态。
因此,创建一个新脚本并将以下代码粘贴到其中。
public class LevelManager : MonoBehaviour {
public static LevelManager instance;
public int currentLevel = 0;
void Awake () {
instance = this;
DontDestroyOnLoad(gameObject);
}
}现在,在您的脚本中,将代码更改为
LevelManager.instance.currentLevel = Application.loadedLevel;加载您的新场景时,
Application.LoadLevel(LevelManager.instance.currentLevel);同时单击后退按钮。
https://stackoverflow.com/questions/32028560
复制相似问题