private bool DrawEvent(AnimationEvent animationEvent, AnimationState containingState)
        {
            bool shouldDelete = false;

            EditorGUI.BeginChangeCheck();
            EditorUtilities.DrawHorizontal(() =>
            {
                EditorGUILayout.LabelField("name", GUILayout.Width(50f));
                animationEvent.name = EditorGUILayout.TextField(animationEvent.name);
            });
            EditorUtilities.DrawHorizontal(() =>
            {
                EditorGUILayout.LabelField("time", GUILayout.Width(50f));
                animationEvent.time = EditorGUILayout.Slider((float)animationEvent.time, 0f, containingState.Duration);
            });
            EditorUtilities.DrawHorizontal(() =>
            {
                GUILayout.FlexibleSpace();
                if (GUILayout.Button($"Delete '{animationEvent.name}'"))
                {
                    shouldDelete = true;
                }
            });
            if (EditorGUI.EndChangeCheck())
            {
                EditorUtilities.SetDirty(animationPlayer);
            }

            return(shouldDelete);
        }
        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, 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();
                }
            });
        }
Esempio n. 4
0
        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();
            }
        }