示例#1
0
        /// <summary>
        /// Due to Unity's serialization limits, we have to do this ourselves:
        /// 1. GameObjects don't allow for lists of derived types
        /// 2. You can use ScriptableObjects, but prefabs don't support ScriptableObjects
        /// 3. ISerializationCallbackReceiver can't use Find()
        ///
        /// This function is called AFTER the items have been deserialized
        /// </summary>
        public void OnAfterDeserialize()
        {
            JSONSerializer.RootObject = gameObject;

            for (int i = 0; i < mItemDefinitions.Count; i++)
            {
                Type lType = null;

                JSONNode lNode = JSONNode.Parse(mItemDefinitions[i]);
                if (lNode != null)
                {
                    string lTypeString = lNode["__Type"].Value;
                    if (lTypeString != null && lTypeString.Length > 0)
                    {
                        lType = Type.GetType(lTypeString);
                    }
                }

                // Fill an existing existing attribute
                if (lType != null && mItems.Count > i && mItems[i].GetType() == lType)
                {
                    object lItemObj = mItems[i];
                    JSONSerializer.DeserializeInto(mItemDefinitions[i], ref lItemObj);
                }
                // Create a new attribute
                else
                {
                    BasicAttribute lAttribute = JSONSerializer.Deserialize(mItemDefinitions[i]) as BasicAttribute;
                    if (lAttribute != null)
                    {
                        if (mItems.Count <= i)
                        {
                            mItems.Add(lAttribute);
                        }
                        else
                        {
                            mItems[i] = lAttribute;
                        }
                    }
                }
            }

            // Get rid of excess items
            for (int i = mItems.Count - 1; i > mItemDefinitions.Count - 1; i--)
            {
                mItems.RemoveAt(i);
            }

            JSONSerializer.RootObject = null;
        }
示例#2
0
        /// <summary>
        /// Due to Unity's serialization limits, we have to do this ourselves:
        /// 1. GameObjects don't allow for lists of derived types
        /// 2. You can use ScriptableObjects, but prefabs don't support ScriptableObjects
        /// 3. ISerializationCallbackReceiver can't use Find()
        ///
        /// This function is called AFTER the items have been deserialized
        /// </summary>
        public void OnAfterDeserialize()
        {
            JSONSerializer.RootObject = gameObject;

            // CDL 07/27/2018 - keep track of any invalid attribute definitions
            List <int> lInvalidDefinitions = new List <int>();

            for (int i = 0; i < mItemDefinitions.Count; i++)
            {
                Type lType       = null;
                bool lUpdateType = false;

                JSONNode lNode = JSONNode.Parse(mItemDefinitions[i]);
                if (lNode != null)
                {
                    string lTypeString = lNode["__Type"].Value;

                    // CDL 07/27/2018 - Use AssemblyHelper.ResolveType() intead of Type.GetType() so that
                    // the Type can still be obtained if the Motion was serialized as belonging to a different assembly
                    lType = AssemblyHelper.ResolveType(lTypeString, out lUpdateType);
                    //Debug.Log("Attribute type resolved to " + lType.AssemblyQualifiedName);
                    //if (lTypeString != null && lTypeString.Length > 0) { lType = Type.GetType(lTypeString); }
                }

                // CDL 07/27/2018 - save this definition for removal afterwards, as we can't resolve the Type from any assembly.
                if (lType == null)
                {
                    lInvalidDefinitions.Add(i);
                    continue;
                }

                // Fill an existing existing attribute
                if (lType != null && mItems.Count > i && mItems[i].GetType() == lType)
                {
                    object lItemObj = mItems[i];

                    JSONSerializer.DeserializeInto(mItemDefinitions[i], ref lItemObj);

                    // CDL 07/27/2018 - update the serialized Type if necessary
                    if (lUpdateType)
                    {
                        //Debug.Log("Updating type: " + lItemObj.GetType().AssemblyQualifiedName);
                        mItemDefinitions[i] = JSONSerializer.Serialize(lItemObj, false);
                    }
                }
                // Create a new attribute
                else
                {
                    BasicAttribute lAttribute = JSONSerializer.Deserialize(mItemDefinitions[i]) as BasicAttribute;
                    if (lAttribute != null)
                    {
                        if (mItems.Count <= i)
                        {
                            mItems.Add(lAttribute);
                        }
                        else
                        {
                            mItems[i] = lAttribute;
                        }
                    }
                }
            }

            // Get rid of excess items
            for (int i = mItems.Count - 1; i > mItemDefinitions.Count - 1; i--)
            {
                mItems.RemoveAt(i);
            }

            // CDL 07/27.2018 - Clean up any invalid attribute definitions
            if (lInvalidDefinitions.Count > 0)
            {
                for (int i = 0; i < lInvalidDefinitions.Count; i++)
                {
                    int lIndex = lInvalidDefinitions[i];
                    mItemDefinitions.RemoveAt(lIndex);
                }
            }

            JSONSerializer.RootObject = null;
        }
        /// <summary>
        /// Deserialize the object from a string
        /// </summary>
        /// <param name="rDefinition">JSON string</param>
        public virtual void Deserialize(string rDefinition)
        {
            object lThis = this;

            JSONSerializer.DeserializeInto(rDefinition, ref lThis);
        }