Last active
January 7, 2016 04:42
-
-
Save steamb23/d5b52888329a4b596863 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
using System.Runtime.Serialization; | |
using System.Runtime.Serialization.Formatters.Binary; | |
namespace UnityEngine | |
{ | |
[Serializable] | |
public class PlayerData : ISerializable | |
{ | |
public static PlayerData Current = new PlayerData(); | |
// TODO. 여기에 public 필드를 선언하세요. | |
// ex)public string Test; | |
public PlayerData() | |
{ | |
} | |
public void GetObjectData(SerializationInfo info, StreamingContext context) | |
{ | |
// TODO. 여기에 직렬화 작업을 작성하세요. | |
// info.AddValue("Test", Test); | |
} | |
public PlayerData(SerializationInfo info, StreamingContext context) | |
{ | |
// TODO. 여기에 역직렬화 작업을 작성하세요. | |
// this.Test = info.GetString("Test"); | |
// 만약 출시후 필드 변수가 추가되면 호환성을 위해서 추가된 변수는 아래에 별도의 코드로 작성하셔야합니다. | |
// try | |
// { | |
// NewTest = info.GetInt32("NewTest"); | |
// } | |
// catch (SerializationException e) | |
// { | |
// NewTest = 10; | |
// } | |
// 만약 위 코드를 작성하지 않을 경우 변수가 호환되지 않아 불러오기가 실패하게 됩니다. | |
} | |
#region PlayerPrefs Set/Get | |
public static void PlayerPrefsSet() | |
{ | |
PlayerPrefsSet(Current, "PlayerData"); | |
} | |
public static void PlayerPrefsGet() | |
{ | |
Current = PlayerPrefsGet("PlayerData"); | |
} | |
public static void PlayerPrefsSet(PlayerData playerData, string key) | |
{ | |
using (MemoryStream stream = new MemoryStream()) | |
{ | |
BinaryFormatter formatter = new BinaryFormatter(); | |
formatter.Serialize(stream, playerData); | |
string base64 = Convert.ToBase64String(stream.ToArray()); | |
PlayerPrefs.SetString(key, base64); | |
} | |
} | |
public static PlayerData PlayerPrefsGet(string key) | |
{ | |
PlayerData result; | |
string base64 = PlayerPrefs.GetString(key); | |
using (MemoryStream stream = new MemoryStream(Convert.FromBase64String(base64))) | |
{ | |
BinaryFormatter formatter = new BinaryFormatter(); | |
result = (PlayerData)formatter.Deserialize(stream); | |
} | |
return result; | |
} | |
#endregion | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment