我需要创建一个类(或类,如果需要),每次用户单击“下一个按钮”时随机加载级别,并且一旦加载了所有级别,我们就会停止加载并关闭应用程序。我设置了代码,但我仍然没有得到我想要的结果:
用户点击按钮。
加载随机级别
该级别存储在数组列表中
一旦用户完成该级别,他/她按下“加载下一级”按钮
加载下一个随机级别
但首先,我们检查随机等级是否与以前不同。
如果不是,那么我们重复步骤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(); }
}
}
答案 0 :(得分:2)
根据Unity3D文档(http://docs.unity3d.com/Documentation/ScriptReference/Random.Range.html),range返回min(包含)和max(不包括)之间的整数,因此,在您的情况下,Random.Range(1,2)将始终返回1。
试试这个
int = sceneToLoad = Random.Range(1,3)
答案 1 :(得分:2)
您创建一个包含您的关卡编号的列表,然后删除当前加载的关卡。你重复一遍,直到列表为空。
同样不要使用ArrayList
,它是非常陈旧和不推荐使用的类型,从.NET / Mono拥有Generic支持之前的年龄开始。最好使用Generic List<T>
,它类型安全且速度比ArrayList
快。
using UnityEngine;
using System.Collections;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
[ExecuteInEditMode]
public class SceneManager : MonoBehaviour
{
// not best idea to have it static
public static bool userClickedNextButton;
protected const int MAX = 50;
private List<int> scenes = new List<int>();
void Start() {
// Initialize the list with levels
scenes = new List<int>(Enumerable.Range(1,MAX)); // This creates a list with values from 1 to 50
}
void Update()
{
if (userClickedNextButton)
{
if(scenes.Count == 0) {
// No scenes left, quit
Application.Quit();
}
// Get a random index from the list of remaining level
int randomIndex = Random.Range(0, scenes.Count);
int level = scenes[randomIndex];
scenes.RemoveAt(randomIndex); // Removes the level from the list
Application.LoadLevel(level);
userClickedNextButton = false;
}
}
}
答案 2 :(得分:1)
有一种更简单的方法可以做到这一点。您可以使用随机唯一编号准备数组。如果要加载新级别,只需增加数组的索引即可。 这是一个可以提供帮助的代码:(将此脚本附加到第一个场景中的空游戏对象)
using UnityEngine;
using System.Collections;
public class MoveToRandomScene : MonoBehaviour {
public Texture nextButtonTexture; // set this in the inspector
public static int[] arrScenes;
public static int index;
void Start () {
index = 0;
arrScenes = GenerateUniqueRandom (10, 0, 10); //assuming you have 10 levels starting from 0.
}
void OnGUI {
// Load the first element of the array
if (GUI.Button(new Rect (0,0,Screen.width/4,Screen.height/4),nextButtonTexture))
{
int level = arrScenes [0] ;
Application.LoadLevel (level);
}
}
//Generate unique numbers (levels) in an array
public int[] GenerateUniqueRandom(int amount, int min, int max)
{
int[] arr = new int[amount];
for (int i = 0; i < amount; i++)
{
bool done = false;
while (!done)
{
int num = Random.Range(min, max);
int j = 0;
for (j = 0; j < i; j++)
{
if (num == arr[j])
{
break;
}
}
if (j == i)
{
arr[i] = num;
done = true;
}
}
}
return arr;
}
}
对于其他场景,您只需创建此脚本并在需要加载新的随机场景时将其附加到空游戏对象:
void OnGUI {
if (GUI.Button(new Rect (0,0,Screen.width/4,Screen.height/4),nextButtonTexture))
{
if (MoveToRandomScene.index == 9) {
// Load again your first level
Application.LoadLevel(0);
}
// else you continue loading random levels
else {
MoveToRandomScene.index++;
int level = MoveToRandomScene.arrScenes[MoveToRandomScene.index];
Application.LoadLevel(level);
}
}
}