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));
            }
        }
Exemplo n.º 2
0
        private static void DrawTransitionSelection(AnimationLayer layer, AnimationPlayerState selectedState, string fromStateName,
                                                    StateTransition selectedTransition, Action <StateTransition> SetSelectedTransition)
        {
            transitionsFromState.Clear();
            foreach (var transition in layer.transitions)
            {
                if (transition.FromState == selectedState)
                {
                    transitionsFromState.Add(transition);
                }
            }

            transitionsFromState.Sort((t1, t2) => t1.ToState.Name.CompareTo(t2.ToState.Name)); //@TODO: Grab hold of NaturalComparrison from Mesmer, use it, so state_2 doesn't sort under state_11

            if (transitionsFromState.Count == 0)
            {
                EditorGUILayout.LabelField($"No defined transitions from {fromStateName}");
            }
            else
            {
                EditorGUILayout.LabelField($"Transitions from {fromStateName}:");

                var buttonWidth = 100f;
                foreach (var transition in transitionsFromState)
                {
                    var width = GUI.skin.button.CalcSize(new GUIContent(transition.name)).x + 20f;
                    buttonWidth = Mathf.Max(width, buttonWidth);
                }

                EditorGUILayout.Space();
                AnimationPlayerState lastToState = null;

                EditorUtilities.DrawIndented(() => {
                    for (var i = 0; i < transitionsFromState.Count; i++)
                    {
                        var transition = transitionsFromState[i];
                        if (transition.ToState != lastToState)
                        {
                            lastToState = transition.ToState;
                            EditorGUILayout.LabelField($"Transitions to {lastToState.Name}");
                        }

                        using (new GUILayout.HorizontalScope())
                            using (new EditorGUI.DisabledScope(transition == selectedTransition)) {
                                if (GUILayout.Button(transition.name, GUILayout.Width(buttonWidth)))
                                {
                                    SetSelectedTransition(transition);
                                }

                                if (transition.isDefault)
                                {
                                    EditorGUILayout.LabelField("(Default)");
                                }
                            }
                    }
                });
            }
        }
        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();
                }
            });
        }
Exemplo n.º 4
0
        private static void DrawSelectedTransition(AnimationPlayer animationPlayer, StateTransition selectedTransition, string fromStateName,
                                                   string toStateName, AnimationLayer layer, List <StateTransition> allTransitionsFromState)
        {
            if (selectedTransition != null)
            {
                EditorGUILayout.LabelField($"Selected transition: From \"{fromStateName}\" to \"{toStateName}\"");
                Undo.RecordObject(animationPlayer, $"Edit of transition from  {fromStateName} to {toStateName}");

                EditorUtilities.DrawIndented(() =>
                {
                    EditorGUI.BeginChangeCheck();
                    selectedTransition.isDefault = EditorGUILayout.Toggle("Is Default", selectedTransition.isDefault);
                    if (EditorGUI.EndChangeCheck() && selectedTransition.isDefault)
                    {
                        foreach (var transition in allTransitionsFromState)
                        {
                            if (transition != selectedTransition && transition.ToState == selectedTransition.ToState)
                            {
                                transition.isDefault = false;
                            }
                        }
                    }

                    selectedTransition.name = EditorGUILayout.TextField("Name", selectedTransition.name);

                    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)))
                    {
                        Undo.RecordObject(animationPlayer, $"Clear transition from  {fromStateName} to {toStateName}");
                        layer.transitions.Remove(selectedTransition);
                    }

                    EditorGUILayout.EndHorizontal();
                });
            }
        }
        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();
                }
            });
        }