Skip to content

Instantly share code, notes, and snippets.

@shop-0761
Last active January 26, 2019 00:53
Show Gist options
  • Save shop-0761/7a1833346c976c9ad505c2685ebbafca to your computer and use it in GitHub Desktop.
Save shop-0761/7a1833346c976c9ad505c2685ebbafca to your computer and use it in GitHub Desktop.
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