private void DrawRuntimeDebugData() { if (!Application.isPlaying) { return; } EditorGUILayout.LabelField("Current blend variable values:"); var blendVars = animationPlayer.GetBlendVariables(); EditorUtilities.DrawIndented(() => { foreach (var blendVar in blendVars) { EditorGUILayout.BeginHorizontal(); { EditorGUILayout.LabelField(blendVar, GUILayout.Width(100f)); EditorGUILayout.LabelField(animationPlayer.GetBlendVar(blendVar).ToString()); } EditorGUILayout.EndHorizontal(); } }); EditorUtilities.Splitter(); if (!animationPlayer.gameObject.scene.IsValid()) { //is looking at the prefab at runtime, don't attempt to draw the graph! return; } for (int i = 0; i < animationPlayer.GetStateCount(selectedLayer); i++) { EditorGUILayout.BeginHorizontal(); { string stateName = animationPlayer.layers[selectedLayer].states[i].Name; if (GUILayout.Button($"Blend to {stateName} using default transition")) { animationPlayer.Play(i, selectedLayer); } if (GUILayout.Button($"Blend to {stateName} over .5 secs")) { animationPlayer.Play(i, TransitionData.Linear(.5f), selectedLayer); } if (GUILayout.Button($"Snap to {stateName}")) { animationPlayer.SnapTo(i, selectedLayer); } } EditorGUILayout.EndHorizontal(); } EditorGUILayout.LabelField("Playing clip " + animationPlayer.GetPlayingState(selectedLayer)); for (int i = animationPlayer.GetStateCount(selectedLayer) - 1; i >= 0; i--) { EditorGUILayout.LabelField("Current weigth for state " + i + ": " + animationPlayer.GetStateWeight(i, selectedLayer)); } }
public override void OnInspectorGUI() { HandleInitialization(true); if (animationPlayer.layers == null || animationPlayer.layers.Length == 0) { return; } EditorUtilities.Splitter(); DrawLayerSelection(); if (animationPlayer.layers.Length == 0) { return; //Deleted last layer in DrawLayerSelection } if (selectedState == -1 && animationPlayer.layers[selectedLayer].states.Count > 0) { selectedState.SetTo(0); //Handle adding a state for the first time. } GUILayout.Space(10f); DrawSelectedLayer(); GUILayout.Space(10f); EditorUtilities.Splitter(); var numStatesBefore = animationPlayer.layers[selectedLayer].states.Count; StateSelectionAndAdditionDrawer.Draw(animationPlayer, selectedLayer, selectedState, selectedEditMode, this); if (numStatesBefore != animationPlayer.layers[selectedLayer].states.Count) { Repaint(); return; } DrawSelectedState(); EditorUtilities.Splitter(); EditorGUILayout.LabelField("Default transition"); EditorUtilities.RecordUndo(animationPlayer, "Change default transition", () => { animationPlayer.defaultTransition = AnimationTransitionDrawer.DrawTransitionData(animationPlayer.defaultTransition); }); EditorUtilities.Splitter(); DrawRuntimeDebugData(); }
private void DrawEvents() { if (animationPlayer.layers[selectedLayer].states.Count == 0) { EditorGUILayout.LabelField("No states on layer, can't make events"); return; } var state = animationPlayer.GetState(selectedState, selectedLayer); int indexToDelete = -1; EditorGUILayout.LabelField($"Animation events for {state.Name}"); EditorUtilities.Splitter(); EditorUtilities.DrawIndented(() => { for (var i = 0; i < state.animationEvents.Count; i++) { if (DrawEvent(state.animationEvents[i], state)) { indexToDelete = i; } if (i != state.animationEvents.Count - 1) { GUILayout.Space(5); } } }); if (indexToDelete != -1) { state.animationEvents.RemoveAt(indexToDelete); } EditorUtilities.Splitter(); EditorUtilities.DrawHorizontal(() => { GUILayout.FlexibleSpace(); if (GUILayout.Button("Create new event")) { EditorUtilities.RecordUndo(animationPlayer, $"Added animation event to {state.Name}"); state.animationEvents.Add(new AnimationEvent { name = "New Event" }); MarkDirty(); } }); }
public static void DrawTransitions(AnimationPlayer animationPlayer, int selectedLayer, int selectedStateIdx, PersistedShortPair selectedToStateAndTransition, string[] stateNamesInLayer) { var layer = animationPlayer.layers[selectedLayer]; if (layer.states.Count == 0) { EditorGUILayout.LabelField("No states, can't define transitions"); return; } var(toStateIndex, transitionIndex) = selectedToStateAndTransition; var selectedState = layer.states[selectedStateIdx]; var selectedToState = layer.states.GetIfInBounds(toStateIndex); var allTransitionsBetweenStates = GetTransitionsBetweenStates(layer, selectedState, selectedToState); var selectedTransition = allTransitionsBetweenStates.GetIfInBounds(transitionIndex); var fromStateName = selectedState.Name; var toStateName = selectedToState.Name; void SetTransition(StateTransition transition) { var newStateIndex = layer.states.IndexOf(transition.ToState); var newTransitionIndex = GetTransitionsBetweenStates(layer, selectedState, transition.ToState).IndexOf(transition); selectedToStateAndTransition.SetTo(((short)newStateIndex, (short)newTransitionIndex)); } EditorUtilities.Splitter(); DrawTransitionSelection(layer, selectedState, fromStateName, selectedTransition, SetTransition); DrawCreateNewTransition(animationPlayer, SetTransition, stateNamesInLayer, fromStateName, toStateName, selectedState, layer); EditorUtilities.Splitter(); DrawSelectedTransition(animationPlayer, selectedTransition, fromStateName, toStateName, layer, transitionsFromState); EditorUtilities.Splitter(); DrawDefaultTransition(animationPlayer); }
private void DrawRuntimeDebugData() { if (!Application.isPlaying) { return; } EditorGUILayout.LabelField("Current blend variable values:"); var blendVars = animationPlayer.GetBlendVariables(); EditorUtilities.DrawIndented(() => { foreach (var blendVar in blendVars) { EditorGUILayout.BeginHorizontal(); { EditorGUILayout.LabelField(blendVar, GUILayout.Width(100f)); EditorGUILayout.LabelField(animationPlayer.GetBlendVar(blendVar).ToString()); } EditorGUILayout.EndHorizontal(); } }); EditorUtilities.Splitter(); if (!animationPlayer.gameObject.scene.IsValid()) { //is looking at the prefab at runtime, don't attempt to draw the graph! return; } EditorGUILayout.LabelField("Playing clip " + animationPlayer.GetPlayingState(selectedLayer)); for (int i = animationPlayer.GetStateCount(selectedLayer) - 1; i >= 0; i--) { EditorGUILayout.LabelField("Current weigth for state " + i + ": " + animationPlayer.GetStateWeight(i, selectedLayer)); } }
public static void DrawTransitions(AnimationPlayer animationPlayer, PersistedInt selectedLayer, PersistedInt selectedStateIdx, PersistedInt selectedToStateIdx, string[] stateNamesInLayer) { var layer = animationPlayer.layers[selectedLayer]; if (layer.states.Count == 0) { EditorGUILayout.LabelField("No states, can't define transitions"); return; } var selectedState = layer.states[selectedStateIdx]; var selectedToState = layer.states[selectedToStateIdx]; var selectedTransition = layer.transitions.Find(t => t.FromState == selectedState && t.ToState == selectedToState); var fromStateName = selectedState.Name; var toStateName = selectedToState.Name; if (selectedTransition != null) { EditorGUILayout.LabelField($"Selected transition: From \"{fromStateName}\" to \"{toStateName}\""); EditorUtilities.RecordUndo(animationPlayer, $"Edit of transition from {fromStateName} to {toStateName}"); EditorUtilities.DrawIndented(() => { selectedTransition.transitionData = DrawTransitionData(selectedTransition.transitionData); GUILayout.Space(20f); EditorGUILayout.BeginHorizontal(); GUILayout.FlexibleSpace(); if (EditorUtilities.AreYouSureButton("Clear transition", "Are you sure?", "Clear_Transition_" + fromStateName + "_" + toStateName, 1f, GUILayout.Width(150f))) { EditorUtilities.RecordUndo(animationPlayer, $"Clear transition from {fromStateName} to {toStateName}"); layer.transitions.Remove(selectedTransition); } EditorGUILayout.EndHorizontal(); }); } EditorUtilities.Splitter(); var transitionsFromState = layer.transitions.Where(t => t.FromState == selectedState).ToList(); if (transitionsFromState.Count == 0) { EditorGUILayout.LabelField($"No defined transitions from {fromStateName}"); } else { EditorGUILayout.LabelField($"Transitions from {fromStateName}:"); EditorGUILayout.Space(); EditorUtilities.DrawHorizontal(() => { GUILayout.FlexibleSpace(); EditorUtilities.DrawVertical(() => { EditorUtilities.DrawIndented(() => { foreach (var transition in transitionsFromState) { EditorGUI.BeginDisabledGroup(transition == selectedTransition); if (GUILayout.Button(transition.ToState.Name, GUILayout.MinWidth(100f))) { selectedToStateIdx.SetTo(layer.states.IndexOf(transition.ToState)); } EditorGUI.EndDisabledGroup(); } }); }); }); } EditorGUILayout.Space(); EditorUtilities.DrawHorizontal(() => { GUILayout.FlexibleSpace(); if (GUILayout.Button("Create new transition")) { GenericMenu menu = new GenericMenu(); foreach (var state in stateNamesInLayer) { menu.AddItem(new GUIContent($"Transition from {fromStateName} to {state}"), false, () => { EditorUtilities.RecordUndo(animationPlayer, $"Adding transition from {fromStateName} to {toStateName}"); var newState = new StateTransition { FromState = selectedState, ToState = layer.states.Find(s => s.Name == state), transitionData = TransitionData.Linear(.2f) }; layer.transitions.Add(newState); selectedToStateIdx.SetTo(layer.states.FindIndex(s => s.Name == state)); }); } menu.ShowAsContext(); } }); }
private static void Draw2DBlendTree(BlendTree2D state, ref bool markDirty) { state.blendVariable = EditorUtilities.TextField("First blend variable", state.blendVariable, 120f); state.blendVariable2 = EditorUtilities.TextField("Second blend variable", state.blendVariable2, 120f); EditorGUI.indentLevel++; if (state.blendTree == null) { state.blendTree = new System.Collections.Generic.List <BlendTreeEntry2D>(); } int swapIndex = -1; for (var i = 0; i < state.blendTree.Count; i++) { var blendTreeEntry = state.blendTree[i]; var oldClip = blendTreeEntry.clip; blendTreeEntry.clip = EditorUtilities.ObjectField("Clip", blendTreeEntry.clip, 150f, 200f); if (blendTreeEntry.clip != oldClip && blendTreeEntry.clip != null) { markDirty |= state.OnClipAssigned(blendTreeEntry.clip); } EditorGUILayout.BeginHorizontal(); { blendTreeEntry.threshold1 = EditorUtilities.FloatField($"When '{state.blendVariable}' =", blendTreeEntry.threshold1, 150f, 200f); EditorGUI.BeginDisabledGroup(i == 0); if (GUILayout.Button("\u2191", upDownButtonStyle, upDownButtonOptions)) { swapIndex = i; } EditorGUI.EndDisabledGroup(); } EditorGUILayout.EndHorizontal(); EditorGUILayout.BeginHorizontal(); { blendTreeEntry.threshold2 = EditorUtilities.FloatField($"When '{state.blendVariable2}' =", blendTreeEntry.threshold2, 150f, 200f); EditorGUI.BeginDisabledGroup(i == state.blendTree.Count - 1); if (GUILayout.Button("\u2193", upDownButtonStyle, upDownButtonOptions)) { swapIndex = i + 1; } EditorGUI.EndDisabledGroup(); // Remove 2D blend tree entry if (GUILayout.Button("Remove", GUILayout.Width(70f))) { state.blendTree.RemoveAt(i); } } EditorGUILayout.EndHorizontal(); if (i != state.blendTree.Count - 1) { EditorUtilities.Splitter(width: 350f); } } if (swapIndex != -1) { markDirty = true; state.blendTree.Swap(swapIndex, swapIndex - 1); } EditorGUI.indentLevel--; GUILayout.Space(10f); EditorGUILayout.BeginHorizontal(); if (GUILayout.Button("Add blend tree entry", GUILayout.Width(150f))) { state.blendTree.Add(new BlendTreeEntry2D()); markDirty = true; } GUILayout.FlexibleSpace(); EditorGUILayout.EndHorizontal(); }
private static void Draw1DBlendTree(BlendTree1D state, ref bool markDirty) { state.blendVariable = EditorUtilities.TextField("Blend with variable", state.blendVariable, 130f); EditorGUI.indentLevel++; int swapIndex = -1; if (state.blendTree == null) { state.blendTree = new System.Collections.Generic.List <BlendTreeEntry1D>(); } for (var i = 0; i < state.blendTree.Count; i++) { var blendTreeEntry = state.blendTree[i]; var whenLabel = $"When '{state.blendVariable}' ="; var whenLabelRequiredWidth = GUI.skin.label.CalcSize(new GUIContent(whenLabel)).x; var remainingAfterClipLabel = Screen.width - 30f - 70f - 25f - 100f; var remainingAfterWhenLabel = Screen.width - whenLabelRequiredWidth - 70f - 25f - 100f; EditorGUILayout.BeginHorizontal(); { var oldClip = blendTreeEntry.clip; blendTreeEntry.clip = EditorUtilities.ObjectField("Clip", blendTreeEntry.clip, 30f, remainingAfterClipLabel); if (blendTreeEntry.clip != oldClip) { if (blendTreeEntry.clip != null) { state.OnClipAssigned(blendTreeEntry.clip); } markDirty = true; } EditorGUI.BeginDisabledGroup(i == 0); if (GUILayout.Button("\u2191", upDownButtonStyle, upDownButtonOptions)) { swapIndex = i; } EditorGUI.EndDisabledGroup(); } EditorGUILayout.EndHorizontal(); EditorGUILayout.BeginHorizontal(); { blendTreeEntry.threshold = EditorUtilities.FloatField(whenLabel, blendTreeEntry.threshold, whenLabelRequiredWidth, remainingAfterWhenLabel); EditorGUI.BeginDisabledGroup(i == state.blendTree.Count - 1); if (GUILayout.Button("\u2193", upDownButtonStyle, upDownButtonOptions)) { swapIndex = i + 1; } EditorGUI.EndDisabledGroup(); // Remove 1D blend tree entry if (GUILayout.Button("Remove", GUILayout.Width(70f))) { state.blendTree.RemoveAt(i); } } EditorGUILayout.EndHorizontal(); if (i != state.blendTree.Count - 1) { EditorUtilities.Splitter(width: Screen.width - 100f); } } if (swapIndex != -1) { markDirty = true; state.blendTree.Swap(swapIndex, swapIndex - 1); } EditorGUI.indentLevel--; GUILayout.Space(10f); EditorGUILayout.BeginHorizontal(); if (GUILayout.Button("Add blend tree entry", GUILayout.Width(150f))) { state.blendTree.Add(new BlendTreeEntry1D()); markDirty = true; } GUILayout.FlexibleSpace(); EditorGUILayout.EndHorizontal(); }
private void DrawAnimationStatePreview() { EditorUtilities.Splitter(); var oldPreviewMode = previewMode; EditorUtilities.DrawHorizontal(() => { EditorGUILayout.LabelField("Preview mode"); EditorGUI.BeginDisabledGroup(previewMode == PreviewMode.Automatic); if (GUILayout.Button("Automatic")) { previewMode = PreviewMode.Automatic; } EditorGUI.EndDisabledGroup(); EditorGUI.BeginDisabledGroup(previewMode == PreviewMode.Manual); if (GUILayout.Button("Manual")) { previewMode = PreviewMode.Manual; } EditorGUI.EndDisabledGroup(); }); if (oldPreviewMode == PreviewMode.Manual && previewMode == PreviewMode.Automatic) { automaticPreviewLastTime = Time.realtimeSinceStartup; } if (previewMode == PreviewMode.Manual) { var oldPreviewTime = previewTime; previewTime = EditorGUILayout.Slider(previewTime, 0f, currentPreviewedState.Duration); previewGraph.Evaluate(previewTime - oldPreviewTime); } else { var currentTime = Time.realtimeSinceStartup; var deltaTime = currentTime - automaticPreviewLastTime; automaticPreviewLastTime = currentTime; previewTime = (previewTime + deltaTime) % currentPreviewedState.Duration; previewGraph.Evaluate(deltaTime); var oldVal = previewTime; previewTime = EditorGUILayout.Slider(previewTime, 0f, currentPreviewedState.Duration); if (oldVal != previewTime) { previewMode = PreviewMode.Manual; } } SceneView.RepaintAll(); if (GUILayout.Button("Stop preview")) { StopPreviewing(); } }