private void Start() { if (_data != null) { _data.OnInit(); } foreach (var state in _stateInstances) { state.Value.OnInit(); } if (_defaultState != null) { if (_stateInstances.ContainsKey(_defaultState)) { _currentState = _stateInstances[_defaultState]; _currentState.OnEnter(null); } else { throw new HTFrameworkException(HTFrameworkModule.FSM, "切换状态失败:有限状态机 " + Name + " 不存在状态 " + _defaultState.Name + "!"); } } }
private void Awake() { if (IsAutoRegister) { Main.m_FSM.RegisterFSM(this); } //加载数据类 if (Data != "<None>") { Type type = ReflectionToolkit.GetTypeInRunTimeAssemblies(Data); if (type != null) { if (type.IsSubclassOf(typeof(FSMDataBase))) { _data = Activator.CreateInstance(type) as FSMDataBase; _data.StateMachine = this; _data.OnInit(); } else { throw new HTFrameworkException(HTFrameworkModule.FSM, "创建有限状态机数据类失败:数据类 " + Data + " 必须继承至有限状态机数据基类:FSMDataBase!"); } } else { throw new HTFrameworkException(HTFrameworkModule.FSM, "创建有限状态机数据类失败:丢失数据类 " + Data + " !"); } } //加载所有状态 for (int i = 0; i < States.Count; i++) { Type type = ReflectionToolkit.GetTypeInRunTimeAssemblies(States[i]); if (type != null) { if (type.IsSubclassOf(typeof(FiniteStateBase))) { if (!_stateInstances.ContainsKey(type)) { FiniteStateBase state = Activator.CreateInstance(type) as FiniteStateBase; state.StateMachine = this; state.OnInit(); _stateInstances.Add(type, state); } } else { throw new HTFrameworkException(HTFrameworkModule.FSM, "加载有限状态失败:有限状态类 " + States[i] + " 必须继承至有限状态基类:FiniteStateBase!"); } } else { throw new HTFrameworkException(HTFrameworkModule.FSM, "加载有限状态失败:丢失有限状态类 " + States[i] + " !"); } } //设置默认状态、最终状态 if (DefaultState == "" || FinalState == "" || _stateInstances.Count <= 0) { throw new HTFrameworkException(HTFrameworkModule.FSM, "有限状态机 " + Name + " 的状态为空!或未指定默认状态、最终状态!"); } _defaultState = ReflectionToolkit.GetTypeInRunTimeAssemblies(DefaultState); if (_defaultState == null) { throw new HTFrameworkException(HTFrameworkModule.FSM, "有限状态机 " + Name + " 丢失了默认状态 " + DefaultState + "!"); } _finalState = ReflectionToolkit.GetTypeInRunTimeAssemblies(FinalState); if (_finalState == null) { throw new HTFrameworkException(HTFrameworkModule.FSM, "有限状态机 " + Name + " 丢失了最终状态 " + FinalState + "!"); } }