对于一个小型视频游戏使用unity,更改级别在引擎中工作完全正常,但是当我将它导出到.exe时它就不起作用了。游戏本身在.exe中运行良好,只有级别更改不起作用
public KeyCode nextLevelKey;
if(Input.GetKeyDown(nextLevelKey) && gameOver == true)
{
SceneManager.LoadScene(nextLevel.name);
Debug.Log("loaded next level");
}
有关导出时为什么不起作用的任何想法?在制作构建时,我按照正确的顺序包含了每个场景,所以不是这样。
好的,我看了一下团结答案,发现了这个:
https://answers.unity.com/questions/139598/setting-variable-as-type-quotscenequot.html,还有:
https://answers.unity.com/questions/205742/adding-scene-to-build.html
编辑方式:人们在第二个链接中尝试过的东西我猜是使用自定义编辑器脚本!你可以保持你的Object nextLevel字段也添加字符串nextLevelName;然后使用OnValidate()或其他一些Editor事件,您可以在nextLevel的值更改时设置nextLevelName值,并改为使用SceneManager.Load(nextLevelName)。
有点方便:尝试将该字段转换为字符串,将两次(或更多)更新场景名称更加痛苦,但无论如何都需要修复。稍微平滑一点,你可以试试这样,虽然我不确定你想要将nextLevel作为变量,但你可以尝试:
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex +1)
或者你可以有一个Scenes类,它提供了一种处理nextLevel问题的方法。也许用令牌?也许是其他的。
public class Scenes
{
public static string[,] scenes = new string[,]{
{"first_level", "scene-1-name"},
{"level_2", "scene-2-name"},
{"level_3", "factory-level"},
{"level_4", "scene-4-name"},
{"level_5", "jungle"},
};
public static string GetSceneNameByToken(string token)
{
for (var i = 0; i < scenes.GetLength(0); i++)
if (scenes[i, 0] == token)
return scenes[i, 1];
return null;
}
}
我个人喜欢第二种方法第一种方法仍然有点不可靠的imo(特别是如果你要在项目中间或下一个项目中改变统一版本)也许这是因为我没有使用编辑器事件很多idk。
好的,所以我明白了。我没有将nextLevel变量设为Object,而是将其设为字符串,然后在对象中(连接更改级别的代码)我输入了级别名称。