protected void InitFSM() { if (m_StateMachine == null) { m_StateMachine = new FStateMachine(name, m_States.Length, m_Transitions.Length); m_StateMachine.MaxStackSize = m_StackSize; MonoBehaviour impl = m_OtherImplement ?? this; for (int i = 0; i < m_States.Length; i++) { FiniteState state = m_States[i]; if (state.Init(impl)) { m_StateMachine.AddState(state, state.m_IsDefaultState); } } for (int i = 0; i < m_Transitions.Length; i++) { FiniteStateTransition trans = m_Transitions[i]; if (trans.Init(impl)) { m_StateMachine.AddTransition(trans.m_FromState, trans.m_ToState, trans); } } } }
private void GenerateStates(Type type) { object[] methods = Utility.Ref.GetMethodsWithAttribute <FStateAttribute>(type, true); if (methods == null) { m_States = new FiniteState[0]; return; } FiniteState tmp; FiniteState defaultState = null; FiniteState firstState = null; Dictionary <string, FiniteState> allstates = new Dictionary <string, FiniteState>(); for (int i = 0; i < methods.Length; i++) { System.Reflection.MethodInfo mtd = methods[i] as System.Reflection.MethodInfo; if (!Utility.Ref.MatchMethodRetAndParams(mtd, typeof(void), null)) { Debug.LogError(string.Format("{0} 不能作为状态方法,因为类型不能和 \" void Method() \" 匹配.", mtd.Name)); continue; } object[] states = mtd.GetCustomAttributes(typeof(FStateAttribute), true); for (int j = 0; j < states.Length; j++) { FStateAttribute state = states[j] as FStateAttribute; string sname = state.Name; if (string.IsNullOrEmpty(sname)) { sname = mtd.Name; } if (!allstates.TryGetValue(sname, out tmp)) { tmp = new FiniteState(); tmp.m_StateName = sname; allstates[sname] = tmp; } if (firstState == null) { firstState = tmp; } if (state.IsDefault) { tmp.m_IsDefaultState = true; if (defaultState != null) { defaultState.m_IsDefaultState = false; } defaultState = tmp; } if (state.KeepInStack) { tmp.m_KeepInStack = true; } if ((state.Event & EStateEvent.OnBegin) != 0) { tmp.m_BeginMethod = mtd.Name; } if ((state.Event & EStateEvent.OnTick) != 0) { tmp.m_TickMethod = mtd.Name; } if ((state.Event & EStateEvent.OnEnd) != 0) { tmp.m_EndMethod = mtd.Name; } } } if (defaultState == null && firstState != null) { firstState.m_IsDefaultState = true; } m_States = new FiniteState[allstates.Count]; allstates.Values.CopyTo(m_States, 0); }