Exemplo n.º 1
0
        private static void AddClipsAsBlendTree(AnimationPlayer animationPlayer, PersistedInt selectedLayer, PersistedInt selectedState,
                                                AnimationPlayerEditor editor, List <AnimationClip> animationClips)
        {
            EditorUtilities.RecordUndo(animationPlayer, "Added clip to Animation Player");
            var layer          = animationPlayer.layers[selectedLayer];
            int numClipsBefore = layer.states.Count;

            var newStateName = GetUniqueStateName(BlendTree1D.DefaultName, layer.states);
            var newState     = BlendTree1D.Create(newStateName);

            foreach (var clip in animationClips)
            {
                var newEntry = new BlendTreeEntry1D
                {
                    clip = clip
                };
                newState.blendTree.Add(newEntry);
            }

            layer.states.Add(newState);

            selectedState.SetTo(numClipsBefore);

            editor.MarkDirty();
        }
Exemplo n.º 2
0
 private static void DrawCreateNewTransition(AnimationPlayer animationPlayer, Action <StateTransition> SetSelectedTransition, string[] stateNamesInLayer,
                                             string fromStateName, string toStateName, AnimationPlayerState selectedState, AnimationLayer layer)
 {
     using (new EditorGUILayout.HorizontalScope()) {
         GUILayout.FlexibleSpace();
         if (GUILayout.Button($"Create new transition from {fromStateName}"))
         {
             GenericMenu menu = new GenericMenu();
             foreach (var state in stateNamesInLayer)
             {
                 menu.AddItem(new GUIContent($"Transition from {fromStateName} to {state}"), false, () => {
                     //@TODO: If this is the first transition between the states, it should be set as default.
                     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);
                     SetSelectedTransition(newState);
                 });
             }
             menu.ShowAsContext();
         }
     }
 }
Exemplo n.º 3
0
        private static void DrawAddStateButtons(AnimationPlayer animationPlayer, PersistedInt selectedState, AnimationPlayerEditor editor, AnimationLayer layer)
        {
            if (GUILayout.Button("Single Clip State", buttonWidth))
            {
                EditorUtilities.RecordUndo(animationPlayer, "Add state to animation player");
                layer.states.Add(SingleClipState.Create(GetUniqueStateName(SingleClipState.DefaultName, layer.states)));
                selectedState.SetTo(layer.states.Count - 1);
                editor.MarkDirty();
            }

            if (GUILayout.Button("1D Blend Tree", buttonWidth))
            {
                EditorUtilities.RecordUndo(animationPlayer, "Add blend tree to animation player");
                layer.states.Add(BlendTree1D.Create(GetUniqueStateName(BlendTree1D.DefaultName, layer.states)));
                selectedState.SetTo(layer.states.Count - 1);
                editor.MarkDirty();
            }

            if (GUILayout.Button("2D Blend Tree", buttonWidth))
            {
                EditorUtilities.RecordUndo(animationPlayer, "Add 2D blend tree to animation player");
                layer.states.Add(BlendTree2D.Create(GetUniqueStateName(BlendTree2D.DefaultName, layer.states)));
                selectedState.SetTo(layer.states.Count - 1);
                editor.MarkDirty();
            }
        }
Exemplo n.º 4
0
        private static void DoDragAndDrop(AnimationPlayer animationPlayer, PersistedInt selectedLayer, PersistedInt selectedState, AnimationPlayerEditor editor)
        {
            var   dragAndDropRect = EditorUtilities.ReserveRect(GUILayout.Height(62f));
            Event evt             = Event.current;

            GUI.Box(dragAndDropRect, "Drag clips here to add\nthem to the player!", dragAndDropBoxStyle);

            switch (evt.type)
            {
            case EventType.DragUpdated:
            case EventType.DragPerform:
                if (!dragAndDropRect.Contains(evt.mousePosition))
                {
                    return;
                }

                DragAndDrop.visualMode = DragAndDropVisualMode.Copy;

                if (evt.type == EventType.DragPerform)
                {
                    DragAndDrop.AcceptDrag();

                    var animationClips = DragAndDrop.objectReferences.FilterByType <AnimationClip>().ToList();
                    foreach (var obj in DragAndDrop.objectReferences.FilterByType <GameObject>())
                    {
                        var assetPath = AssetDatabase.GetAssetPath(obj);
                        if (!(AssetImporter.GetAtPath(assetPath) is ModelImporter))
                        {
                            return;
                        }

                        animationClips.AddRange(AssetDatabase.LoadAllAssetsAtPath(assetPath).FilterByType <AnimationClip>().
                                                Where(clip => (clip.hideFlags & HideFlags.HideInHierarchy) == 0));
                    }

                    if (animationClips.Count > 0)
                    {
                        EditorUtilities.RecordUndo(animationPlayer, "Added clip to Animation Player");
                        var layer          = animationPlayer.layers[selectedLayer];
                        int numClipsBefore = layer.states.Count;

                        foreach (var clip in animationClips)
                        {
                            var newStateName = GetUniqueStateName(clip.name, layer.states);
                            var newState     = SingleClipState.Create(newStateName, clip);
                            layer.states.Add(newState);
                        }

                        selectedState.SetTo(numClipsBefore);

                        editor.MarkDirty();
                    }
                }

                break;
            }
        }
