【发布时间】:2019-03-16 14:38:43
【问题描述】:
我需要创建一个类(或多个类,如果需要),每次用户单击“下一步按钮”时随机加载关卡,一旦所有关卡都加载完毕,我们就会停止加载并关闭应用程序。我设置了代码,但仍然没有得到我正在寻找的结果:
用户点击按钮。
加载随机关卡
这些级别被存储在一个数组列表中
一旦用户完成该级别,他/她就会按下“加载下一个级别”按钮
加载下一个随机关卡
但首先,我们检查随机级别是否与之前不同。
如果不是,那么我们重复步骤 2-5,否则我们转到步骤 8
如果所有关卡都被访问过,那么我们退出应用程序
我遇到的问题是每次我点击播放时我的游戏都会加载相同的级别,并且在我完成当前场景后它不会进入下一个场景。这是我目前所拥有的:
using UnityEngine;
using System.Collections;
[ExecuteInEditMode]
public class SceneManager : MonoBehaviour
{
public static bool userClickedNextButton; //This flag is raised by the other classes that have the GUI button logic
protected const int MAX = 2;
private ArrayList scenesWereAlreadyLoaded = new ArrayList();
void Update()
{
if (userClickedNextButton)
{
//by default the game starts at 0 so I want to be able to
//randomly call the next two scenes in my game. There will
//be more levels but for now I am just testing two
int sceneToLoad = Random.Range(1, 2);
if (!scenesWereAlreadyLoaded.Contains(sceneToLoad))
{
scenesWereAlreadyLoaded.Add(sceneToLoad);
Application.LoadLevel(sceneToLoad);
}
userClickedNextButton = false;
}
if (scenesWereAlreadyLoaded.Count > MAX) { Application.Quit(); }
}
}
【问题讨论】: