void EnemyAIState() { if (agent.speed == 1) { animState = AIAnimationState.Walk; } else if (agent.speed == 3) { animState = AIAnimationState.Run; } else { animState = AIAnimationState.Idle; } if (animState == AIAnimationState.Walk) { anim.SetTrigger("walkTrigger"); } else if (animState == AIAnimationState.Run) { anim.SetTrigger("runTrigger"); } else { anim.SetTrigger("idleTrigger"); } }
static int GetCurrentStateIndex(AIAnimationState curState, AIAnimationState[] states) { for (int i = 0; i < states.Length; i++) { if (curState == states[i]) { return(i); } } return(-1); }
//Not used...Left them as reference void PlayerAnimState() { if (agent.speed == 1) { animState = AIAnimationState.Walk; } else if (agent.speed == 4) { animState = AIAnimationState.Run; } else { animState = AIAnimationState.Idle; } }
void OnPlayAnimation(AIAnimationState animationState) { Debug.Log("Play " + animationState.name); }
public override void OnInspectorGUI() { m_Object.Update(); int arraySize = m_AnimationStatesCount.intValue; AIAnimationState[] states = new AIAnimationState[arraySize + 1]; SerializedProperty animationType = m_Object.FindProperty("animationType"); EditorGUILayout.Separator(); EditorGUILayout.PropertyField(animationType); 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 AIAnimationState; if (states[i] == null) { AIBehaviorsAssignableObjectArray.RemoveObjectAtIndex(m_Object, i, "states"); continue; } GUILayout.BeginHorizontal(); if (string.IsNullOrEmpty(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 <AIAnimationState>(); } 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 AIAnimationState); break; } GUI.enabled = oldEnabled; GUILayout.Space(10); GUILayout.EndHorizontal(); GUILayout.Space(2); if (states[i].foldoutOpen) { DrawAnimProperties(states[i], GetAnimationType((AnimationType)animationType.intValue)); } else { SerializedObject serializedAnimState = new SerializedObject(states[i]); serializedAnimState.ApplyModifiedProperties(); } } if (arraySize == 0) { m_Object.FindProperty(kArraySize).intValue++; animationStatesProp.GetArrayElementAtIndex(0).objectReferenceValue = statesGameObject.AddComponent <AIAnimationState>(); } EditorGUILayout.Separator(); m_Object.ApplyModifiedProperties(); }
public static void DrawAnimProperties(AIAnimationState animState, AnimationType animationType) { 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 (animationType == AnimationType.TwoD) { 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 { if (animationType == AnimationType.Mecanim) { SerializedProperty useGoToState = m_animState.FindProperty("crossFadeIn"); SerializedProperty exitWithGoToState = m_animState.FindProperty("crossFadeOut"); EditorGUILayout.PropertyField(useGoToState); EditorGUILayout.PropertyField(exitWithGoToState); } SerializedProperty transitionTime = m_animState.FindProperty("transitionTime"); EditorGUILayout.PropertyField(transitionTime); speedTooltip = "This is a normalized value\n\n0.5 = half speed\n1.0 = normal speed\n2.0 = double speed"; } if (animationType != AnimationType.Mecanim) { EditorGUILayout.PropertyField(m_speed, new GUIContent("Speed " + (animationType == AnimationType.TwoD ? "(FPS)" : ""), speedTooltip)); EditorGUILayout.PropertyField(m_wrapMode); } EditorGUILayout.Separator(); m_animState.ApplyModifiedProperties(); }
public static void DrawAnimationFields(SerializedObject m_StateObject, bool usesMultipleAnimations) { SerializedProperty m_animationStates = m_StateObject.FindProperty("animationStatesComponent"); SerializedProperty statesProperty = m_StateObject.FindProperty("animationStates"); int arraySize = statesProperty.arraySize; AIAnimationStates animStatesComponent; AIBehaviorsStyles styles = new AIBehaviorsStyles(); bool newFoldoutValue; bool hadNullAnimation = false; const string foldoutValueKey = "AIBehaviors_AnimationsFoldout"; AssignAnimationStatesComponent(m_StateObject); animStatesComponent = m_animationStates.objectReferenceValue as AIAnimationStates; 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); } // 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) { GUILayout.BeginVertical(GUI.skin.box); AIAnimationState[] states = new AIAnimationState[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 AIAnimationStates 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 AIAnimationState; 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, "animationStates"); GUILayout.EndHorizontal(); break; } GUI.enabled = oldEnabled; } } GUILayout.EndHorizontal(); } else { statesProperty.GetArrayElementAtIndex(i).objectReferenceValue = animStatesComponent.states[0]; } } GUI.enabled = oldEnabled; } GUILayout.EndVertical(); } m_StateObject.ApplyModifiedProperties(); }