/// <summary>
        /// Processes the motion definitions and updates the motions to match
        /// the definitions.
        /// </summary>
        public void InstanciateMotions()
        {
            int lMotionCount    = Motions.Count;
            int lMotionDefCount = MotionDefinitions.Count;

            // First, remove any extra motions that may exist
            for (int i = lMotionCount - 1; i >= lMotionDefCount; i--)
            {
                Motions.RemoveAt(i);
            }

            // Initialize the root object so we can serialize transforms and game objects
            JSONSerializer.RootObject = mMotionController.gameObject;

            // We need to match the motion definitions to the motions
            for (int i = 0; i < lMotionDefCount; i++)
            {
                string   lDefinition     = MotionDefinitions[i];
                JSONNode lDefinitionNode = JSONNode.Parse(lDefinition);
                if (lDefinitionNode == null)
                {
                    continue;
                }

                MotionControllerMotion lMotion = null;

                string lTypeString = lDefinitionNode["Type"].Value;

                Type lType = Type.GetType(lTypeString);
                if (lType == null)
                {
                    continue;
                }

                float lPriority = 0;

                // If don't have a motion matching the type, we need to create one
                if (Motions.Count <= i || lTypeString != Motions[i].GetType().AssemblyQualifiedName)
                {
                    lMotion = Activator.CreateInstance(lType) as MotionControllerMotion;
                    lMotion.MotionController = mMotionController;
                    lMotion.MotionLayer      = this;

                    if (Motions.Count <= i)
                    {
                        Motions.Add(lMotion);
                    }
                    else
                    {
                        Motions[i] = lMotion;
                    }
                }
                // Grab the matching motion
                else
                {
                    lMotion = Motions[i];

                    // Track the priority so we can reset it
                    lPriority = lMotion.Priority;
                }

                // Fill the motion with data from the definition
                if (lMotion != null)
                {
                    lMotion.DeserializeMotion(lDefinition);

                    // Reset the priority based on the default
                    if (lPriority > 0)
                    {
                        lMotion.Priority = lPriority;
                    }

                    // We re-serialize the motion incase there was a change. If the
                    // type changed or some other value, we want the updated definition
                    MotionDefinitions[i] = lMotion.SerializeMotion();
                }
            }

            // Clear the root object
            JSONSerializer.RootObject = null;

            // Allow each motion to initialize now that his has been deserialized
            for (int i = 0; i < Motions.Count; i++)
            {
                Motions[i].Awake();
                Motions[i].Initialize();
            }
        }
Beispiel #2
0
        /// <summary>
        /// Processes the motion definitions and updates the motions to match
        /// the definitions.
        /// </summary>
        public void InstanciateMotions()
        {
            if (mMotionController == null)
            {
                return;
            }

            int lMotionCount    = Motions.Count;
            int lMotionDefCount = MotionDefinitions.Count;

            // First, remove any extra motions that may exist
            for (int i = lMotionCount - 1; i >= lMotionDefCount; i--)
            {
                Motions.RemoveAt(i);
            }

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

            // Initialize the root object so we can serialize transforms and game objects
            JSONSerializer.RootObject = mMotionController.gameObject;
            // We need to match the motion definitions to the motions
            for (int i = 0; i < lMotionDefCount; i++)
            {
                string   lDefinition     = MotionDefinitions[i];
                JSONNode lDefinitionNode = JSONNode.Parse(lDefinition);
                if (lDefinitionNode == null)
                {
                    continue;
                }

                MotionControllerMotion lMotion = null;

                string lTypeString = lDefinitionNode["Type"].Value;
                //Debug.Log("[MotionControllerLayer] Deserializing motion: " + lTypeString);

                // 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
                bool lUpdateType;
                Type lType = AssemblyHelper.ResolveType(lTypeString, out lUpdateType);
                if (lType == null)
                {
                    // CDL 07/27/2018 - save this definition for removal afterwards, as we can't resolve the Type from any assembly.
                    lInvalidDefinitions.Add(i);
                    Debug.Log(string.Format("[MotionControllerLayer] Could not get type for {0}. Definition will be removed.", lTypeString));
                    //Debug.Log(lDefinition);
                    continue;
                }

                float lPriority = 0;

                // If don't have a motion matching the type, we need to create one
                if (Motions.Count <= i || lTypeString != Motions[i].GetType().AssemblyQualifiedName)
                {
                    lMotion = Activator.CreateInstance(lType) as MotionControllerMotion;
                    // CDL 07/27/2018 - added a null check
                    if (lMotion == null)
                    {
                        continue;
                    }
                    lMotion.MotionController = mMotionController;
                    lMotion.MotionLayer      = this;

                    if (Motions.Count <= i)
                    {
                        Motions.Add(lMotion);
                    }
                    else
                    {
                        Motions[i] = lMotion;
                    }
                }
                // Grab the matching motion
                else
                {
                    lMotion = Motions[i];

                    // Track the priority so we can reset it
                    lPriority = lMotion.Priority;
                }

                // Fill the motion with data from the definition
                if (lMotion != null)
                {
                    lMotion.DeserializeMotion(lDefinition);

                    // Reset the priority based on the default
                    if (lPriority > 0)
                    {
                        lMotion.Priority = lPriority;
                    }

                    // CDL 07/27/2018
                    // If we need to update the Type (because the AssemblyQualfiedName needs to be updated), clear it
                    // so that SerializeMotion() gets the type name again
                    if (lUpdateType)
                    {
                        lMotion.Type = "";
                    }
                    // We re-serialize the motion incase there was a change. If the
                    // type changed or some other value, we want the updated definition
                    MotionDefinitions[i] = lMotion.SerializeMotion();
                }
            }

            // CDL 07/27.2018 - Clean up any invalid Motion Definitions
            if (lInvalidDefinitions.Count > 0)
            {
                for (int i = 0; i < lInvalidDefinitions.Count; i++)
                {
                    int lIndex = lInvalidDefinitions[i];
                    Debug.Log("Removing invalid Motion Defintion: " + MotionDefinitions[lIndex]);
                    MotionDefinitions.RemoveAt(lIndex);
                }
            }

            // Clear the root object
            JSONSerializer.RootObject = null;

            // Allow each motion to initialize now that this has been deserialized
            for (int i = 0; i < Motions.Count; i++)
            {
                Motions[i].Awake();
                Motions[i].Initialize();
            }
        }