简体   繁体   English

如何从另一个场景中的脚本访问保存/加载功能?

[英]How can I access the Save/Load functions from a script that is on another scene?

I have two scene : Main Menu, Game On the Main Menu I have also a two buttons : Save,Load我有两个场景:主菜单、游戏在主菜单上我还有两个按钮:保存、加载

And two scripts :和两个脚本:

Save System :保存系统:

using UnityEngine;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

public static class SaveSystem
{
    public static void SavePlayer(Player player)
    {
        BinaryFormatter formatter = new BinaryFormatter();
        string path = Application.persistentDataPath + "/player.bin";
        FileStream stream = new FileStream(path, FileMode.Create);

        PlayerData data = new PlayerData(player);

        formatter.Serialize(stream, data);
        stream.Close();
    }

    public static PlayerData LoadPlayer()
    {
        string path = Application.persistentDataPath + "/player.bin";
        if(File.Exists(path))
        {
            BinaryFormatter formatter = new BinaryFormatter();
            FileStream stream = new FileStream(path, FileMode.Open);

            PlayerData data = formatter.Deserialize(stream) as PlayerData;
            stream.Close();

            return data;
        }
        else
        {
            Debug.LogError("Save file not found in " + path);
            return null;
        }
    }
}

And Player :和球员:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player : MonoBehaviour
{
   public void SavePlayer()
    {
        SaveSystem.SavePlayer(this);
    }

    public void LoadPlayer()
    {
        PlayerData data = SaveSystem.LoadPlayer();

        Vector3 position;
        position.x = data.position[0];
        position.y = data.position[1];
        position.z = data.position[2];
        transform.position = position;
    }
}

The Player script is attached to the Player in the Game scene but now I can't call or get access to the SavePlayer and LoadPlayer from the two buttons in the On Click events. Player 脚本附加到游戏场景中的 Player,但现在我无法从 On Click 事件中的两个按钮调用或访问 SavePlayer 和 LoadPlayer。

And another problem is that the Game scene is not all the time in the Hierarchy the Game scene is loaded when I click the Play button and then the Main Menu scene is not loaded and when I hit the escape key it's loading the Main Menu scene but then the Game scene is not loaded.另一个问题是游戏场景并不总是在层次结构中,当我单击播放按钮时,游戏场景被加载,然后主菜单场景没有加载,当我按下转义键时,它正在加载主菜单场景但是那么游戏场景没有加载。

So I wonder how should I and how can I make a reference between the buttons Save/Load and the functions SavePlayer/LoadPLayer ?所以我想知道我应该如何以及如何在按钮 Save/Load 和函数 SavePlayer/LoadPLayer 之间进行引用?

After we cleared out that your two scenes are actually not loaded at the same time I would use another approach.在我们确定您的两个场景实际上并未同时加载后,我将使用另一种方法。

You already have a static class for your loading process.您已经有一个用于加载过程的static类。 Either there or in a second one you could simply store the player data meanwhile still in the MainMenu.无论是在那里还是在第二个中,您都可以简单地将玩家数据同时存储在 MainMenu 中。 Then the player loads this data on start.然后播放器在开始时加载这些数据。

For the active loading and saving from within the game scene you could have another button for the saving.对于游戏场景中的主动加载和保存,您可以使用另一个按钮进行保存。

Something like就像是

public static class SaveSystem
{
    public static PlayerData Data;

    // Without a parameter serialize the values from the current Data
    // which will be updated by the player
    public static void SavePlayer()
    {
        WriteFile(Data);
    }

    public static void SavePlayer(Player player)
    {
        var data = new PlayerData(player);
        WriteFile(data);
    }

    private static void WriteFile(PlayerData data)
    {
        BinaryFormatter formatter = new BinaryFormatter();
        string path = Application.persistentDataPath + "/player.bin";
        FileStream stream = new FileStream(path, FileMode.Create);

        formatter.Serialize(stream, data);
        stream.Close();
    }

    public static PlayerData LoadPlayer()
    {
        string path = Application.persistentDataPath + "/player.bin";
        if(File.Exists(path))
        {
            BinaryFormatter formatter = new BinaryFormatter();
            FileStream stream = new FileStream(path, FileMode.Open);

            Data = formatter.Deserialize(stream) as PlayerData;
            stream.Close();

            return Data;
        }
        else
        {
            Debug.LogError("Save file not found in " + path);
            return null;
        }
    }
}

Then from the menu然后从菜单

public class MenuButtons : MonoBehaviour
{
    public void Load()
    {
        // Only reads the save file into the SaveSystem.Data
        SaveSystem.LoadPlayer();
    }

    public void Save()
    {
        // Only write SaveSystem.Data to the file
        SaveSystem.SavePlayer();
    }
}

Now in the game scene on the player现在在玩家的游戏场景中

public class Player : MonoBehaviour
{
    // Load the player values on startup from SaveSystem.Data
    private void Awake ()
    {
        var data = SaveSystem.Data;

        if(data == null) return;

        Vector3 position;
        position.x = data.position[0];
        position.y = data.position[1];
        position.z = data.position[2];
        transform.position = position;
    }

    // Now it is up to you when and how often you want the player to save its data
    // I would do it e.g. in OnDestroy
    // so everytime the scene is changed
    private void OnDestroy ()
    {
        // Only populates the SaveSystem.Data
        // without necessarily writing the file
        SaveSystem.Data = new PlayerData(this);
    }
}

暂无
暂无

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

相关问题 如何从另一个场景脚本访问场景 - How can I access a scene from another scenes script 如何从一个脚本访问布尔值到另一个脚本,以便在多个场景中加载? - How to access a bool from one script to another so I can load in multiple scenes? 如何从另一个脚本访问脚本中的枚举? - How can I get access of a enum in a script from another script? 如何从 Unity 中的另一个脚本访问变量? - How can I access a variable from another script in Unity? 如何从另一个场景中获取游戏对象的参考? - How can I get a reference for a gameobject from another scene? 重新实现场景不会从另一个脚本加载变量的内容 - Realoding an scene doesn't load the content of a variable from another script 我想编写一个脚本,从中我可以将 name 变量用于 unity 2d 中的另一个场景 - I want to write a script from which I can use the name variable into another scene in unity 2d 如何将我的角色从一个场景收集的硬币保存到另一个场景? (统一) - How do i save the coins my character has collected from one scene to another? (Unity) 如果两个脚本都未附加到任何GameObject,如何从另一个脚本访问脚本? - How can I access a script from another script if both scripts not attached to any GameObject? 直接播放场景时,场景保存和加载工作正常,但是当我从主菜单加载时,它是 null 异常 - Scene save&load works fine when playing scene directly,but when i load from main menu it's null exception
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM