这段代码的结构是为了实现 数据的封装和管理,特别是在 Unity 中保存和加载玩家数据时。以下是对代码设计的逐步解释:
1. PlayerCoin
类
PlayerCoin
是一个简单的数据类,用于表示单个玩家的硬币信息。它包含以下字段:
count
:玩家拥有的硬币数量。name
:玩家的名字。isWinner
:表示玩家是否获胜。
[System.Serializable]
:这个属性告诉 Unity 这个类可以被序列化(即将其数据转换为 JSON 或其他格式)。这是 Unity 中保存数据的必要步骤。
2. PlayerCoinList
类
PlayerCoinList
是一个容器类,用于管理多个 PlayerCoin
对象。它包含一个 List<PlayerCoin>
,用于存储玩家数据。
List<PlayerCoin>
:这是一个动态数组,可以存储任意数量的PlayerCoin
对象。[System.Serializable]
:同样需要这个属性,因为 Unity 需要将整个PlayerCoinList
对象序列化为 JSON。
3. DataSaveManager
类
DataSaveManager
是一个 Unity 的 MonoBehaviour
,用于管理数据的保存和加载。它包含一个 PlayerCoinList
对象,用于存储和操作玩家数据。
MonoBehaviour
:这是 Unity 中所有脚本的基类,允许脚本附加到游戏对象上。PlayerCoinList list
:这是一个实例化的PlayerCoinList
对象,用于存储玩家数据。
为什么这样设计?
1. 数据封装
PlayerCoin
:表示单个玩家的数据。PlayerCoinList
:表示多个玩家的数据集合。DataSaveManager
:负责管理这些数据的保存和加载。
这种分层设计的好处是:
数据结构清晰,易于扩展。
每个类都有明确的职责,代码更易于维护。
2. 序列化的需求
Unity 的 JsonUtility
类只能序列化带有 [System.Serializable]
属性的类。因此:
PlayerCoin
和PlayerCoinList
都需要标记为[System.Serializable]
。PlayerCoinList
是一个“根对象”,因为JsonUtility
需要一个根对象来序列化整个数据结构。
3. Unity 的 JSON 限制
Unity 的 JsonUtility
不能直接序列化 List<T>
或数组,因此需要将 List<PlayerCoin>
包装在一个类(如 PlayerCoinList
)中,作为序列化的根对象。
using UnityEngine;
using System.Collections.Generic;
using System.IO;
[System.Serializable]
public class PlayerCoin
{
public int count;
public string name;
public bool isWinner;
}
[System.Serializable]
public class PlayerCoinList
{
public List<PlayerCoin> playerCoinList = new List<PlayerCoin>();
}
public class DataSaveManager : MonoBehaviour
{
public PlayerCoinList list = new PlayerCoinList();
void Start()
{
// 添加示例数据
PlayerCoin player1 = new PlayerCoin();
player1.count = 100;
player1.name = "Player1";
player1.isWinner = true;
PlayerCoin player2 = new PlayerCoin();
player2.count = 50;
player2.name = "Player2";
player2.isWinner = false;
list.playerCoinList.Add(player1);
list.playerCoinList.Add(player2);
// 保存数据
SaveData();
// 加载数据
LoadData();
}
void SaveData()
{
// 将对象序列化为 JSON
string json = JsonUtility.ToJson(list);
// 将 JSON 数据写入文件
string filePath = Application.persistentDataPath + "/playerData.json";
File.WriteAllText(filePath, json);
Debug.Log("Data saved to: " + filePath);
}
void LoadData()
{
string filePath = Application.persistentDataPath + "/playerData.json";
// 检查文件是否存在
if (File.Exists(filePath))
{
// 读取 JSON 数据
string json = File.ReadAllText(filePath);
// 反序列化 JSON 数据
list = JsonUtility.FromJson<PlayerCoinList>(json);
Debug.Log("Data loaded from: " + filePath);
}
else
{
Debug.Log("File not found: " + filePath);
}
}
}