public void OnAnimationState(AIBehaviorsAnimationState animState)
    {
        if ( hasAnimationComponent && animState != null )
        {
            string stateName = animState.name;

            if ( anim[stateName] != null )
            {
                anim[stateName].wrapMode = animState.animationWrapMode;
                anim[stateName].speed = animState.speed;
                anim.CrossFade(stateName);
            }
            else
                Debug.LogWarning("The animation state \"" + stateName + "\" couldn't be found.");
        }
    }
    public static void DrawAnimProperties(AIBehaviorsAnimationState animState, bool is3D)
    {
        SerializedObject m_animState = new SerializedObject(animState);
        string speedTooltip = "";

        m_animState.Update();

        SerializedProperty m_animName = m_animState.FindProperty("name");
        SerializedProperty m_speed = m_animState.FindProperty("speed");
        SerializedProperty m_wrapMode = m_animState.FindProperty("animationWrapMode");

        EditorGUILayout.Separator();

        EditorGUILayout.PropertyField(m_animName);

        if ( !is3D )
        {
            SerializedProperty m_startFrame = m_animState.FindProperty("startFrame");
            SerializedProperty m_endFrame = m_animState.FindProperty("endFrame");
            EditorGUILayout.PropertyField(m_startFrame);
            EditorGUILayout.PropertyField(m_endFrame);

            if ( m_startFrame.intValue < 0 )
                m_startFrame.intValue = 0;

            if ( m_endFrame.intValue < 0 )
                m_endFrame.intValue = 0;

            speedTooltip = "This is a frames persecond value, IE: 30";
        }
        else
        {
            speedTooltip = "This is a normalized value\n\n0.5 = half speed\n1.0 = normal speed\n2.0 = double speed";
        }

        EditorGUILayout.PropertyField(m_speed, new GUIContent("Speed " + (is3D ? "" : "(FPS)"), speedTooltip));
        EditorGUILayout.PropertyField(m_wrapMode);

        EditorGUILayout.Separator();

        m_animState.ApplyModifiedProperties();
    }
    public override void OnInspectorGUI()
    {
        m_Object.Update();

        int arraySize = m_AnimationStatesCount.intValue;
        AIBehaviorsAnimationState[] states = new AIBehaviorsAnimationState[arraySize+1];
        SerializedProperty is3D = m_Object.FindProperty("is3D");

        EditorGUILayout.Separator();
        EditorGUILayout.PropertyField(is3D);

        for ( int i = 0; i < arraySize; i++ )
        {
            string stateNameLabel = "";
            bool oldEnabled = GUI.enabled;

            if ( m_Object.FindProperty(string.Format(kArrayData, i)) == null )
            {
                AIBehaviorsAssignableObjectArray.RemoveObjectAtIndex(m_Object, i, "states");
                continue;
            }

            states[i] = m_Object.FindProperty(string.Format(kArrayData, i)).objectReferenceValue as AIBehaviorsAnimationState;

            if ( states[i] == null )
            {
                AIBehaviorsAssignableObjectArray.RemoveObjectAtIndex(m_Object, i, "states");
                continue;
            }

            GUILayout.BeginHorizontal();

            if ( states[i].name == null || states[i].name == "" )
                stateNameLabel = "Untitled animation";
            else
                stateNameLabel = states[i].name;

            states[i].foldoutOpen = EditorGUILayout.Foldout(states[i].foldoutOpen, stateNameLabel, EditorStyles.foldoutPreDrop);

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

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

            GUI.enabled = true;
            if ( GUILayout.Button(styles.blankContent, styles.addStyle, GUILayout.MaxWidth(styles.addRemoveButtonWidths)) )
            {
                animationStatesProp.InsertArrayElementAtIndex(i);
                animationStatesProp.GetArrayElementAtIndex(i+1).objectReferenceValue = statesGameObject.AddComponent<AIBehaviorsAnimationState>();
            }

            GUI.enabled = arraySize > 1;
            if ( GUILayout.Button(styles.blankContent, styles.removeStyle, GUILayout.MaxWidth(styles.addRemoveButtonWidths)) )
            {
                AIBehaviorsAssignableObjectArray.RemoveObjectAtIndex(m_Object, i, "states");
                DestroyImmediate(m_Object.targetObject as AIBehaviorsAnimationState);
                break;
            }
            GUI.enabled = oldEnabled;

            GUILayout.Space(10);

            GUILayout.EndHorizontal();

            GUILayout.Space(2);

            if ( states[i].foldoutOpen )
                DrawAnimProperties(states[i], is3D.boolValue);
        }

        if ( arraySize == 0 )
        {
            m_Object.FindProperty(kArraySize).intValue++;
            animationStatesProp.GetArrayElementAtIndex(0).objectReferenceValue = statesGameObject.AddComponent<AIBehaviorsAnimationState>();
        }

        EditorGUILayout.Separator();

        m_Object.ApplyModifiedProperties();
    }
    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();
    }
    static int GetCurrentStateIndex(AIBehaviorsAnimationState curState, AIBehaviorsAnimationState[] states)
    {
        for ( int i = 0; i < states.Length; i++ )
        {
            if ( curState == states[i] )
                return i;
        }

        return -1;
    }
 void OnPlayAnimation(AIBehaviorsAnimationState animationState)
 {
     Debug.Log("Play " + animationState.name);
 }