예제 #1
0
        void OnEnable()
        {
            BaseState[] states;

            styles = new AIBehaviorsStyles();

            m_Object  = new SerializedObject(target);
            fsm       = m_Object.targetObject as AIBehaviors;
            transform = fsm.transform;

            derivedStateNames = AIBehaviorsComponentInfoHelper.GetStateTypeNames();

            curStateSelection  = 0;
            prevStateSelection = 0;

            // Sorts old states and initializes new states
            InitStates();
            List <BaseState> statesList = new List <BaseState>(fsm.GetAllStates());

            for (int i = 0; i < statesList.Count; i++)
            {
                if (statesList[i] == null)
                {
                    statesList.RemoveAt(i);
                    i--;
                }
            }

            states = statesList.ToArray();
            fsm.ReplaceAllStates(states);

            for (int i = 0; i < states.Length; i++)
            {
                states[i].OnInspectorEnabled(m_Object);
            }

            if (EditorPrefs.HasKey(advancedModeKey))
            {
                advancedMode = EditorPrefs.GetBool(advancedModeKey, false);
            }

            if (EditorPrefs.HasKey(selectedStateKey))
            {
                string stateName = EditorPrefs.GetString(selectedStateKey);

                for (int i = 0; i < states.Length; i++)
                {
                    if (stateName == states[i].name)
                    {
                        curStateSelection  = i;
                        prevStateSelection = i;
                    }
                }
            }
        }
예제 #2
0
        void OnEnable()
        {
            styles = new AIBehaviorsStyles();

            m_Object               = new SerializedObject(target);
            animationStatesProp    = m_Object.FindProperty("states");
            m_AnimationStatesCount = m_Object.FindProperty(kArraySize);

            animStates = m_Object.targetObject as AIAnimationStates;
            transform  = animStates.transform;

            InitStatesGameObject();
        }
