Ejemplo n.º 1
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();
            }
        }
Ejemplo n.º 2
0
 private static void DrawAddition(AnimationPlayer animationPlayer, PersistedInt selectedLayer, PersistedInt selectedState, AnimationPlayerEditor editor,
                                  AnimationLayer layer, List <AnimationClip> multiSelectClips)
 {
     if (multiSelectClips != null && multiSelectClips.Count > 0)
     {
         EditorGUILayout.BeginVertical(buttonWidth);
         {
             DrawMultiSelectChoice(animationPlayer, selectedLayer, selectedState, multiSelectClips, editor);
         }
         EditorGUILayout.EndVertical();
         return;
     }
     EditorGUILayout.BeginVertical(buttonWidth);
     {
         EditorGUILayout.LabelField("Add new state:");
         if (DragAndDrop.objectReferences.Length > 0)
         {
             DoDragAndDrop(animationPlayer, selectedLayer, selectedState, editor);
         }
         else
         {
             DrawAddStateButtons(animationPlayer, selectedState, editor, layer);
         }
     }
     EditorGUILayout.EndVertical();
 }
Ejemplo n.º 3
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();
        }
        private void OnEnable()
        {
            animationPlayer = (AnimationPlayer)target;
            HandleInitialization(false);

            if (animationPlayer.EnsureVersionUpgraded())
            {
                EditorUtility.SetDirty(animationPlayer);
                if (animationPlayer.gameObject.scene.IsValid())
                {
                    EditorSceneManager.MarkSceneDirty(animationPlayer.gameObject.scene);
                }
            }

            Undo.undoRedoPerformed -= CheckSelectionBounds;
            Undo.undoRedoPerformed += CheckSelectionBounds;

            var instanceId = animationPlayer.GetInstanceID();

            selectedLayer    = new PersistedInt(persistedLayer + instanceId);
            selectedEditMode = new PersistedAnimationPlayerEditMode(persistedEditMode + instanceId);
            selectedState    = new PersistedInt(persistedState + instanceId);
            selectedToState  = new PersistedInt(persistedToState + instanceId);

            stateNamesNeedsUpdate = true;
        }
Ejemplo n.º 5
0
        private static bool DrawDeleteStateButton(PersistedInt selectedLayer, PersistedInt selectedState)
        {
            EditorGUILayout.BeginHorizontal();
            GUILayout.FlexibleSpace();
            var deleteThisState = EditorUtilities.AreYouSureButton("Delete state", "are you sure", "DeleteState_" + selectedState + "_" + selectedLayer, 1f);

            EditorGUILayout.EndHorizontal();
            return(deleteThisState);
        }
Ejemplo n.º 6
0
        public static void Draw(AnimationPlayer animationPlayer, PersistedInt selectedLayer, PersistedInt selectedState, AnimationPlayerEditor editor,
                                List <AnimationClip> multiSelectClips)
        {
            if (popupStyle == null)
            {
                buttonWidth        = GUILayout.Width(200f);
                selectedStateLabel = new GUIContent("Selected state: ");

                popupStyle = new GUIStyle(EditorStyles.popup)
                {
                    fontSize    = GUI.skin.button.fontSize,
                    font        = GUI.skin.button.font,
                    fontStyle   = GUI.skin.button.fontStyle,
                    fixedHeight = 20f,
                    alignment   = TextAnchor.MiddleCenter
                };
                dragAndDropBoxStyle = new GUIStyle(EditorStyles.helpBox)
                {
                    alignment = TextAnchor.MiddleCenter,
                    fontStyle = FontStyle.Bold,
                    fontSize  = 12,
                };
            }

            var layer = animationPlayer.layers[selectedLayer];

            var wrap = Screen.width < 420f;

            if (wrap)
            {
                EditorGUILayout.BeginHorizontal();
                GUILayout.FlexibleSpace();
                EditorGUILayout.BeginVertical();
                {
                    DrawSelection(layer, selectedState);
                    GUILayout.Space(10f);
                    DrawAddition(animationPlayer, selectedLayer, selectedState, editor, layer, multiSelectClips);
                }
                EditorGUILayout.EndVertical();
                GUILayout.FlexibleSpace();
                EditorGUILayout.EndHorizontal();
            }
            else
            {
                EditorGUILayout.BeginHorizontal();
                {
                    GUILayout.FlexibleSpace();

                    DrawSelection(layer, selectedState);
                    GUILayout.Space(20f);
                    DrawAddition(animationPlayer, selectedLayer, selectedState, editor, layer, multiSelectClips);

                    GUILayout.FlexibleSpace();
                }
                EditorGUILayout.EndHorizontal();
            }
        }
Ejemplo n.º 7
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;
            }
        }
