Esempio n. 1
0
        public void DrawAudioProperties(SerializedObject m_State)
        {
            SerializedProperty m_Property;

            m_Property = m_State.FindProperty("sounds");

            SerializedProperty property;

            for (int i = 0; i < m_Property.arraySize; i++)
            {
                GUILayout.BeginVertical(GUI.skin.box);
                property = m_Property.GetArrayElementAtIndex(i);
                SerializedProperty prop;

                prop = property.FindPropertyRelative("audioClip");
                EditorGUILayout.PropertyField(prop);

                prop = property.FindPropertyRelative("audioVolume");
                EditorGUILayout.PropertyField(prop);

                prop = property.FindPropertyRelative("audioPitch");
                EditorGUILayout.PropertyField(prop);

                prop = property.FindPropertyRelative("audioPitchRandomness");
                EditorGUILayout.PropertyField(prop, new GUIContent("Random Pitch (+/-)"));

                prop = property.FindPropertyRelative("loopAudio");
                EditorGUILayout.PropertyField(prop);

                if (GUILayout.Button("Play"))
                {
                    PlaySound(i);
                }

                GUILayout.EndVertical();
            }

            // Plus and minus buttons
            GUILayout.BeginHorizontal();
            AIBehaviorsStyles styles = new AIBehaviorsStyles();

            if (GUILayout.Button(styles.blankContent, styles.addStyle, GUILayout.MaxWidth(styles.addRemoveButtonWidths)))
            {
                m_Property.arraySize++;
            }
            GUI.enabled = m_Property.arraySize > 0;
            if (GUILayout.Button(styles.blankContent, styles.removeStyle, GUILayout.MaxWidth(styles.addRemoveButtonWidths)))
            {
                m_Property.arraySize--;
            }
            GUI.enabled = true;
            GUILayout.EndHorizontal();
        }
    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();
        states = fsm.GetAllStates();

        for ( int i = 0; i < fsm.stateCount; 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;
                }
            }
        }
    }
Esempio n. 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();
        }
    }
    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 AIBehaviorsAnimationStates;
        transform = animStates.transform;

        InitStatesGameObject();
    }
Esempio n. 5
0
        protected virtual void DrawChangeVariableProperties(SerializedObject m_Object, AIBehaviors fsm)
        {
            string[]           varNames = fsm.GetVariableNames();
            SerializedProperty property = m_Object.FindProperty("variables");

            // Add button
            styles = new AIBehaviorsStyles();
            if (GUILayout.Button(styles.blankContent, styles.addStyle, GUILayout.MaxWidth(styles.addRemoveButtonWidths)))
            {
                if (varNames.Length > property.arraySize)
                {
                    property.arraySize++;
                    m_Object.ApplyModifiedProperties();
                }
                else
                {
                    Debug.LogError("You need to add more variables to edit them");
                }
            }

            for (int i = 0; i < variables.Length; i++)
            {
                SerializedProperty arrayElement  = property.GetArrayElementAtIndex(i);
                SerializedProperty selectedIndex = arrayElement.FindPropertyRelative("selectedIndex");
                GUILayout.BeginVertical(GUI.skin.box);
                {
                    // Not alowing to have the same variable selected more than once
                    int tempIndex = EditorGUILayout.Popup(selectedIndex.intValue, varNames);
                    if (CheckExistingIndex(tempIndex, i))
                    {
                        selectedIndex.intValue = FindAvaiableIndex(i);
                        GUILayout.EndVertical();
                        break;
                    }
                    else
                    {
                        selectedIndex.intValue = tempIndex;
                    }

                    AiBehaviorVariable selectedVar = fsm.userVariables[selectedIndex.intValue];

                    SerializedProperty changeVariableMode = arrayElement.FindPropertyRelative("changeVariableMode");
                    if (selectedVar.IsBoolean())
                    {
                        changeVariableMode.enumValueIndex = 0;                         // A bool can only be changed, not incremented
                    }
                    EditorGUILayout.PropertyField(changeVariableMode, new GUIContent(""));

                    SerializedProperty changingValue = arrayElement.FindPropertyRelative("changingValue");
                    if (selectedVar.IsFloat())
                    {
                        changingValue.floatValue = EditorGUILayout.FloatField(changingValue.floatValue);
                    }
                    else if (selectedVar.IsInteger())
                    {
                        changingValue.floatValue = (int)EditorGUILayout.IntField((int)changingValue.floatValue);
                    }
                    else if (selectedVar.IsBoolean())
                    {
                        changingValue.floatValue = EditorGUILayout.Toggle(changingValue.floatValue == 1f) ? 1f : 0f;
                    }

                    // Remove button
                    if (GUILayout.Button(styles.blankContent, styles.removeStyle, GUILayout.MaxWidth(styles.addRemoveButtonWidths)))
                    {
                        property.DeleteArrayElementAtIndex(i);
                        break;
                    }
                }
                GUILayout.EndVertical();
            }
        }
    public static void DrawAnimationFields(SerializedObject m_StateObject)
    {
        SerializedProperty m_animationStates = m_StateObject.FindProperty("animationStatesComponent");
        SerializedProperty statesProperty = m_StateObject.FindProperty("animationStates");
        int arraySize = statesProperty.arraySize;
        AIBehaviorsAnimationStates animStatesComponent;// = m_animationStates.objectReferenceValue as AIBehaviorsAnimationStates;
        AIBehaviorsStyles styles = new AIBehaviorsStyles();
        bool newFoldoutValue;

        const string foldoutValueKey = "AIBehaviors_AnimationsFoldout";

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

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

            gotFoldoutValue = true;
        }

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

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

        if ( !foldoutValue )
            return;

        // Is the component assigned?
        if ( m_animationStates.objectReferenceValue != null && animStatesComponent != null )
        {
            AIBehaviorsAnimationState[] states = new AIBehaviorsAnimationState[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 AnimationStates 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 AIBehaviorsAnimationState;
                        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];

                            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, "animationStates");
                                GUILayout.EndHorizontal();
                                break;
                            }
                            GUI.enabled = oldEnabled;
                        }
                        GUILayout.EndHorizontal();
                    }
                    else
                    {
                        Debug.Log("Add");
                        statesProperty.GetArrayElementAtIndex(i).objectReferenceValue = animStatesComponent.states[0];
                    }
                }

                GUI.enabled = oldEnabled;
            }
        }
        else
        {
            Debug.Log(animStatesComponent == null);
            Debug.Log(m_animationStates.objectReferenceValue == null);
        }

        m_StateObject.ApplyModifiedProperties();
    }