Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Unity-加载-解锁级别并检查是否存在下一个级别_C#_Unity3d - Fatal编程技术网

C# Unity-加载-解锁级别并检查是否存在下一个级别

C# Unity-加载-解锁级别并检查是否存在下一个级别,c#,unity3d,C#,Unity3d,我正在制作类似《愤怒的小鸟》的小游戏,因为我对这两款游戏都是新手 和C#。如果所有敌人都死了,我想加载并解锁下一个关卡,并检查新关卡是否存在 (如果不是)返回到标高选择场景 这就是我所尝试的: LevelScript using UnityEngine; using UnityEngine.SceneManagement; public class LevelScript : MonoBehaviour { [SerializeField] string _nextLevelName; M

我正在制作类似《愤怒的小鸟》的小游戏,因为我对这两款游戏都是新手 和C#。如果所有敌人都死了,我想加载并解锁下一个关卡,并检查新关卡是否存在 (如果不是)返回到标高选择场景

这就是我所尝试的:

LevelScript

using UnityEngine;
using UnityEngine.SceneManagement;

public class LevelScript : MonoBehaviour
{

[SerializeField] string _nextLevelName;

Monster[] _monsters;




void OnEnable()
{
    _monsters = FindObjectsOfType<Monster>();
}


private void Update()
{
    int currentLevel = SceneManager.GetActiveScene().buildIndex ;
   

    if (currentLevel >= PlayerPrefs.GetInt("levelsUnlocked"))
    {
        PlayerPrefs.SetInt("levelsUnlocked", currentLevel );
    }

    if (MonsterAreAllDead())
    {

        GoToNextLevel();

    }

    Debug.Log("Level" + PlayerPrefs.GetInt("levelsUnlocked") + "UNLOCKED");
}






public void Pass()
{
    int currentLevel = SceneManager.GetActiveScene().buildIndex;

    if (currentLevel >= PlayerPrefs.GetInt("levelsUnlocked") )
    {
        PlayerPrefs.SetInt("levelsUnlocked", currentLevel + 1);
       
    };

}
    
bool MonsterAreAllDead()
{

    foreach (var monster in _monsters)
    {
        if (monster.gameObject.activeSelf)
            return false;

    }

    return true;
}


void GoToNextLevel()
{
    Debug.Log("Go to next level" + _nextLevelName);
    SceneManager.LoadScene(_nextLevelName);


}
使用UnityEngine;
使用UnityEngine.SceneManagement;
公共类LevelScript:MonoBehavior
{
[SerializeField]字符串_nextLevelName;
怪物[]_怪物;
void OnEnable()
{
_怪物=FindObjectsOfType();
}
私有void更新()
{
int currentLevel=SceneManager.GetActiveScene().buildIndex;
如果(currentLevel>=PlayerPrefs.GetInt(“levelsUnlocked”))
{
PlayerPrefs.SetInt(“levelsUnlocked”,currentLevel);
}
if(MonsterAreAllDead())
{
GoToNextLevel();
}
Debug.Log(“Level”+PlayerPrefs.GetInt(“levelsUnlocked”)+“UNLOCKED”);
}
公共通行证()
{
int currentLevel=SceneManager.GetActiveScene().buildIndex;
如果(currentLevel>=PlayerPrefs.GetInt(“levelsUnlocked”))
{
PlayerPrefs.SetInt(“levelsUnlocked”,当前级别+1);
};
}
布尔怪兽死了
{
foreach(怪物中的变量怪物)
{
if(monster.gameObject.activeSelf)
返回false;
}
返回true;
}
void GoToNextLevel()
{
Log(“转到下一级”+_nextLevelName);
SceneManager.LoadScene(_nextLevelName);
}
}

级别经理

 public class LevelManager : MonoBehaviour
{

    int levelsUnlocked;



    public Button[] buttons;

    
    void Start()
    {
        levelsUnlocked = PlayerPrefs.GetInt("levelsUnlocked", 1);
        
        for (int i = 0; i < buttons.Length; i++)
        {
            buttons[i].interactable = false;
        }


        for (int i = 0; i < levelsUnlocked; i++)
        {




            buttons[i].interactable = true;



    }
    }

    




public void LoadLevel(int levelIndex)
    {
        SceneManager.LoadScene(levelIndex);
    
    }
public类LevelManager:monobhavior
{
int-levelsUnlocked;
公共按钮[]按钮;
void Start()
{
levelsunlock=PlayerPrefs.GetInt(“levelsunlock”,1);
对于(int i=0;i
我得到了这两个脚本,我将画布、按钮和关卡脚本都附加到了我的关卡上

问题是每个关卡在开始和完成关卡后都会自动解锁 它想进入下一个还不存在的层次


请帮助我。如果我的问题很愚蠢,而且英语很差,请原谅。

你应该在你的LevelManager中制作一系列场景,以了解你的所有级别(顺序) 为了获得下一个级别,您可以获得阵列中实际场景的位置,并检查下一个场景

有人想

伪代码:

[Serialized]
Scene[] AllScenes

void GoToNextLevel()
{
    int currentLevelPos = AllScenes.IndexOf(currentScene);
    if (AllScenes[currentLevelPos + 1] != null)
          Load Next Level
    else
         Go To Level Menu
}

你能解释一下在IndexOf()中放什么吗?