public RuntimeAnimatorMachine(AnimationStateMachineInfo pInfo)
 {
     if (pInfo == null)
     {
         return;
     }
     machineInfo     = pInfo;
     layerIndex      = machineInfo.layerIndex;
     index           = machineInfo.index;
     defaultName     = machineInfo.defaultName;
     defaultHashName = machineInfo.defaultHashName;
     machineHashName = machineInfo.machineHashName;
 }
Esempio n. 2
0
        public void ReadFromFile(System.IO.BinaryReader pReader)
        {
            name  = pReader.ReadString();
            index = pReader.ReadInt32();

            int tlen = pReader.ReadInt32();

            for (int i = 0; i < tlen; i++)
            {
                var tmachine = new AnimationStateMachineInfo();
                tmachine.ReadFromFile(pReader);
                machines.Add(tmachine);
            }
            defaultMachine = machines.Count > 0 ? machines[0] : null;
        }
Esempio n. 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);
            }
        }