Пример #1
0
        private void Awake()
        {
            Main.m_FSM.RegisterFSM(this);
            //加载数据类
            if (Data != "<None>")
            {
                Type type = GlobalTools.GetTypeInRunTimeAssemblies(Data);
                if (type != null)
                {
                    if (type.BaseType == typeof(FSMData))
                    {
                        _data = Activator.CreateInstance(type) as FSMData;
                        _data.StateMachine = this;
                        _data.OnInit();
                    }
                    else
                    {
                        GlobalTools.LogError("创建数据类失败:数据类 " + Data + " 必须继承至有限状态机数据基类:FSMData!");
                    }
                }
                else
                {
                    GlobalTools.LogError("创建数据类失败:丢失数据类 " + Data + "!");
                }
            }
            //加载所有状态
            for (int i = 0; i < States.Count; i++)
            {
                Type type = GlobalTools.GetTypeInRunTimeAssemblies(States[i]);
                if (type != null)
                {
                    if (type.BaseType == typeof(FiniteState))
                    {
                        if (!_stateInstances.ContainsKey(type))
                        {
                            FiniteState state = Activator.CreateInstance(type) as FiniteState;
                            state.StateMachine = this;
                            state.OnInit();
                            _stateInstances.Add(type, state);
                        }
                    }
                    else
                    {
                        GlobalTools.LogError("加载有限状态失败:有限状态 " + States[i] + " 必须继承至有限状态基类:FiniteState!");
                    }
                }
                else
                {
                    GlobalTools.LogError("加载有限状态失败:丢失有限状态 " + States[i] + "!");
                }
            }
            //进入默认状态
            if (DefaultState == "" || _stateInstances.Count <= 0)
            {
                GlobalTools.LogError("有限状态机 " + Name + " 的状态为空!或未指定默认状态!");
                return;
            }
            Type dtype = GlobalTools.GetTypeInRunTimeAssemblies(DefaultState);

            if (dtype != null)
            {
                if (_stateInstances.ContainsKey(dtype))
                {
                    _currentState = _stateInstances[dtype];
                    _currentState.OnEnter();
                }
                else
                {
                    GlobalTools.LogError("切换状态失败:有限状态机 " + Name + " 不存在状态 " + dtype.Name + "!");
                }
            }
            else
            {
                GlobalTools.LogError("切换状态失败:丢失有限状态 " + DefaultState + "!");
            }
        }
Пример #2
0
        private void Awake()
        {
            if (IsAutoRegister)
            {
                Main.m_FSM.RegisterFSM(this);
            }
            //加载数据类
            if (Data != "<None>")
            {
                Type type = GlobalTools.GetTypeInRunTimeAssemblies(Data);
                if (type != null)
                {
                    if (type.IsSubclassOf(typeof(FSMData)))
                    {
                        _data = Activator.CreateInstance(type) as FSMData;
                        _data.StateMachine = this;
                        _data.OnInit();
                    }
                    else
                    {
                        GlobalTools.LogError(string.Format("创建数据类失败:数据类 {0} 必须继承至有限状态机数据基类:FSMData!", Data));
                    }
                }
                else
                {
                    GlobalTools.LogError(string.Format("创建数据类失败:丢失数据类 {0}!", Data));
                }
            }
            //加载所有状态
            for (int i = 0; i < States.Count; i++)
            {
                Type type = GlobalTools.GetTypeInRunTimeAssemblies(States[i]);
                if (type != null)
                {
                    if (type.IsSubclassOf(typeof(FiniteState)))
                    {
                        if (!_stateInstances.ContainsKey(type))
                        {
                            FiniteState state = Activator.CreateInstance(type) as FiniteState;
                            state.StateMachine = this;
                            state.OnInit();
                            _stateInstances.Add(type, state);
                        }
                    }
                    else
                    {
                        GlobalTools.LogError(string.Format("加载有限状态失败:有限状态 {0} 必须继承至有限状态基类:FiniteState!", States[i]));
                    }
                }
                else
                {
                    GlobalTools.LogError(string.Format("加载有限状态失败:丢失有限状态 {0}!", States[i]));
                }
            }
            //进入默认状态
            if (DefaultState == "" || _stateInstances.Count <= 0)
            {
                GlobalTools.LogError(string.Format("有限状态机 {0} 的状态为空!或未指定默认状态!", Name));
                return;
            }
            Type dtype = GlobalTools.GetTypeInRunTimeAssemblies(DefaultState);

            if (dtype != null)
            {
                if (_stateInstances.ContainsKey(dtype))
                {
                    _currentState = _stateInstances[dtype];
                    _currentState.OnEnter();
                }
                else
                {
                    GlobalTools.LogError(string.Format("切换状态失败:有限状态机 {0} 不存在状态 {1}!", Name, dtype.Name));
                }
            }
            else
            {
                GlobalTools.LogError(string.Format("切换状态失败:丢失有限状态 {0}!", DefaultState));
            }
        }