public RuntimeAnimatorState(AnimationStateInfo pInfo)
        {
            if (pInfo == null)
            {
                return;
            }
            stateInfo    = pInfo;
            layerIndex   = stateInfo.layerIndex;
            machineIndex = stateInfo.machineIndex;
            stateName    = stateInfo.name;
            nameHash     = stateInfo.hashName;

            transLength = stateInfo.transtionList.Count;

            motionHash = stateInfo.motionName.GetHashCode();
        }
示例#2
0
        public void ReadFromFile(System.IO.BinaryReader pReader)
        {
            layerIndex  = pReader.ReadInt32();
            index       = pReader.ReadInt32();
            machineName = pReader.ReadString();
            defaultName = pReader.ReadString();

            int tlen = pReader.ReadInt32();

            for (int i = 0; i < tlen; i++)
            {
                var tstate = new AnimationStateInfo();
                tstate.ReadFromFile(pReader);
                if (tstate.hashName == defaultHashName)
                {
                    defaultState = tstate;
                }
                stateInfos.Add(tstate);
            }

            machineHashName = machineName.GetHashCode();
            defaultHashName = defaultName.GetHashCode();
        }
示例#3
0
        void AnalyzeStateMachine(AnimationLayerInfo pLayerInfo,
                                 UnityEditor.Animations.AnimatorControllerLayer ownerLayer,
                                 UnityEditor.Animations.AnimatorStateMachine stateMachine,
                                 Animator animator,
                                 SkinnedMeshRenderer[] meshRender,
                                 int layer,
                                 int bakeFPS,
                                 int animationIndex)
        {
            var tmachineInfo = new AnimationStateMachineInfo()
            {
                layerIndex  = pLayerInfo.index, index = pLayerInfo.machines.Count,
                defaultName = stateMachine.defaultState != null ? stateMachine.defaultState.name : "",
                machineName = stateMachine.name
            };

            pLayerInfo.machines.Add(tmachineInfo);

            for (int i = 0; i != stateMachine.states.Length; ++i)
            {
                ChildAnimatorState state = stateMachine.states[i];
                var tstateinfo           = new AnimationStateInfo()
                {
                    layerIndex = pLayerInfo.index, machineIndex = tmachineInfo.index, index = i
                };
                tstateinfo.SetData(state.state);
                tmachineInfo.stateInfos.Add(tstateinfo);

                AnimationClip clip     = state.state.motion as AnimationClip;
                bool          needBake = false;
                if (clip == null)
                {
                    continue;
                }
                if (!generateAnims.TryGetValue(clip.name, out needBake))
                {
                    continue;
                }
                foreach (var obj in generateInfo)
                {
                    if (obj.info.animationName == clip.name)
                    {
                        needBake = false;
                        break;
                    }
                }

                if (!needBake)
                {
                    continue;
                }

                AnimationBakeInfo bake = new AnimationBakeInfo();
                bake.length               = clip.averageDuration;
                bake.animator             = animator;
                bake.animator.cullingMode = AnimatorCullingMode.AlwaysAnimate;
                bake.meshRender           = meshRender;
                bake.layer = layer;
                bake.info  = new AnimationInfo();
                bake.info.animationName     = clip.name;
                bake.info.animationNameHash = state.state.nameHash;
                bake.info.animationIndex    = animationIndex;
                bake.info.totalFrame        = (int)(bake.length * bakeFPS + 0.5f) + 1;
                bake.info.totalFrame        = Mathf.Clamp(bake.info.totalFrame, 1, bake.info.totalFrame);
                bake.info.fps        = bakeFPS;
                bake.info.rootMotion = true;
                bake.info.wrapMode   = clip.isLooping ? WrapMode.Loop : clip.wrapMode;
                if (bake.info.rootMotion)
                {
                    bake.info.velocity        = new Vector3[bake.info.totalFrame];
                    bake.info.angularVelocity = new Vector3[bake.info.totalFrame];
                }

                generateInfo.Add(bake);
                animationIndex += bake.info.totalFrame;
                totalFrame     += bake.info.totalFrame;

                bake.info.eventList = new List <AnimationEvent>();
                //AnimationClip clip = state.state.motion as AnimationClip;
                foreach (var evt in clip.events)
                {
                    AnimationEvent aniEvent = new AnimationEvent();
                    aniEvent.function        = evt.functionName;
                    aniEvent.floatParameter  = evt.floatParameter;
                    aniEvent.intParameter    = evt.intParameter;
                    aniEvent.stringParameter = evt.stringParameter;
                    aniEvent.time            = evt.time;
                    if (evt.objectReferenceParameter != null)
                    {
                        aniEvent.objectParameter = evt.objectReferenceParameter.name;
                    }
                    else
                    {
                        aniEvent.objectParameter = "";
                    }
                    bake.info.eventList.Add(aniEvent);
                }

                cacheTransition.Add(state.state, state.state.transitions);
                state.state.transitions = null;

                cacheAnimationEvent.Add(clip, clip.events);
                UnityEngine.AnimationEvent[] tempEvent = new UnityEngine.AnimationEvent[0];
                UnityEditor.AnimationUtility.SetAnimationEvents(clip, tempEvent);
            }
            for (int i = 0; i != stateMachine.stateMachines.Length; ++i)
            {
                AnalyzeStateMachine(pLayerInfo, ownerLayer, stateMachine.stateMachines[i].stateMachine, animator, meshRender, layer, bakeFPS, animationIndex);
            }
        }