Ejemplo n.º 8
0
        private static void DrawSelection(AnimationLayer selectedLayer, PersistedInt selectedState)
        {
            UpdateLayerDropdownNames(selectedLayer);

            EditorGUILayout.BeginVertical(buttonWidth);
            {
                EditorGUILayout.LabelField(selectedStateLabel, buttonWidth);
                selectedState.SetTo(EditorGUILayout.Popup(selectedState, selectedLayer.layersForEditor, popupStyle, buttonWidth));
            }
            EditorGUILayout.EndVertical();
        }
Ejemplo n.º 9
0
        private static void DoDragAndDrop(AnimationPlayer animationPlayer, PersistedInt selectedLayer, PersistedInt selectedState, AnimationPlayerEditor editor)
        {
            const int   numButtons     = 5;
            const float spacePerButton = 20f;
            const float border         = 3f;

            Debug.Log(DragAndDrop.objectReferences.PrettyPrint());

            var   dragAndDropRect = EditorUtilities.ReserveRect(GUILayout.Height((numButtons * spacePerButton) + border));
            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.OfType <AnimationClip>().ToList();
                    foreach (var obj in DragAndDrop.objectReferences.OfType <GameObject>())
                    {
                        var assetPath = AssetDatabase.GetAssetPath(obj);
                        if (!(AssetImporter.GetAtPath(assetPath) is ModelImporter))
                        {
                            return;
                        }

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

                    if (animationClips.Count == 1)
                    {
                        AddClipsAsSeperateStates(animationPlayer, selectedLayer, selectedState, editor, animationClips);
                    }
                    else if (animationClips.Count > 1)
                    {
                        editor.StartDragAndDropMultiChoice(animationClips);
                    }
                }

                break;
            }
        }
Ejemplo n.º 10
0
        public void DrawStatePreview(PersistedInt selectedLayer, PersistedInt selectedState)
        {
            //@TODO: handle changing state somehow

            if (IsShowingPreview)
            {
                DrawAnimationStatePreview();
            }
            else if (GUILayout.Button("Start previewing state"))
            {
                var state = animationPlayer.layers[selectedLayer].states[selectedState];
                StartPreviewing(state);
            }
        }
Ejemplo n.º 11
0
 private static void DrawAddition(AnimationPlayer animationPlayer, PersistedInt selectedLayer, PersistedInt selectedState, AnimationPlayerEditor editor,
                                  AnimationLayer layer)
 {
     EditorGUILayout.BeginVertical(buttonWidth);
     {
         EditorGUILayout.LabelField("Add new state:");
         if (DragAndDrop.objectReferences.Length > 0)
         {
             DoDragAndDrop(animationPlayer, selectedLayer, selectedState, editor);
         }
         else
         {
             DrawAddStateButtons(animationPlayer, selectedState, editor, layer);
         }
     }
     EditorGUILayout.EndVertical();
 }
Ejemplo n.º 12
0
        public static void DrawStateData(AnimationPlayer animationPlayer, PersistedInt selectedLayer, PersistedInt selectedState,
                                         AnimationPlayerEditor currentEditor)
        {
            var updateStateNames = false;
            var layer            = animationPlayer.layers[selectedLayer];

            if (layer.states.Count == 0)
            {
                EditorGUILayout.LabelField("No states");
                return;
            }

            if (!layer.states.IsInBounds(selectedState))
            {
                Debug.LogError("Out of bounds state: " + selectedState + " out of " + layer.states.Count + " states! Resetting to 0");
                selectedState.SetTo(0);
                return;
            }

            EditorGUILayout.LabelField("State");

            EditorGUI.indentLevel++;
            DrawStateData(layer.states[selectedState], ref updateStateNames);

            GUILayout.Space(20f);

            var deleteThisState = DrawDeleteStateButton(selectedLayer, selectedState);

            EditorGUI.indentLevel--;

            if (deleteThisState)
            {
                DeleteState(animationPlayer, layer, selectedState);
                updateStateNames = true;
            }

            if (updateStateNames)
            {
                currentEditor.MarkDirty();
            }

            currentEditor.previewer.DrawStatePreview(selectedLayer, selectedState);
        }
        private static void DrawMultiSelectChoice(AnimationPlayer animationPlayer, PersistedInt selectedLayer, PersistedInt selectedState,
                                                  List <AnimationClip> multiSelectClips, AnimationPlayerEditor editor)
        {
            EditorGUILayout.LabelField($"Adding {multiSelectClips.Count} clips! How do you want to add them?");
            if (GUILayout.Button("Seperate Clips"))
            {
                AddClipsAsSeperateStates(animationPlayer, selectedLayer, selectedState, editor, multiSelectClips);
                editor.StopDragAndDropMultiChoice();
            }

            if (GUILayout.Button("Blend Tree"))
            {
                AddClipsAsBlendTree(animationPlayer, selectedLayer, selectedState, editor, multiSelectClips);
                editor.StopDragAndDropMultiChoice();
            }

            if (GUILayout.Button("Cancel"))
            {
                editor.StopDragAndDropMultiChoice();
            }
        }
