【发布时间】:2019-12-17 19:44:31
【问题描述】:
我一直在尝试在我的游戏中实现保存游戏功能。当我从保存中加载游戏时,它不会加载玩家的位置。
我有 2 个主要场景:游戏发生的场景和主菜单场景。主菜单使用了我的加载功能,它应该读取我的保存文件并将播放器放在给定位置,但是,它只是在默认位置加载场景。没有错误被抛出,没有警告消息。这是我所有的代码:
这是我的存档系统:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
public static class SaveSystem
{
public static void SavePlayer (Player player)
{
BinaryFormatter formatter = new BinaryFormatter();
string path = Application.persistentDataPath + "player.fun";
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.fun";
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 in " + path);
return null;
}
}
}
玩家数据的容器:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class PlayerData
{
public int level;
public int health;
public float[] position;
public int stamina;
public PlayerData (Player player)
{
level = player.level;
health = player.health;
stamina = player.stamina;
position = new float[3];
position[0] = player.transform.position.x;
position[1] = player.transform.position.y;
position[2] = player.transform.position.z;
}
}
我的场景更改脚本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class SceneChanger : MonoBehaviour
{
public static bool Isload = false;
public void gotoWelwardLoad()
{
SceneManager.LoadScene("Welward");
bool Isload = true;
}
public void gotoWelward()
{
SceneManager.LoadScene("Welward");
}
public void gotomainmenu()
{
SceneManager.LoadScene("Menu");
}
}
我的播放器脚本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class Player : MonoBehaviour
{
public int health = 1;
public int stamina = 1;
public int level = 1;
public void SavePlayer ()
{
SaveSystem.SavePlayer(this);
}
public void LoadPlayer ()
{
SceneManager.LoadScene("Welward");
PlayerData data = SaveSystem.LoadPlayer();
level = data.level;
health = data.health;
stamina = data.stamina;
Vector3 position;
position.x = data.position[0];
position.y = data.position[1];
position.z = data.position[2];
transform.position = position;
}
public static void gotomenu ()
{
SceneManager.LoadScene("Menu");
}
public static void Welward()
{
SceneManager.LoadScene("Welward");
SaveSystem.LoadPlayer();
}
}
与完整的统一项目文件的链接: https://drive.google.com/open?id=1mFH5aNklC0qMWeJjMT4KD0CbTTx65VRp
【问题讨论】:
-
如果您的问题描述准确,几乎可以肯定该问题可以隔离到
SaveSystem.SavePlayer()或SaveSystem.LoadPlayer()。您很可能只是忘记保存或加载字段。 -
记住一个简单的好习惯,在共享 Unity 项目时省略上传 Library/Obj/Logs/Build 文件夹(与您的 .gitignore 相同的规则)
-
在您上传的项目中缺少字体(因此菜单上没有文字)。还缺少
ThirdPersonController预制件,如何在游戏中移动以测试保存/加载行为? -
同时,您的场景层次结构缺乏组织。有很多Button、GameObject、GameObject(1)等东西。考虑命名它们以保持井井有条。例如LoadButton、SaveButton等
-
@RobertHarvey “您很可能只是忘记保存或加载字段”是什么意思?