예제 #3
0
        private static void DrawArray(SerializedObject sObject, string fieldName)
        {
            SerializedProperty arraySizeProperty = sObject.FindProperty(fieldName + ".Array.size");
            SerializedProperty arrayDataProperty;
            SerializedProperty prop;
            string             arrayDataPropertyName = fieldName + ".Array.data[{0}]";
            string             baseStateTypeString   = (typeof(BaseState)).ToString();
            AIBehaviorsStyles  styles = new AIBehaviorsStyles();

            prop = sObject.FindProperty(fieldName);

            GUILayout.BeginHorizontal();
            {
                GUILayout.Label(fieldName.ToUpper() + ": ");
                EditorGUILayout.PropertyField(arraySizeProperty);
            }
            GUILayout.EndHorizontal();

            for (int i = 0; i < prop.arraySize; i++)
            {
                bool isBaseState;
                bool oldEnabled = GUI.enabled;

                GUILayout.BeginHorizontal();
                {
                    arrayDataProperty = sObject.FindProperty(string.Format(arrayDataPropertyName, i));
                    isBaseState       = arrayDataProperty.type.Contains(baseStateTypeString);

                    if (isBaseState)
                    {
                        GameObject  targetObject = (sObject.targetObject as Component).gameObject;
                        AIBehaviors fsm          = targetObject.transform.parent.GetComponent <AIBehaviors>();
                        BaseState   curState     = arrayDataProperty.objectReferenceValue as BaseState;

                        arrayDataProperty.objectReferenceValue = AIBehaviorsStatePopups.DrawEnabledStatePopup(fsm, curState) as BaseState;
                    }
                    else
                    {
                        EditorGUILayout.PropertyField(arrayDataProperty);
                    }

                    GUI.enabled = i > 0;
                    if (GUILayout.Button(styles.blankContent, styles.upStyle, GUILayout.MaxWidth(styles.arrowButtonWidths)))
                    {
                        prop.MoveArrayElement(i, i - 1);
                    }
                    GUI.enabled = oldEnabled;

                    GUI.enabled = i < prop.arraySize - 1;
                    if (GUILayout.Button(styles.blankContent, styles.downStyle, GUILayout.MaxWidth(styles.arrowButtonWidths)))
                    {
                        prop.MoveArrayElement(i, i + 1);
                    }
                    GUI.enabled = oldEnabled;

                    if (GUILayout.Button(styles.blankContent, styles.addStyle, GUILayout.MaxWidth(styles.addRemoveButtonWidths)))
                    {
                        prop.InsertArrayElementAtIndex(i);
                    }
                    GUI.enabled = oldEnabled;

                    if (GUILayout.Button(styles.blankContent, styles.removeStyle, GUILayout.MaxWidth(styles.addRemoveButtonWidths)))
                    {
                        prop.DeleteArrayElementAtIndex(i);
                    }
                    GUI.enabled = oldEnabled;
                }
                GUILayout.EndHorizontal();
            }
        }
        private static void DrawSubTriggers(SerializedProperty triggerProperty, AIBehaviorsStyles styles, int insetSpace, AIBehaviors fsm, string[] triggerTypeNames, string[] subTriggerTypeNames)
        {
            BaseTrigger        trigger             = triggerProperty.objectReferenceValue as BaseTrigger;
            SerializedObject   triggerObject       = new SerializedObject(triggerProperty.objectReferenceValue);
            SerializedProperty subTriggersProperty = triggerObject.FindProperty("subTriggers");

            if (trigger.subTriggers.Length > 0)
            {
                GUILayout.BeginVertical(GUI.skin.box);
                {
                    int arraySize = trigger.subTriggers.Length;

                    for (int i = 0; i < arraySize; i++)
                    {
                        bool removedAnElement = false;

                        GUILayout.BeginHorizontal();
                        {
                            GUILayout.Space(insetSpace);
                            removedAnElement = DrawTriggerSelectionPopup(subTriggerTypeNames, ref subTriggersProperty, i);

                            if (!removedAnElement)
                            {
                                removedAnElement = DrawArrowsPlusAndMinus(i, arraySize, ref triggerObject, ref subTriggersProperty, styles, "subTriggers", triggerTypeNames[0]);
                            }
                        }
                        GUILayout.EndHorizontal();

                        if (removedAnElement)
                        {
                            break;
                        }

                        GUILayout.BeginHorizontal();
                        {
                            GUILayout.Space(insetSpace);

                            GUILayout.BeginVertical();
                            {
                                trigger.subTriggers[i].DrawInspectorGUI(fsm);

                                DrawSubTriggers(subTriggersProperty.GetArrayElementAtIndex(i), styles, insetSpace + subTriggersInsetAmount, fsm, triggerTypeNames, subTriggerTypeNames);
                            }
                            GUILayout.EndVertical();
                        }
                        GUILayout.EndHorizontal();
                    }
                }
                GUILayout.EndVertical();
            }

            GUILayout.BeginHorizontal();
            {
                GUILayout.Space(insetSpace);
                if (GUILayout.Button(styles.blankContent, styles.addStyle, GUILayout.MaxWidth(styles.addRemoveButtonWidths)))
                {
                    GameObject go        = trigger.gameObject;
                    int        arraySize = subTriggersProperty.arraySize;

                                        #if UNITY_4_3
                    Undo.RegisterFullObjectHierarchyUndo(fsm.gameObject);
                                        #else
                    Undo.RegisterFullObjectHierarchyUndo(fsm.gameObject, "AddSubTrigger");
                                        #endif

                    subTriggersProperty.arraySize++;
                    subTriggersProperty.GetArrayElementAtIndex(arraySize).objectReferenceValue = ComponentHelper.AddComponentByName(go, triggerTypeNames[0]);
                }
            }
            GUILayout.EndHorizontal();

            triggerObject.ApplyModifiedProperties();
        }
        public static void Draw(SerializedObject m_Object, AIBehaviors fsm, string foldoutLabel, string foldoutValueKey)
        {
            string[]           triggerTypeNames    = AIBehaviorsComponentInfoHelper.GetTriggerTypeNames();
            string[]           subTriggerTypeNames = AIBehaviorsComponentInfoHelper.GetTriggerTypeNames(true);
            SerializedProperty triggersProperty    = m_Object.FindProperty("triggers");
            AIBehaviorsStyles  styles = new AIBehaviorsStyles();
            bool foldoutValue         = false;
            bool newFoldoutValue      = false;

            EditorGUILayout.Separator();

            if (EditorPrefs.HasKey(foldoutValueKey))
            {
                foldoutValue = EditorPrefs.GetBool(foldoutValueKey);
            }

            newFoldoutValue = EditorGUILayout.Foldout(foldoutValue, foldoutLabel, EditorStyles.foldoutPreDrop);

            if (foldoutValue != newFoldoutValue)
            {
                foldoutValue = newFoldoutValue;
                EditorPrefs.SetBool(foldoutValueKey, foldoutValue);
            }

            if (!foldoutValue)
            {
                return;
            }

            int  arraySize = triggersProperty.arraySize;
            bool arrayIndexRemoved;

            for (int i = 0; i < arraySize; i++)
            {
                GUILayout.BeginVertical(GUI.skin.box);
                {
                    GUILayout.BeginHorizontal();
                    {
                        arrayIndexRemoved = DrawTriggerSelectionPopup(triggerTypeNames, ref triggersProperty, i);

                        if (!arrayIndexRemoved)
                        {
                            arrayIndexRemoved = DrawArrowsPlusAndMinus(i, arraySize, ref m_Object, ref triggersProperty, styles, "triggers", triggerTypeNames[0]);
                        }
                    }
                    GUILayout.EndHorizontal();

                    if (arrayIndexRemoved)
                    {
                        arraySize--;
                    }
                    else
                    {
                        BaseTrigger baseTrigger = triggersProperty.GetArrayElementAtIndex(i).objectReferenceValue as BaseTrigger;

                        if (baseTrigger == null)
                        {
                            Debug.LogError("Null trigger deleted!");
                            triggersProperty.DeleteArrayElementAtIndex(i);
                        }
                        else
                        {
                            baseTrigger.DrawInspectorGUI(fsm);

                            DrawSubTriggers(triggersProperty.GetArrayElementAtIndex(i), styles, subTriggersInsetAmount, fsm, triggerTypeNames, subTriggerTypeNames);
                            baseTrigger.DrawTransitionState(fsm);
                        }
                    }
                }
                GUILayout.EndVertical();
            }

            if (GUILayout.Button(styles.blankContent, styles.addStyle, GUILayout.MaxWidth(styles.addRemoveButtonWidths)))
            {
                Component result = null;

                for (int i = 0; i < triggerTypeNames.Length; i++)
                {
                    result = ComponentHelper.AddComponentByName(fsm.statesGameObject, triggerTypeNames[i]);

                    if (result != null)
                    {
                        break;
                    }
                }

                triggersProperty.arraySize++;
                triggersProperty.GetArrayElementAtIndex(arraySize).objectReferenceValue = result;
            }

            m_Object.ApplyModifiedProperties();
        }
        private static bool DrawArrowsPlusAndMinus(int index, int arraySize, ref SerializedObject m_Object, ref SerializedProperty triggersProperty, AIBehaviorsStyles styles, string propertyName, string firstTriggerType)
        {
            SerializedProperty prop = triggersProperty.GetArrayElementAtIndex(index);

            if (prop != null)
            {
                Object obj = prop.objectReferenceValue;

                if (obj != null)
                {
                    bool oldEnabled = GUI.enabled;

                    GUI.enabled = index > 0;
                    if (GUILayout.Button(styles.blankContent, styles.upStyle, GUILayout.MaxWidth(styles.arrowButtonWidths)))
                    {
                        triggersProperty.MoveArrayElement(index, index - 1);
                    }

                    GUI.enabled = index < arraySize - 1;
                    if (GUILayout.Button(styles.blankContent, styles.downStyle, GUILayout.MaxWidth(styles.arrowButtonWidths)))
                    {
                        triggersProperty.MoveArrayElement(index, index + 1);
                    }

                    GUI.enabled = true;
                    if (GUILayout.Button(styles.blankContent, styles.addStyle, GUILayout.MaxWidth(styles.addRemoveButtonWidths)))
                    {
                        GameObject go = (m_Object.targetObject as Component).gameObject;

                        triggersProperty.InsertArrayElementAtIndex(index);
                        triggersProperty.GetArrayElementAtIndex(index + 1).objectReferenceValue = ComponentHelper.AddComponentByName(go, firstTriggerType);
                    }

                    if (GUILayout.Button(styles.blankContent, styles.removeStyle, GUILayout.MaxWidth(styles.addRemoveButtonWidths)))
                    {
                        BaseTrigger baseTrigger = prop.objectReferenceValue as BaseTrigger;

                                                #if UNITY_4_3
                        Undo.RegisterFullObjectHierarchyUndo(baseTrigger);
                                                #else
                        Undo.RegisterFullObjectHierarchyUndo(baseTrigger, "RemovedTrigger");
                                                #endif

                        AIBehaviorsAssignableObjectArray.RemoveObjectAtIndex(m_Object, index, propertyName);
                        DestroyTriggers(baseTrigger.subTriggers);
                        Object.DestroyImmediate(baseTrigger, true);

                        return(true);
                    }

                    GUI.enabled = oldEnabled;
                }
            }

            return(false);
        }
        public static void DrawAnimationFields(SerializedObject m_StateObject, bool usesMultipleAnimations)
        {
            SerializedProperty m_animationStates = m_StateObject.FindProperty("skillStatesComponent");
            SerializedProperty statesProperty    = m_StateObject.FindProperty("skillStates");
            int               arraySize          = statesProperty.arraySize;
            AISkillStates     animStatesComponent;
            AIBehaviorsStyles styles = new AIBehaviorsStyles();
            bool              newFoldoutValue;
            bool              hadNullAnimation = false;

            const string foldoutValueKey = "AIBehaviors_SkillsFoldout";

            AssignAnimationStatesComponent(m_StateObject);
            animStatesComponent = m_animationStates.objectReferenceValue as AISkillStates;

            if (!gotFoldoutValue)
            {
                if (EditorPrefs.HasKey(foldoutValueKey))
                {
                    foldoutValue = EditorPrefs.GetBool(foldoutValueKey);
                }

                gotFoldoutValue = true;
            }

            newFoldoutValue = foldoutValue;
            newFoldoutValue = EditorGUILayout.Foldout(foldoutValue, "Skills:", EditorStyles.foldoutPreDrop);

            if (foldoutValue != newFoldoutValue)
            {
                foldoutValue = newFoldoutValue;
                EditorPrefs.SetBool(foldoutValueKey, foldoutValue);
            }

            // Check the array for null animations
            for (int i = 0; i < arraySize; i++)
            {
                if (statesProperty.GetArrayElementAtIndex(i).objectReferenceValue == null)
                {
                    if (animStatesComponent != null)
                    {
                        statesProperty.GetArrayElementAtIndex(i).objectReferenceValue = animStatesComponent.states[0];
                        hadNullAnimation = true;
                    }
                }
            }

            if (!foldoutValue || hadNullAnimation)
            {
                m_StateObject.ApplyModifiedProperties();
                return;
            }

            // Is the component assigned?
            if (m_animationStates.objectReferenceValue != null && animStatesComponent != null && arraySize > 0)
            {
                GUILayout.BeginVertical(GUI.skin.box);

                AISkillState[] states = new AISkillState[arraySize];
                string[]       animationStateNames = GetAnimationStateNames(m_StateObject);

                if (animStatesComponent.states.Length == 0)
                {
                    Color oldColor = GUI.color;
                    GUI.color = Color.yellow;
                    GUILayout.Label("No states have been created\nfor the AISkillStates component.");
                    GUI.color = oldColor;

                    return;
                }

                for (int i = 0; i < arraySize; i++)
                {
                    SerializedProperty prop = statesProperty.GetArrayElementAtIndex(i);
                    bool oldEnabled         = GUI.enabled;

                    if (prop != null)
                    {
                        Object obj = prop.objectReferenceValue;
                        int    curIndex;

                        if (obj != null)
                        {
                            states[i] = obj as AISkillState;
                            curIndex  = GetCurrentStateIndex(states[i], animStatesComponent.states);

                            if (curIndex == -1)
                            {
                                if (animStatesComponent.states.Length > 0)
                                {
                                    curIndex  = 0;
                                    states[i] = animStatesComponent.states[0];
                                }
                            }

                            GUILayout.BeginHorizontal();
                            {
                                curIndex = EditorGUILayout.Popup(curIndex, animationStateNames, EditorStyles.popup);
                                m_StateObject.FindProperty(string.Format(kArrayData, i)).objectReferenceValue = animStatesComponent.states[curIndex];

                                if (usesMultipleAnimations)
                                {
                                    GUI.enabled = i > 0;
                                    if (GUILayout.Button(styles.blankContent, styles.upStyle, GUILayout.MaxWidth(styles.arrowButtonWidths)))
                                    {
                                        statesProperty.MoveArrayElement(i, i - 1);
                                    }

                                    GUI.enabled = i < arraySize - 1;
                                    if (GUILayout.Button(styles.blankContent, styles.downStyle, GUILayout.MaxWidth(styles.arrowButtonWidths)))
                                    {
                                        statesProperty.MoveArrayElement(i, i + 1);
                                    }

                                    GUI.enabled = true;
                                    if (GUILayout.Button(styles.blankContent, styles.addStyle, GUILayout.MaxWidth(styles.addRemoveButtonWidths)))
                                    {
                                        statesProperty.InsertArrayElementAtIndex(i);
                                    }

                                    //GUI.enabled = arraySize > 1;
                                    if (GUILayout.Button(styles.blankContent, styles.removeStyle, GUILayout.MaxWidth(styles.addRemoveButtonWidths)))
                                    {
                                        AIBehaviorsAssignableObjectArray.RemoveObjectAtIndex(m_StateObject, i, "skillStates");
                                        GUILayout.EndHorizontal();
                                        break;
                                    }
                                    GUI.enabled = oldEnabled;
                                }
                            }
                            GUILayout.EndHorizontal();
                        }
                        else
                        {
                            statesProperty.GetArrayElementAtIndex(i).objectReferenceValue = animStatesComponent.states[0];
                        }
                    }

                    GUI.enabled = oldEnabled;
                }

                GUILayout.EndVertical();
            }

            if (GUILayout.Button(styles.blankContent, styles.addStyle, GUILayout.MaxWidth(styles.addRemoveButtonWidths)))
            {
                statesProperty.arraySize++;
                statesProperty.GetArrayElementAtIndex(arraySize).objectReferenceValue = animStatesComponent.states[0];
            }


            m_StateObject.ApplyModifiedProperties();
        }