Skip to content

Instantly share code, notes, and snippets.

@shop-0761 shop-0761/GameData.cs
Last active Jan 26, 2019

Embed
What would you like to do?
MessagePackでセーブとロードするやつ
//MessagePack をいれとく
//https://github.com/neuecc/MessagePack-CSharp/releases
//GameData.Load(gameObject) GameData.Save(gameObject) で使う
[MessagePack.MessagePackObject]
public class saveDataFormat
{
[MessagePack.Key(0)]
public string name;
[MessagePack.Key(1)]
public Vector3 pos;
[MessagePack.Key(2)]
public Quaternion rot;
[MessagePack.Key(3)]
public Vector3 scale;
}
public static class GameData
{
public static void Save(GameObject obj)
{
var data = new saveDataFormat();
data.name = obj.name;
data.pos = obj.transform.position;
data.rot = obj.transform.rotation;
data.scale = obj.transform.localScale;
var bytes = MessagePack.MessagePackSerializer.Serialize(data);
File.WriteAllBytes(Application.dataPath + "/SaveData/" + data.name + ".bin", bytes);
}
public static void Load(GameObject obj)
{
byte[] readData = File.ReadAllBytes(Application.dataPath + "/SaveData/" + obj.name + ".bin");
var data = MessagePack.MessagePackSerializer.Deserialize<saveDataFormat>(readData);
obj.transform.position = data.pos;
obj.transform.rotation = data.rot;
obj.transform.localScale = data.scale;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.