| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 | using System;using UnityEngine;using System.Globalization;#if UNITY_EDITORusing UnityEditor;#endifnamespace GraphProcessor{    // Warning: this class only support the serialization of UnityObject and primitive    [System.Serializable]    public class SerializableObject    {        [System.Serializable]        class ObjectWrapper        {            public UnityEngine.Object value;        }        public string serializedType;        public string serializedName;        public string serializedValue;        public object value;        public SerializableObject(object value, Type type, string name = null)        {            this.value = value;            this.serializedName = name;            this.serializedType = type.AssemblyQualifiedName;        }        public void Deserialize()        {            if (String.IsNullOrEmpty(serializedType))            {                Debug.LogError("Can't deserialize the object from null type");                return;            }            Type type = Type.GetType(serializedType);            if (type.IsPrimitive)            {                if (string.IsNullOrEmpty(serializedValue))                    value = Activator.CreateInstance(type);                else                    value = Convert.ChangeType(serializedValue, type, CultureInfo.InvariantCulture);            }            else if (typeof(UnityEngine.Object).IsAssignableFrom(type))            {                ObjectWrapper obj = new ObjectWrapper();                JsonUtility.FromJsonOverwrite(serializedValue, obj);                value = obj.value;            }            else if (type == typeof(string))                value = serializedValue.Length > 1 ? serializedValue.Substring(1, serializedValue.Length - 2).Replace("\\\"", "\"") : "";            else            {                try {                    value = Activator.CreateInstance(type);                    JsonUtility.FromJsonOverwrite(serializedValue, value);                } catch (Exception e){                    Debug.LogError(e);                    Debug.LogError("Can't serialize type " + serializedType);                }            }        }        public void Serialize()        {            if (value == null)                return ;            serializedType = value.GetType().AssemblyQualifiedName;            if (value.GetType().IsPrimitive)                serializedValue = Convert.ToString(value, CultureInfo.InvariantCulture);            else if (value is UnityEngine.Object) //type is a unity object            {                if ((value as UnityEngine.Object) == null)                    return ;                ObjectWrapper wrapper = new ObjectWrapper { value = value as UnityEngine.Object };                serializedValue = JsonUtility.ToJson(wrapper);            }            else if (value is string)                serializedValue = "\"" + ((string)value).Replace("\"", "\\\"") + "\"";            else            {                try {                    serializedValue = JsonUtility.ToJson(value);                    if (String.IsNullOrEmpty(serializedValue))                        throw new Exception();                } catch {                    Debug.LogError("Can't serialize type " + serializedType);                }            }        }    }}
 |