在 Unity-3D 中随机加载不同关卡时出现问题

本文关键字:问题 Unity-3D 随机 加载 | 更新日期: 2024-10-26 06:05:46

我需要创建一个类(如果需要,则一个或多个类),每次用户单击"下一步按钮"时都会随机加载关卡,一旦加载了所有关卡,我们就会停止加载并关闭应用程序。我设置了代码,但仍然没有得到我正在寻找的结果,即:

  1. 用户单击按钮。

  2. 加载随机关卡

  3. 该级别存储在数组列表中

  4. 用户完成该级别后,他/她按下"加载下一级"按钮

  5. 加载下一个随机级别

  6. 但首先,我们检查随机水平是否与以前不同。

  7. 如果不是,那么我们重复步骤 2-5,否则我们转到步骤 8

  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(); }
    }
}

在 Unity-3D 中随机加载不同关卡时出现问题

使用级别编号创建一个列表,然后删除当前加载的级别。重复此操作,直到列表为空。

也不要使用ArrayList,它是非常古老且已弃用的类型,从 .NET/Mono 具有泛型支持之前的时代开始。最好使用泛型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;
        }
    }
}

根据 Unity3D 文档 (http://docs.unity3d.com/Documentation/ScriptReference/Random.Range.html),range 返回一个介于最小(包含)和最大(不包括)之间的整数,因此,在您的情况下,Random.Range(1,2) 将始终返回 1。

试试这个

int = sceneToLoad = Random.Range(1,3)

有一种更简单的方法可以做到这一点。您可以准备一个具有随机唯一数字的数组。当您想要加载新级别时,只需递增数组的索引即可。这里有一个可以提供帮助的代码:(将此脚本附加到第一个场景中的空游戏对象)

  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);
    }
 }
}