简体   繁体   English

导出游戏时场景不会改变,即使它在引擎中也是如此

[英]Scene not changing when the game is exported, even though it does in the engine

Using unity for a small video game, changing levels works in the engine completely fine, but when I export it to .exe it just doesn't work. 对于一个小型视频游戏使用unity,更改级别在引擎中工作完全正常,但是当我将它导出到.exe时它就不起作用了。 The game itself works fine in .exe, only level changing doesn't work 游戏本身在.exe中运行良好,只有级别更改不起作用

public KeyCode nextLevelKey;

if(Input.GetKeyDown(nextLevelKey) && gameOver == true)
    {
        SceneManager.LoadScene(nextLevel.name);
        Debug.Log("loaded next level");
    }

Any ideas on why it doesn't work when exported? 有关导出时为什么不起作用的任何想法? I have included every scene in the right order when making the build so it's not that. 在制作构建时,我按照正确的顺序包含了每个场景,所以不是这样。

Ok so i took a look at unity answers and found this: 好的,我看了一下团结答案,发现了这个:

https://answers.unity.com/questions/139598/setting-variable-as-type-quotscenequot.html and also this: https://answers.unity.com/questions/139598/setting-variable-as-type-quotscenequot.html以及:

https://answers.unity.com/questions/205742/adding-scene-to-build.html https://answers.unity.com/questions/205742/adding-scene-to-build.html

Editor Way: What people have tried in 2nd link i guess is with custom editor scripts !That you can keep your Object nextLevel field also add string nextLevelName; 编辑方式:人们在第二个链接中尝试过的东西我猜是使用自定义编辑器脚本!你可以保持你的Object nextLevel字段也添加字符串nextLevelName; And then using OnValidate() or some other Editor event you can set nextLevelName value whenever nextLevel 's value changes and use SceneManager.Load(nextLevelName) instead. 然后使用OnValidate()或其他一些Editor事件,您可以在nextLevel的值更改时设置nextLevelName值,并改为使用SceneManager.Load(nextLevelName)。

Kinda Easier Way : Try converting that field to string it's a pain to update scene names twice (or maybe more) but fixes things anyways. 有点方便:尝试将该字段转换为字符串,将两次(或更多)更新场景名称更加痛苦,但无论如何都需要修复。 To Smooth things a bit you can try sth like this , though I'm not sure what you're trying to achieve with having nextLevel as variable but you can try: 稍微平滑一点,你可以试试这样,虽然我不确定你想要将nextLevel作为变量,但你可以尝试:

SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex +1)

Or you can have a Scenes class that provides a way of dealing with your nextLevel problem. 或者你可以有一个Scenes类,它提供了一种处理nextLevel问题的方法。 Maybe with tokens? 也许用令牌? Maybe sth else. 也许是其他的。

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

I Personally like the 2nd approach cos first one is still kinda unreliable imo (specially if you were to change unity version in middle of project or for next projects) maybe that's cos I've not worked with Editor Events much idk. 我个人喜欢第二种方法第一种方法仍然有点不可靠的imo(特别是如果你要在项目中间或下一个项目中改变统一版本)也许这是因为我没有使用编辑器事件很多idk。

Ok, so I figured it out. 好的,所以我明白了。 Instead of making the nextLevel variable an Object I made it a string, and then in the object (that the code for changing levels is attached to) I entered the level name. 我没有将nextLevel变量设为Object,而是将其设为字符串,然后在对象中(连接更改级别的代码)我输入了级别名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM