简体   繁体   English

从最后一个场景开始游戏Unity 3D

[英]Start Game from the Last Scene Unity 3D

After closing the game and reopening it, I want to load the last scene, so the player can continue from the level he reached. 关闭游戏并重新打开后,我想加载最后一个场景,以便玩家可以从达到的水平继续。 I've tried using PlayerPrefs as you see in the code bellow, but the game crash on startup. 正如您在下面的代码中所见,我已经尝试使用PlayerPrefs,但是游戏在启动时崩溃。

GameManager Script: GameManager脚本:

    bool GameHasEnded = false;
    public float RestartDelay = 2f;
    public float NextLevelDelay = 5f;

    int level_index;

    private void Start()
    {

        level_index = PlayerPrefs.GetInt("Last_Level");
        SceneManager.LoadScene(level_index);


    }

    public void CompleteLevel()
    {
        Invoke("NextLevel", NextLevelDelay);
        level_index = level_index++;
        PlayerPrefs.SetInt("Last_Level", level_index);
    }

    public void EndGame()
    {
        if (GameHasEnded == false)
        {
            GameHasEnded = true;
            Invoke("Restart", RestartDelay);
        }
    }

    void NextLevel()
    {
        SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex +1);
    }

    void Restart()
    {
        SceneManager.LoadScene(SceneManager.GetActiveScene().path);
    }
}

All scenes are linked with GameManager , they all have the same code that load the next scene: 所有场景都与GameManager链接,它们都具有加载下一个场景的相同代码:

FindObjectOfType<GameManager>().CompleteLevel();

You need to specify a defaultValue for GetInt so that the first time it starts up, it can pick the appropriate scene to start at. 您需要为GetInt指定一个defaultValue ,以便它第一次启动时可以选择适当的场景开始。

Also, you need to track whether or not you have already loaded at scene start, and only do this load if it hasn't yet happened. 另外,您需要跟踪场景开始时是否已经加载,并且仅在尚未发生时才执行此加载。 GameManager seems like it should be made into a singleton but since it's not, you can just make this loaded flag static, so that is common among all GameManager instances. GameManager似乎应该将它做成一个单例,但是由于不是这样,您可以将这个loaded标志GameManager静态,因此这在所有GameManager实例中都很常见。

Altogether, this might look like this: 总共看起来可能像这样:

public readonly int defaultLastLevel = 1; // Set as appropriate
private static bool loaded = false;

void Start()
{
    if (!loaded) {
        loaded = true;
        level_index = PlayerPrefs.GetInt("Last_Level", defaultLastLevel);
        SceneManager.LoadScene(level_index);
    }
}

Also, your CompleteLevel has some very strange code where you post-increment and assign in the same line: 另外,您的CompleteLevel具有一些非常奇怪的代码,您可以在其中进行后递增并在同一行中进行分配:

level_index = level_index++;

This is extremely hard to read and actually doesn't do what you want it to do . 这是很难读懂的,实际上并没有做你想做的事情 You can do level_index = SceneManager.GetActiveScene().buildIndex + 1; 您可以执行level_index = SceneManager.GetActiveScene().buildIndex + 1; instead. 代替。

Also, you need to Save changes you make to PlayerPrefs with PlayerPrefs.Save(); 另外,还需要使用PlayerPrefs.Save(); SavePlayerPrefs PlayerPrefs.Save();更改PlayerPrefs.Save();

Altogether, this makes your CompleteLevel look like this: 总之,这使您的CompleteLevel看起来像这样:

public void CompleteLevel()
{
    Invoke("NextLevel", NextLevelDelay);
    level_index = SceneManager.GetActiveScene().buildIndex + 1; // use this instead
    PlayerPrefs.SetInt("Last_Level", level_index);
    PlayerPrefs.Save();
}

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

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