示例#1
0
        public static object Deserialize(SerializationData e)
        {
            Type type = null;

            if (string.IsNullOrEmpty(e.TypeGUID) || !TypeGUIDs.TryGetValue(e.TypeGUID, out type))
            {
                if (!string.IsNullOrEmpty(e.Type))
                {
                    type = Type.GetType(e.Type);
                }
            }
            if (type == null)
            {
                UnityEngine.Debug.LogErrorFormat("反序列化失败,缺少类型 {0}, json数据:\n {1}", e.Type, e.JsonDatas);
                return(null);
            }

            var obj = Activator.CreateInstance(type);

#if UNITY_EDITOR
            UnityEditor.EditorJsonUtility.FromJsonOverwrite(e.JsonDatas, obj);
#else
            UnityEngine.JsonUtility.FromJsonOverwrite(e.JsonDatas, obj);
#endif

            return(obj);
        }
示例#2
0
 public void OnBeforeSerialize()
 {
     if (data != null)
     {
         serializeData = SerializerHelper.Serialize(data);
     }
 }
示例#3
0
        public static SerializationData Serialize(object obj)
        {
            if (obj == null)
            {
                throw new Exception("序列化目标对象不能为null");
            }
            SerializationData elem = new SerializationData
            {
                Type = obj.GetType().FullName
            };
            TypeIdAttribute typeIdentify = obj.GetType().GetCustomAttribute <TypeIdAttribute>();

            if (typeIdentify != null)
            {
                elem.TypeGUID = typeIdentify.GUID;
            }
            if (string.IsNullOrWhiteSpace(elem.TypeGUID))
            {
                UnityEngine.Debug.LogErrorFormat("序列化类型 {0} 缺少TypeIdentify 属性,类重名将会丢失数据", elem.Type);
            }
#if UNITY_EDITOR
            elem.JsonDatas = UnityEditor.EditorJsonUtility.ToJson(obj);
#else
            elem.JsonDatas = UnityEngine.JsonUtility.ToJson(obj);
#endif

            return(elem);
        }