From 6eb915c129fc90c6f4c82ae097dd6ffad5239efc Mon Sep 17 00:00:00 2001 From: chai Date: Mon, 25 Jan 2021 14:28:30 +0800 Subject: +scripts --- .../Scripts/XEditor/XSkillEditor/XSerialized.cs | 70 ++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 Client/Assets/Scripts/XEditor/XSkillEditor/XSerialized.cs (limited to 'Client/Assets/Scripts/XEditor/XSkillEditor/XSerialized.cs') diff --git a/Client/Assets/Scripts/XEditor/XSkillEditor/XSerialized.cs b/Client/Assets/Scripts/XEditor/XSkillEditor/XSerialized.cs new file mode 100644 index 00000000..6d71a0bc --- /dev/null +++ b/Client/Assets/Scripts/XEditor/XSkillEditor/XSerialized.cs @@ -0,0 +1,70 @@ +#if UNITY_EDITOR +using System; +using System.Collections.Generic; +using UnityEngine; + +using System.IO; +using System.Runtime.Serialization; +using System.Runtime.Serialization.Formatters.Binary; + +namespace XEditor +{ + public class XSerialized where T : class + { + //deep copy of T + private static string SerializeToString(T value) + { + using (MemoryStream objectStream = new MemoryStream()) + { + IFormatter formatter = new BinaryFormatter(); + formatter.Serialize(objectStream, value); + objectStream.Flush(); + return Convert.ToBase64String(objectStream.ToArray()); + } + } + + private static T DeserializeFromString(string data) + { + byte[] bytes = Convert.FromBase64String(data); + using (MemoryStream stream = new MemoryStream(bytes)) + { + return (T)(new BinaryFormatter()).Deserialize(stream); + } + } + + [SerializeField] + private string _serializedData; + + protected T _class; + + public XSerialized() { } + + public XSerialized(T _class) + { + Set(_class); + } + + public void Set(T _class) + { + this._class = _class; + Serialize(); + } + + public T Get() + { + if (_class == null) _class = Deserialize(); + return _class; + } + + public virtual void Serialize() + { + _serializedData = SerializeToString(_class); + } + + protected virtual T Deserialize() + { + return DeserializeFromString(_serializedData); + } + } +} +#endif \ No newline at end of file -- cgit v1.1-26-g67d0