Ejemplo n.º 1
0
        /// <summary>
        /// 进行自动化任务(参数)
        /// </summary>
        private void DoAutomaticTaskOfArgs(FSMArgsBase fsmArgs)
        {
            if (_isAutomate)
            {
                FieldInfo[] fieldInfos = AutomaticTask.GetAutomaticFields(fsmArgs.GetType());
                AutomaticTask.ApplyObjectPath(fsmArgs, fieldInfos);

                if (_isSupportedDataDriver)
                {
                    AutomaticTask.ApplyDataBinding(fsmArgs, fieldInfos);
                }
            }
        }
        protected override void OnDefaultEnable()
        {
            base.OnDefaultEnable();

            if (Targets.Length > 1)
            {
                return;
            }

            _stateGC           = new GUIContent();
            _stateGC.image     = EditorGUIUtility.IconContent("AnimatorState Icon").image;
            _addGC             = new GUIContent();
            _addGC.image       = EditorGUIUtility.IconContent("d_Toolbar Plus More").image;
            _addGC.tooltip     = "Add a new state";
            _removeGC          = new GUIContent();
            _removeGC.image    = EditorGUIUtility.IconContent("d_Toolbar Minus").image;
            _removeGC.tooltip  = "Remove select state";
            _defaultGC         = new GUIContent();
            _defaultGC.image   = EditorGUIUtility.IconContent("TimelineEditModeRippleON").image;
            _defaultGC.tooltip = "Default state";
            _finalGC           = new GUIContent();
            _finalGC.image     = EditorGUIUtility.IconContent("TimelineEditModeReplaceON").image;
            _finalGC.tooltip   = "Final state";
            _editGC            = new GUIContent();
            _editGC.image      = EditorGUIUtility.IconContent("d_editicon.sml").image;
            _editGC.tooltip    = "Edit state script";

            _states    = GetProperty("States");
            _stateList = new ReorderableList(serializedObject, _states, true, true, false, false);
            _stateList.drawHeaderCallback = (Rect rect) =>
            {
                Rect sub = rect;
                sub.Set(rect.x, rect.y, 200, rect.height);
                GUI.Label(sub, "Enabled States:");

                if (!EditorApplication.isPlaying)
                {
                    sub.Set(rect.x + rect.width - 40, rect.y - 2, 20, 20);
                    if (GUI.Button(sub, _addGC, "InvisibleButton"))
                    {
                        GenericMenu gm    = new GenericMenu();
                        List <Type> types = ReflectionToolkit.GetTypesInRunTimeAssemblies(type =>
                        {
                            return(type.IsSubclassOf(typeof(FiniteStateBase)) && !type.IsAbstract);
                        });
                        for (int i = 0; i < types.Count; i++)
                        {
                            int    j         = i;
                            string stateName = types[j].FullName;
                            FiniteStateNameAttribute fsmAtt = types[j].GetCustomAttribute <FiniteStateNameAttribute>();
                            if (fsmAtt != null)
                            {
                                stateName = fsmAtt.Name;
                            }

                            if (Target.States.Contains(types[j].FullName))
                            {
                                gm.AddDisabledItem(new GUIContent(stateName));
                            }
                            else
                            {
                                gm.AddItem(new GUIContent(stateName), false, () =>
                                {
                                    Undo.RecordObject(target, "Add FSM State");
                                    Target.States.Add(types[j].FullName);
                                    if (string.IsNullOrEmpty(Target.DefaultState))
                                    {
                                        Target.DefaultState = Target.States[0];
                                    }
                                    if (string.IsNullOrEmpty(Target.FinalState))
                                    {
                                        Target.FinalState = Target.States[0];
                                    }
                                    HasChanged();
                                });
                            }
                        }
                        gm.ShowAsContext();
                    }

                    sub.Set(rect.x + rect.width - 20, rect.y - 2, 20, 20);
                    GUI.enabled = _stateList.index >= 0 && _stateList.index < Target.States.Count;
                    if (GUI.Button(sub, _removeGC, "InvisibleButton"))
                    {
                        Undo.RecordObject(target, "Delete FSM State");

                        if (Target.DefaultState == Target.States[_stateList.index])
                        {
                            Target.DefaultState = null;
                        }
                        if (Target.FinalState == Target.States[_stateList.index])
                        {
                            Target.FinalState = null;
                        }

                        Target.States.RemoveAt(_stateList.index);

                        if (string.IsNullOrEmpty(Target.DefaultState) && Target.States.Count > 0)
                        {
                            Target.DefaultState = Target.States[0];
                        }
                        if (string.IsNullOrEmpty(Target.FinalState) && Target.States.Count > 0)
                        {
                            Target.FinalState = Target.States[0];
                        }

                        HasChanged();
                    }
                    GUI.enabled = true;
                }
            };
            _stateList.drawElementCallback = (Rect rect, int index, bool isActive, bool isFocused) =>
            {
                if (index >= 0 && index < Target.States.Count)
                {
                    SerializedProperty property  = _states.GetArrayElementAtIndex(index);
                    string             stateType = property.stringValue;
                    if (!_stateNames.ContainsKey(stateType))
                    {
                        Type type = ReflectionToolkit.GetTypeInRunTimeAssemblies(stateType);
                        if (type != null)
                        {
                            string stateName = type.FullName;
                            FiniteStateNameAttribute fsmAtt = type.GetCustomAttribute <FiniteStateNameAttribute>();
                            if (fsmAtt != null)
                            {
                                stateName = fsmAtt.Name;
                            }
                            _stateNames.Add(stateType, stateName);
                        }
                        else
                        {
                            _stateNames.Add(stateType, "<Missing>");
                        }
                    }

                    Rect subrect = rect;
                    subrect.Set(rect.x, rect.y + 2, rect.width, 16);
                    _stateGC.text = _stateNames[stateType];
                    GUI.color     = _stateGC.text != "<Missing>" ? Color.white : Color.red;
                    GUI.Label(subrect, _stateGC);
                    GUI.color = Color.white;

                    int size = 20;
                    if (Target.FinalState == stateType)
                    {
                        subrect.Set(rect.x + rect.width - size, rect.y + 2, 20, 16);
                        if (GUI.Button(subrect, _finalGC, "InvisibleButton"))
                        {
                            GenericMenu gm = new GenericMenu();
                            foreach (var state in _stateNames)
                            {
                                gm.AddItem(new GUIContent(state.Value), Target.FinalState == state.Key, () =>
                                {
                                    Undo.RecordObject(target, "Set FSM Final State");
                                    Target.FinalState = state.Key;
                                    HasChanged();
                                });
                            }
                            gm.ShowAsContext();
                        }
                        size += 20;
                    }
                    if (Target.DefaultState == stateType)
                    {
                        subrect.Set(rect.x + rect.width - size, rect.y + 2, 20, 16);
                        if (GUI.Button(subrect, _defaultGC, "InvisibleButton"))
                        {
                            GenericMenu gm = new GenericMenu();
                            foreach (var state in _stateNames)
                            {
                                gm.AddItem(new GUIContent(state.Value), Target.DefaultState == state.Key, () =>
                                {
                                    Undo.RecordObject(target, "Set FSM Default State");
                                    Target.DefaultState = state.Key;
                                    HasChanged();
                                });
                            }
                            gm.ShowAsContext();
                        }
                        size += 20;
                    }
                    if (isActive && isFocused)
                    {
                        subrect.Set(rect.x + rect.width - size, rect.y, 20, 20);
                        if (GUI.Button(subrect, _editGC, "InvisibleButton"))
                        {
                            MonoScriptToolkit.OpenMonoScript(stateType);
                        }
                        size += 20;
                    }
                }
            };
            _stateList.drawElementBackgroundCallback = (Rect rect, int index, bool isActive, bool isFocused) =>
            {
                if (Event.current.type == EventType.Repaint)
                {
                    GUIStyle gUIStyle = (index % 2 != 0) ? "CN EntryBackEven" : "CN EntryBackodd";
                    gUIStyle    = (!isActive && !isFocused) ? gUIStyle : "RL Element";
                    rect.x     += 2;
                    rect.width -= 6;
                    gUIStyle.Draw(rect, false, isActive, isActive, isFocused);
                }
            };
            _stateNames = new Dictionary <string, string>();

            if (Target.Args == null)
            {
                FSMArgsBase args = Target.GetComponent <FSMArgsBase>();
                if (args != null)
                {
                    Target.Args = args;
                    HasChanged();
                }
            }
            if (Target.Args != null)
            {
                Target.Args.hideFlags = HideFlags.HideInHierarchy | HideFlags.HideInInspector;
            }
        }
Ejemplo n.º 3
0
 /// <summary>
 /// 应用FSM参数的对象路径定义
 /// </summary>
 /// <param name="fsmArgs">FSM参数实例</param>
 /// <param name="fieldInfos">所有自动化字段</param>
 public static void ApplyObjectPath(FSMArgsBase fsmArgs, FieldInfo[] fieldInfos)
 {
     ApplyObjectPath(fsmArgs, fsmArgs.StateMachine.gameObject, fieldInfos);
 }
Ejemplo n.º 4
0
 /// <summary>
 /// 应用FSM参数的数据绑定
 /// </summary>
 /// <param name="fsmArgs">FSM参数实例</param>
 /// <param name="fieldInfos">所有自动化字段</param>
 public static void ApplyDataBinding(FSMArgsBase fsmArgs, FieldInfo[] fieldInfos)
 {
     ApplyDataBinding(fsmArgs, fsmArgs.StateMachine.CurrentData, fieldInfos);
 }