Ejemplo n.º 14
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();
                }
            });
        }
Ejemplo n.º 16
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);
            }
        }
Ejemplo n.º 17
0
        public static void DrawStateData(AnimationPlayer animationPlayer, int selectedLayer, PersistedInt selectedState, AnimationPlayerEditor currentEditor)
        {
            ReloadCheck();

            var markDirty = false;
            var layer     = animationPlayer.layers[selectedLayer];

            if (layer.states.Count == 0)
            {
                EditorGUILayout.LabelField("No states");
                return;
            }

            if (!layer.states.IsInBounds(selectedState))
            {
                Debug.LogError($"Out of bounds state: {selectedState} out of {layer.states.Count} states! Resetting to 0");
                selectedState.SetTo(0);
                return;
            }

            var stateName = layer.states[selectedState].Name;

            EditorGUILayout.LabelField($"Settings for \"{stateName}\":");

            EditorGUI.indentLevel++;
            DrawStateData(layer.states[selectedState], ref markDirty);

            GUILayout.Space(20f);

            EditorGUILayout.BeginHorizontal();
            if (selectedState == 0)
            {
                EditorGUILayout.LabelField("This is the default state of this layer", GUILayout.Width(250f));
            }
            else if (GUILayout.Button("Set as the default state of this layer", GUILayout.Width(250f)))
            {
                layer.states.Swap(selectedState, 0);
                selectedState.SetTo(0);
                markDirty = true;
            }
            GUILayout.FlexibleSpace();
            var deleteThisState = EditorUtilities.AreYouSureButton($"Delete {stateName}", "Are you sure", $"DeleteState_{selectedState}_{selectedLayer}", 1f);

            EditorGUILayout.EndHorizontal();
            EditorGUI.indentLevel--;

            GUILayout.Space(20f);

            currentEditor.previewer.DrawStatePreview(selectedLayer, selectedState);

            if (deleteThisState)
            {
                DeleteState(animationPlayer, layer, selectedState);
                markDirty = true;
            }

            if (markDirty)
            {
                currentEditor.MarkDirty();
            }
        }
Ejemplo n.º 18
0
        public static void DrawStateData(AnimationPlayer animationPlayer, PersistedInt selectedLayer, PersistedInt selectedState,
                                         AnimationPlayerEditor currentEditor)
        {
            if (reloadChecker == null)
            {
                reloadChecker       = new object();
                upDownButtonOptions = new[] { GUILayout.Width(25f), GUILayout.Height(15f) };

                upDownButtonStyle = new GUIStyle(GUI.skin.button)
                {
                    alignment = TextAnchor.UpperCenter,
                    clipping  = TextClipping.Overflow
                };
            }

            var markDirty = false;
            var layer     = animationPlayer.layers[selectedLayer];

            if (layer.states.Count == 0)
            {
                EditorGUILayout.LabelField("No states");
                return;
            }

            if (!layer.states.IsInBounds(selectedState))
            {
                Debug.LogError($"Out of bounds state: {selectedState} out of {layer.states.Count} states! Resetting to 0");
                selectedState.SetTo(0);
                return;
            }

            var stateName = layer.states[selectedState].Name;

            EditorGUILayout.LabelField($"Settings for \"{stateName}\":");

            EditorGUI.indentLevel++;
            DrawStateData(layer.states[selectedState], ref markDirty);

            GUILayout.Space(20f);

            EditorGUILayout.BeginHorizontal();
            if (selectedState == 0)
            {
                EditorGUILayout.LabelField("This is the default state of this layer", GUILayout.Width(250f));
            }
            else if (GUILayout.Button("Set as the default state of this layer", GUILayout.Width(250f)))
            {
                layer.states.Swap(selectedState, 0);
                selectedState.SetTo(0);
                markDirty = true;
            }
            GUILayout.FlexibleSpace();
            var deleteThisState = EditorUtilities.AreYouSureButton($"Delete {stateName}", "Are you sure", $"DeleteState_{selectedState}_{selectedLayer}", 1f);

            EditorGUILayout.EndHorizontal();
            EditorGUI.indentLevel--;

            if (deleteThisState)
            {
                DeleteState(animationPlayer, layer, selectedState);
                markDirty = true;
            }

            if (markDirty)
            {
                currentEditor.MarkDirty();
            }

            GUILayout.Space(20f);

            currentEditor.previewer.DrawStatePreview(selectedLayer, selectedState);
        }