C#字节数组和保存的游戏
本文关键字:保存 游戏 数组 字节 字节数 | 更新日期: 2023-09-27 18:25:54
我需要一些帮助。我正在Unity3D中制作一个足够简单的游戏,我想使用SavedGames(谷歌游戏服务)保存一些变量(黄金、库存等)。我读过它,显然我必须将数据作为字节数组发送到谷歌游戏。所以我也读过字节数组,但我不理解它。每一篇文章或关于它的问题似乎都假设有一定程度的知识(我显然没有)。我被卡住了。我的问题是:如何从int变量转换为字节数组?
例如:用户拥有52个金币(int gold_coins)、3个治疗药剂(int heal_pot)和4个法力药剂(int mana_pot)。我想把这三个变量放入一个字节数组中,当用户保存游戏状态时,我可以将其发送到谷歌。然后,当他再次加载它时,字节数组中的数据需要返回到int。(当我看到如何首先将它们放入数组时,我可能会自己想出最后一部分)。
我希望你们能向我解释,或者给我指明正确的方向。在此期间,祝你星期一愉快。非常感谢。
编辑2:所以,我得到了这个:
在SaveData.cs:中
using UnityEngine;
using ProtoBuf;
[ProtoContract]
public enum DataType{
pu0 = 0; //gold in game
pu1 = 1; //heal_pot ingame
pu2 = 2; //mana_pot ingame
}
[ProtoContract]
public class PBSaveData{
[ProtoMember(1)]
public DataType type;
[ProtoMember(2)]
public int amountStored;
}
在PBGameState.cs:中
using ProtoBuf;
[ProtoContract]
public class PBGameState{
[ProtoMember(1)]
public PBSaveData[] saveData;
}
在SaveManager.cs:中
using ProtoBuf;
using System.IO;
public class SaveManager{
private string gameStateFilePath;
void SaveState(){
PBStateGame state = new PBGameState();
state.saveData = new PBSaveData[3];
for (int i=0; i<state.saveData.Length{
state.saveData[i] = new PBSaveData();
state.saveData[i].type = "pu" + (i+1);
state.saveData[i].amoundStore = its[i] //its is where is store my inventory locally.
}
using(var ms = new MemoryStream()){
// Serialize the data to the stream
Serializer.Serialize(ms, state);
byteArray = ms.ToArray();
}
// Create a texture the size of the screen, RGB24 format
int width = Screen.width;
int height = Screen.height;
Texture2D Screenshot = new Texture2D( width, height, TextureFormat.RGB24, false );
// Read screen contents into the texture
Screenshot.ReadPixels( new Rect(0, 0, width, height), 0, 0 );
Screenshot.Apply();
long TotalPlayedTime = 20000;
string currentSaveName = "sg";
string description = "Modified data at: " + System.DateTime.Now.ToString("MM/dd/yyyy H:mm:ss");
GooglePlaySavedGamesManager.ActionGameSaveResult += ActionGameSaveResult;
GooglePlaySavedGamesManager.instance.CreateNewSnapshot(currentSaveName, description, Screenshot, byteArray, TotalPlayedTime);
}
}
void LoadState()
{
PBGameState state = new PBGameState ();
using(var ms = new MemoryStream(byteArray)){
state = Serializer.Deserialize<PBGameState> (ms);
AndroidMessage.Create ("loadtest", state.saveData[0].amountStored.ToString());
}
if (state.saveData != null) {
// Iterate through the loaded game state
for (int i = 0; i < state.saveData.Length; i++) {
its [i] = state.saveData [i].amountStored;
}
} else {
AndroidMessage.Create ("loadtest", "notworking");
}
}
所以保存显然有效,但当我重新启动游戏时,数据似乎没有加载(我有一个启动加载过程的按钮),库存是空的。。。知道我做错了什么吗?谢谢:)
以下是教程的链接:tuto
有人吗?
您需要做的是:
- 有一个对象来存储要保存的值,例如类名
GameState
或其他什么 - 序列化该类的实例,这是在字节数组中转换实例的过程
- 当您想从字节数组中获取值时,需要反序列化您的字节数组
例如,请参阅将任何对象转换为字节[]
另请参阅https://msdn.microsoft.com/en-us/library/ms233843.aspx
回答第二个问题:
不要使用FileStream
,而是使用MemoryStream
:
// New MemoryStream, used just like any other stream, but without file (all in memory)
var ms = new MemoryStream();
// Serialize the data to the stream
Serializer.Serialize(fs, state);
// Get back the byte array from the stream
// Doesn't matter where the Position is on the stream, this'll return the
// whole MemoryStream as a byte array
var byteArray = ms.ToArray();
首先,将变量保存为某种特定格式的字符串(例如用分号分隔)。然后,使用某种编码将其转换为byte[]。
string saveState = String.Format("{0};{1};{2}", gold_coins, heal_pot, mana_pot);
byte[] saveBytes = Encoding.UTF8.GetBytes(saveState);
之后,您可以使用byte[]随心所欲。解析保存的数据:
string[] retrievedData = Encoding.UTF8.GetString(saveBytes).Split(';'); //saveBytes is the Byte[] you get from the cloud.
int gold = int.Parse(retrievedData[0]);
解析后的数组将按照您在字符串中的顺序包含项目。
但是这只是一种简单的(完全不推荐)序列化方式)。有关更好的方法,请参阅@ken2k的答案。