[英]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.