Пример #1
0
        protected override void OnInspectorRuntimeGUI()
        {
            base.OnInspectorRuntimeGUI();

            GUILayout.BeginHorizontal();
            GUILayout.Label("Current State: " + _currentStateName);
            GUILayout.EndHorizontal();

            GUILayout.BeginHorizontal();
            GUILayout.Label("States: ");
            GUILayout.EndHorizontal();

            foreach (KeyValuePair <string, Type> state in _stateInstances)
            {
                GUILayout.BeginHorizontal();
                GUILayout.Space(20);
                GUILayout.Label(state.Key);
                GUILayout.FlexibleSpace();
                GUI.enabled = _currentStateName != state.Key;
                if (GUILayout.Button("Switch", "Minibutton"))
                {
                    Target.SwitchState(state.Value);

                    _currentState = Target.GetType().GetField("_currentState", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(Target) as FiniteState;

                    FiniteStateNameAttribute nameAttribute = _currentState.GetType().GetCustomAttribute <FiniteStateNameAttribute>();
                    _currentStateName = nameAttribute != null ? nameAttribute.Name : _currentState.GetType().Name;
                }
                GUI.enabled = true;
                GUILayout.EndHorizontal();
            }
        }
Пример #2
0
 /// <summary>
 /// 附加状态
 /// </summary>
 public void AppendState <T>() where T : FiniteState
 {
     if (!_stateInstances.ContainsKey(typeof(T)))
     {
         FiniteState state = Activator.CreateInstance(typeof(T)) as FiniteState;
         state.StateMachine = this;
         state.OnInit();
         _stateInstances.Add(typeof(T), state);
     }
     else
     {
         GlobalTools.LogError("附加状态失败:有限状态机 " + Name + " 已存在状态 " + typeof(T).Name + "!");
     }
 }
Пример #3
0
 /// <summary>
 /// 附加状态
 /// </summary>
 /// <param name="type">状态类型</param>
 public void AppendState(Type type)
 {
     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} 已存在状态 {1}!", Name, type.Name));
     }
 }
Пример #4
0
        protected override void OnRuntimeEnable()
        {
            base.OnRuntimeEnable();

            Dictionary <Type, FiniteState> states = Target.GetType().GetField("_stateInstances", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(Target) as Dictionary <Type, FiniteState>;

            _stateInstances = new Dictionary <string, Type>();
            foreach (KeyValuePair <Type, FiniteState> state in states)
            {
                FiniteStateNameAttribute attribute = state.Key.GetCustomAttribute <FiniteStateNameAttribute>();
                _stateInstances.Add(attribute != null ? attribute.Name : state.Key.Name, state.Key);
            }

            _currentState = Target.GetType().GetField("_currentState", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(Target) as FiniteState;

            FiniteStateNameAttribute nameAttribute = _currentState.GetType().GetCustomAttribute <FiniteStateNameAttribute>();

            _currentStateName = nameAttribute != null ? nameAttribute.Name : _currentState.GetType().Name;
        }
Пример #5
0
        /// <summary>
        /// 切换状态
        /// </summary>
        public void SwitchState(Type type)
        {
            if (_stateInstances.ContainsKey(type))
            {
                if (_currentState == _stateInstances[type])
                {
                    return;
                }

                if (_currentState != null)
                {
                    _currentState.OnLeave();
                }

                _currentState = _stateInstances[type];
                _currentState.OnEnter();
            }
            else
            {
                GlobalTools.LogError("切换状态失败:有限状态机 " + Name + " 不存在状态 " + type.Name + "!");
            }
        }
Пример #6
0
        /// <summary>
        /// 切换状态
        /// </summary>
        public void SwitchState <T>() where T : FiniteState
        {
            if (_stateInstances.ContainsKey(typeof(T)))
            {
                if (_currentState == _stateInstances[typeof(T)])
                {
                    return;
                }

                if (_currentState != null)
                {
                    _currentState.OnLeave();
                }

                _currentState = _stateInstances[typeof(T)];
                _currentState.OnEnter();
            }
            else
            {
                GlobalTools.LogError("切换状态失败:有限状态机 " + Name + " 不存在状态 " + typeof(T).Name + "!");
            }
        }
Пример #7
0
        /// <summary>
        /// 切换状态
        /// </summary>
        /// <param name="type">状态类型</param>
        public void SwitchState(Type type)
        {
            if (_stateInstances.ContainsKey(type))
            {
                if (_currentState == _stateInstances[type])
                {
                    return;
                }

                if (_currentState != null)
                {
                    _currentState.OnLeave();
                }

                _currentState = _stateInstances[type];
                _currentState.OnEnter();
            }
            else
            {
                GlobalTools.LogError(string.Format("切换状态失败:有限状态机 {0} 不存在状态 {1}!", Name, type.Name));
            }
        }
Пример #8
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 + "!");
            }
        }
Пример #9
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));
            }
        }