Ejemplo n.º 1
0
 private static Example.StateMachineTransaction ExportTransaction(AnimatorController controller, List <Example.StateMachineTransaction> transList, AnimatorTransitionBase transition, int fromState, Dictionary <Object, Example.StateMachineNode> nodes)
 {
     Example.StateMachineNode toState = null;
     if (transition.destinationState != null)
     {
         toState = nodes[transition.destinationState];
     }
     else if (transition.destinationStateMachine != null)
     {
         toState = nodes[transition.destinationStateMachine];
     }
     return(ExportTransaction(controller, transList, transition, fromState, toState != null?toState.Id:-1));
 }
Ejemplo n.º 2
0
    private static Example.StateMachine ExportStateMachine(AnimatorStateTransition[] topAnyState, List <Example.StateMachine> macList, AnimatorController controller, AnimatorStateMachine st, string name)
    {
        List <Example.StateMachineNode>        nodeList  = new List <Example.StateMachineNode> ();
        List <Example.StateMachineTransaction> transList = new List <Example.StateMachineTransaction> ();
        List <Example.StateMachineParam>       paramList = new List <Example.StateMachineParam> ();

        foreach (var param in controller.parameters)
        {
            var stParam = new Example.StateMachineParam();
            stParam.Name = param.name;
            switch (param.type)
            {
            case AnimatorControllerParameterType.Bool:
                stParam.valueType = Example.StateMachineParam.ValueType.BOOLEAN;
                stParam.BoolValue = param.defaultBool;
                break;

            case AnimatorControllerParameterType.Float:
                stParam.valueType  = Example.StateMachineParam.ValueType.FLOAT;
                stParam.FloatValue = param.defaultFloat;
                break;

            case AnimatorControllerParameterType.Int:
                stParam.valueType = Example.StateMachineParam.ValueType.INTEGER;
                stParam.IntValue  = param.defaultInt;
                break;

            case AnimatorControllerParameterType.Trigger:
                stParam.valueType = Example.StateMachineParam.ValueType.TRIGGER;
                break;

            default:
                Debug.LogErrorFormat("param type {0} not found", param.type);
                break;
            }
            paramList.Add(stParam);
        }

        Dictionary <Object, Example.StateMachineNode> nodes = new Dictionary <Object, Example.StateMachineNode> ();

        int stateID = 0;

        foreach (var state in st.states)
        {
            Debug.LogFormat(name + " child state {0}", state.state.name);
            var stState = new Example.StateMachineNode();
            stState.Id                = nodeList.Count;
            stState.StateName         = state.state.name;
            stState.ChildStateMachine = -1;
            stState.Duration          = state.state.motion != null?Mathf.RoundToInt(state.state.motion.averageDuration *1000) : -1;

            List <string> enterActions = new List <string> ();
            List <string> exitActions  = new List <string> ();
            foreach (var behavior in state.state.behaviours)
            {
                var action1 = behavior as StateMachineEnterAction;
                var action2 = behavior as StateMachineExitAction;
                var action3 = behavior as LuaStateMachineAction;
                if (action1 != null)
                {
                    enterActions.Add(action1.action);
                }
                else if (action2 != null)
                {
                    exitActions.Add(action2.action);
                }
                else if (action3 != null)
                {
                    stState.ScriptName = action3.scriptName;
                }
            }
            stState.EnterActions = enterActions;
            stState.ExitActions  = exitActions;
            nodes [state.state]  = stState;
            nodeList.Add(stState);
        }

        foreach (var childST in st.stateMachines)
        {
            Debug.LogFormat(name + " child st {0}", childST.stateMachine.name);
            var childStateMachine = ExportStateMachine(topAnyState, macList, controller, childST.stateMachine, childST.stateMachine.name);
            var stState           = new Example.StateMachineNode();
            stState.Id                = nodeList.Count;
            stState.StateName         = childST.stateMachine.name;
            stState.ChildStateMachine = childStateMachine.Id;


            List <string> enterActions = new List <string> ();
            List <string> exitActions  = new List <string> ();
            foreach (var behavior in childST.stateMachine.behaviours)
            {
                var action1 = behavior as StateMachineEnterAction;
                var action2 = behavior as StateMachineExitAction;
                var action3 = behavior as LuaStateMachineAction;
                if (action1 != null)
                {
                    enterActions.Add(action1.action);
                }
                else if (action2 != null)
                {
                    exitActions.Add(action2.action);
                }
                else if (action3 != null)
                {
                    stState.ScriptName = action3.scriptName;
                }
            }
            stState.EnterActions = enterActions;
            stState.ExitActions  = exitActions;

            nodes [childST.stateMachine] = stState;
            nodeList.Add(stState);
        }


        /*foreach (var transaction in st.anyStateTransitions) {
         *      var stTransaction = ExportTransaction (controller,transList,transaction,-1,nodes);
         *      stTransaction.ExitTime = transaction.hasExitTime ? Mathf.RoundToInt(transaction.exitTime * 1000): -1;
         * }*/

        foreach (var transaction in topAnyState)
        {
            if (transaction.destinationState != null && nodes.ContainsKey(transaction.destinationState))
            {
                var stTransaction = ExportTransaction(controller, transList, transaction, -1, nodes[transaction.destinationState].Id);
                stTransaction.ExitTime = transaction.hasExitTime ? Mathf.RoundToInt(transaction.exitTime * 1000): -1;
            }
            else if (transaction.destinationStateMachine != null && nodes.ContainsKey(transaction.destinationStateMachine))
            {
                var stTransaction = ExportTransaction(controller, transList, transaction, -1, nodes[transaction.destinationStateMachine].Id);
                stTransaction.ExitTime = transaction.hasExitTime ? Mathf.RoundToInt(transaction.exitTime * 1000): -1;
            }
        }

        foreach (var state in st.states)
        {
            var        stState = nodes [state.state];
            List <int> trans   = new List <int> ();
            foreach (var transaction in state.state.transitions)
            {
                var stTransaction = ExportTransaction(controller, transList, transaction, stState.Id, nodes);
                stTransaction.ExitTime = transaction.hasExitTime ? Mathf.RoundToInt(transaction.exitTime * 1000): -1;
            }
            stState.Transactions = trans;
        }


        foreach (var state in st.stateMachines)
        {
            var        stState = nodes [state.stateMachine];
            List <int> trans   = new List <int> ();
            foreach (var transaction in st.GetStateMachineTransitions(state.stateMachine))
            {
                var stTransaction = ExportTransaction(controller, transList, transaction, stState.Id, nodes);
                if (transaction.name.StartsWith("exittime="))
                {
                    var exitTimeStr = transaction.name.Substring("exittime=".Length);
                    if (exitTimeStr.Length > 0)
                    {
                        stTransaction.ExitTime = Mathf.RoundToInt(float.Parse(exitTimeStr) * 1000);
                    }
                }
            }
            stState.Transactions = trans;
        }

        Example.StateMachine stateMac = new Example.StateMachine();
        stateMac.States       = nodeList;
        stateMac.Transactions = transList;
        stateMac.Params       = paramList;

        Example.StateMachineNode defaultST = null;

        if (st.defaultState != null)
        {
            Object defaultObject = st.defaultState;
            if (!nodes.ContainsKey(st.defaultState))
            {
                var defaultMachine = FindParentStateMachine(st, st.defaultState);
                defaultObject = defaultMachine;
                if (defaultObject == null)
                {
                    Debug.LogErrorFormat("st {0} default {1} not found", st.name, st.defaultState.name);
                }
                else
                {
                    Debug.LogWarningFormat("st {0} default machine {1} ", st.name, defaultMachine.name);
                }
            }
            defaultST = nodes[defaultObject];
        }
        else
        {
            Debug.LogErrorFormat("st {0} defaultState is null", st.name);
        }


        stateMac.Id           = macList.Count;
        stateMac.Name         = name;
        stateMac.DefaultState = defaultST != null?defaultST.Id:-1;
        macList.Add(stateMac);

        return(stateMac);
    }