Exemplo n.º 1
0
        /// <summary>
        /// Synchronzie layer weight, layer state, and parameter values from the source animator
        /// to the target. (Does not sync IK.)
        /// </summary>
        /// <param name="target">
        /// The Animator whose state will be synchronized with the <paramref name="source"/>.
        /// </param>
        /// <param name="source">
        /// The Animator whose state will be synchronzied to the <paramref name="target"/>.
        /// </param>
        /// <param name="force">
        /// If true only the mandatory validity checks will be performed.  Otherwise
        /// <see cref="CanSafelySyncFrom(Animator, Animator, bool)"/> will be checked first.
        /// </param>
        /// <returns>
        /// True if the synchronaization succeeds.
        /// </returns>
        public static bool SynchronizeFrom(this Animator target, Animator source, bool force)
        {
            if (target.runtimeAnimatorController != source.runtimeAnimatorController)
            {
                // Can't force this.
                Debug.LogError("Different runtime animator controllers.", target);
                return(false);
            }

            if (!(force || target.CanSafelySyncFrom(source, true)))
            {
                return(false);
            }

            for (int i = 0; i < source.layerCount; i++)
            {
                if (i != 0)
                {
                    target.SetLayerWeight(i, source.GetLayerWeight(i));
                }

                var sstate = source.GetCurrentAnimatorStateInfo(i);
                target.Play(sstate.fullPathHash, i, sstate.normalizedTime);
            }

            foreach (var svar in source.parameters)
            {
                int hash = svar.nameHash;

                if (target.IsParameterControlledByCurve(hash))
                {
                    continue;
                }

                switch (svar.type)
                {
                case AnimatorControllerParameterType.Bool:

                    target.SetBool(hash, source.GetBool(hash));
                    break;

                case AnimatorControllerParameterType.Int:

                    target.SetInteger(hash, source.GetInteger(hash));
                    break;

                case AnimatorControllerParameterType.Float:

                    target.SetFloat(hash, source.GetFloat(hash));
                    break;

                case AnimatorControllerParameterType.Trigger:

                    if (source.GetBool(hash))
                    {
                        target.SetTrigger(hash);
                    }
                    break;
                }
            }


            return(true);
        }