Exemplo n.º 5
0
        private static void DeleteState(AnimationPlayer animationPlayer, AnimationLayer layer, PersistedInt selectedState)
        {
            EditorUtilities.RecordUndo(animationPlayer, "Deleting state " + layer.states[selectedState].Name);
            layer.transitions.RemoveAll(transition => transition.FromState == layer.states[selectedState] ||
                                        transition.ToState == layer.states[selectedState]);
            layer.states.RemoveAt(selectedState);

            if (selectedState == layer.states.Count) //was last state
            {
                selectedState.SetTo(selectedState - 1);
            }
        }
        private void DrawLayerSelection()
        {
            var numLayers = animationPlayer.layers.Length;

            selectedLayer.SetTo(Mathf.Clamp(selectedLayer, 0, numLayers));

            var twoLines = Screen.width < 420f;

            EditorGUILayout.BeginHorizontal();
            {
                GUILayout.FlexibleSpace();

                selectedLayer.SetTo(EditorUtilities.DrawLeftButton(selectedLayer));
                EditorGUILayout.LabelField("Selected layer: " + selectedLayer, GUILayout.Width(selectedLayerWidth));
                selectedLayer.SetTo(EditorUtilities.DrawRightButton(selectedLayer, numLayers));

                if (twoLines)
                {
                    GUILayout.FlexibleSpace();
                    EditorGUILayout.EndHorizontal();
                    EditorGUILayout.BeginHorizontal();
                    GUILayout.FlexibleSpace();
                }
                else
                {
                    GUILayout.Space(10f);
                }

                if (GUILayout.Button("Add layer", GUILayout.Width(100f)))
                {
                    EditorUtilities.RecordUndo(animationPlayer, "Add layer to animation player");
                    EditorUtilities.ExpandArrayByOne(ref animationPlayer.layers, AnimationLayer.CreateLayer);
                    selectedLayer.SetTo(animationPlayer.layers.Length - 1);
                    MarkDirty();
                }

                EditorGUI.BeginDisabledGroup(numLayers < 2);
                {
                    if (EditorUtilities.AreYouSureButton("Delete layer", "Are you sure?", "DeleteLayer" + selectedLayer, 1f, GUILayout.Width(100f)))
                    {
                        EditorUtilities.RecordUndo(animationPlayer, "Delete layer from animation player");
                        EditorUtilities.DeleteIndexFromArray(ref animationPlayer.layers, selectedLayer);
                        selectedLayer.SetTo(Mathf.Max(0, selectedLayer - 1));
                        MarkDirty();
                    }
                }
                EditorGUI.EndDisabledGroup();

                GUILayout.FlexibleSpace();
            }
            EditorGUILayout.EndHorizontal();
        }
        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();
                }
            });
        }
Exemplo n.º 9
0
        private static void AddClipsAsSeperateStates(AnimationPlayer animationPlayer, PersistedInt selectedLayer, PersistedInt selectedState,
                                                     AnimationPlayerEditor editor, List <AnimationClip> animationClips)
        {
            EditorUtilities.RecordUndo(animationPlayer, "Added clip to Animation Player");
            var layer          = animationPlayer.layers[selectedLayer];
            int numClipsBefore = layer.states.Count;

            foreach (var clip in animationClips)
            {
                var newStateName = GetUniqueStateName(clip.name, layer.states);
                var newState     = SingleClip.Create(newStateName, clip);
                layer.states.Add(newState);
            }

            selectedState.SetTo(numClipsBefore);

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