Esempio n. 1
0
    void AnimationSectionGUI()
    {
        // Handle animations array

        bool changed = false;

        animsFoldout = EditorGUILayout.Foldout(animsFoldout, "Source Animations");
        if (animsFoldout)
        {
            EditorGUIUtility.LookLikeControls(160);

            if (rebuildGroups || groups == null)
            {
                rebuildGroups = false;

                bool noGroupsAreUsed = true;

                if (groups == null)
                {
                    groups = new List <InspectorAnimationGroup>();
                    groups.Add(new InspectorAnimationGroup("No Change"));
                    groups.Add(new InspectorAnimationGroup(""));
                    groups.Add(new InspectorAnimationGroup("Not Used"));
                }
                else
                {
                    for (int i = 0; i < groups.Count; i++)
                    {
                        groups[i].motions.Clear();
                    }
                }

                InspectorAnimationGroup unusedGroup = groups[groups.Count - 1];
                List <AnimationClip>    unusedClips = new List <AnimationClip>(AnimationUtility.GetAnimationClips(lc.GetComponent <Animation>()));

                if (lc.sourceAnimations == null)
                {
                    lc.sourceAnimations = new MotionAnalyzer[0];
                }
                for (int m = 0; m < lc.sourceAnimations.Length; m++)
                {
                    MotionAnalyzer ma = lc.sourceAnimations[m];
                    noGroupsAreUsed = false;

                    if (unusedClips.Contains(ma.animation))
                    {
                        unusedClips.Remove(ma.animation);
                    }

                    if (ma.motionGroup == null)
                    {
                        ma.motionGroup = "";
                    }

                    bool found = false;
                    for (int g = 0; g < groups.Count; g++)
                    {
                        if (ma.motionGroup == groups[g].name)
                        {
                            groups[g].motions.Add(ma);
                            found = true;
                        }
                    }
                    if (!found)
                    {
                        InspectorAnimationGroup group = new InspectorAnimationGroup(ma.motionGroup);
                        group.motions.Add(ma);
                        groups.Insert(groups.Count - 1, group);
                    }
                }

                // Populate group of unused motions
                if (unusedClips.Count != unusedGroup.motions.Count)
                {
                    selectedMotions.Clear();
                    for (int i = 0; i < unusedClips.Count; i++)
                    {
                        MotionAnalyzer ma = new MotionAnalyzer();
                        ma.animation = unusedClips[i];
                        unusedGroup.motions.Add(ma);
                    }
                }

                // Check for empty groups
                for (int i = 2; i < groups.Count - 1; i++)
                {
                    if (groups[i].motions.Count == 0 || groups[i].name == "")
                    {
                        groups.Remove(groups[i]);
                        i--;
                    }
                }

                // Only have unused group expanded by default if no other groups have any animations in them
                if (!noGroupsAreUsed)
                {
                    groups[groups.Count - 1].expanded = false;
                }
            }

            string[] groupNames = new string[groups.Count + 1];
            for (int g = 0; g < groups.Count; g++)
            {
                groupNames[g] = groups[g].name;
            }
            groupNames[1] = "Ungrouped";
            groupNames[groupNames.Length - 2] = "New Group";
            groupNames[groupNames.Length - 1] = "Not Used";

            for (int g = 1; g < groups.Count; g++)
            {
                InspectorAnimationGroup group = groups[g];
                bool used = true;

                if (g == 1 && group.motions.Count == 0)
                {
                    continue;
                }

                GUILayout.BeginHorizontal(GUILayout.ExpandWidth(true));
                group.expanded = GUILayout.Toggle(group.expanded, "", EditorStyles.foldout, GUILayout.ExpandWidth(false));
                if (g == 1)
                {
                    GUILayout.Label("Ungrouped Animations", GUILayout.ExpandWidth(true));
                }
                else if (group.name == "Not Used")
                {
                    GUILayout.Label("Not Used (by Locomotion System)", GUILayout.ExpandWidth(true));
                    used = false;
                }
                else
                {
                    GUILayout.Label("Motion Group:", GUILayout.ExpandWidth(false));
                    string newName = GUILayout.TextField(group.name, GUILayout.ExpandWidth(true));
                    if (newName != group.name)
                    {
                        group.name = newName;
                        changed    = true;
                    }
                }
                if (GUILayout.Button("", "toggle", GUILayout.ExpandWidth(false)))
                {
                    bool allWasSelected = true;
                    for (int m = 0; m < group.motions.Count; m++)
                    {
                        if (!selectedMotions.Contains(group.motions[m]))
                        {
                            selectedMotions.Add(group.motions[m]);
                            allWasSelected = false;
                        }
                    }
                    if (allWasSelected)
                    {
                        for (int m = 0; m < group.motions.Count; m++)
                        {
                            if (selectedMotions.Contains(group.motions[m]))
                            {
                                selectedMotions.Remove(group.motions[m]);
                            }
                        }
                    }
                }
                GUILayout.EndHorizontal();

                if (group.expanded)
                {
                    EditorGUI.indentLevel++;

                    for (int m = 0; m < group.motions.Count; m++)
                    {
                        MotionAnalyzer ma = group.motions[m];

                        GUILayout.BeginHorizontal(GUILayout.ExpandWidth(true));

                        GUILayout.Space(15);

                        // Foldout
                        bool expanded = false;
                        if (used)
                        {
                            bool expandedOld = expandedMotions.Contains(ma);
                            expanded = GUILayout.Toggle(expandedOld, "", EditorStyles.foldout, GUILayout.ExpandWidth(false));
                            if (expanded != expandedOld)
                            {
                                if (expanded)
                                {
                                    expandedMotions.Add(ma);
                                }
                                else
                                {
                                    expandedMotions.Remove(ma);
                                }
                            }

                            GUI.changed   = false;
                            ma.animation  = EditorGUILayout.ObjectField(ma.animation, typeof(AnimationClip), GUILayout.ExpandWidth(true)) as AnimationClip;
                            ma.motionType = (MotionType)EditorGUILayout.EnumPopup(ma.motionType, GUILayout.Width(70));
                            if (GUI.changed)
                            {
                                changed = true;
                            }
                        }
                        else
                        {
                            GUI.enabled = false;
                            GUILayout.Toggle(false, "", EditorStyles.foldout, GUILayout.ExpandWidth(false));
                            EditorGUILayout.ObjectField(ma.animation, typeof(AnimationClip), GUILayout.ExpandWidth(true));
                            GUI.enabled = true;
                            GUILayout.Space(70 + 4);
                        }

                        // Selection
                        bool selectedOld = selectedMotions.Contains(ma);
                        bool selected    = GUILayout.Toggle(selectedOld, "", GUILayout.ExpandWidth(false));
                        if (selected != selectedOld)
                        {
                            if (selected)
                            {
                                selectedMotions.Add(ma);
                            }
                            else
                            {
                                selectedMotions.Remove(ma);
                            }
                        }

                        GUILayout.EndHorizontal();

                        if (expanded)
                        {
                            GUI.changed            = false;
                            EditorGUI.indentLevel += 2;
                            ma.alsoUseBackwards    = EditorGUILayout.Toggle("Also Use Backwards", ma.alsoUseBackwards);
                            ma.fixFootSkating      = EditorGUILayout.Toggle("Fix Foot Skating", ma.fixFootSkating);
                            EditorGUILayout.LabelField("Native Speed", "" + ma.nativeSpeed);
                            EditorGUI.indentLevel -= 2;
                            if (GUI.changed)
                            {
                                changed = true;
                            }
                        }
                    }
                    EditorGUI.indentLevel--;

                    EditorGUILayout.Space();
                }
            }

            EditorGUIUtility.LookLikeControls(120);

            // Apply changes to selections
            bool clearInspectorGroups = false;
            GUI.enabled = (selectedMotions.Count > 0);
            int selectedGroup = EditorGUILayout.Popup("Move Selected To", 0, groupNames);
            if (selectedGroup != 0)
            {
                for (int i = 0; i < selectedMotions.Count; i++)
                {
                    MotionAnalyzer ma = selectedMotions[i];
                    for (int j = 0; j < groups.Count; j++)
                    {
                        if (groups[j].motions.Contains(ma))
                        {
                            groups[j].motions.Remove(ma);
                        }
                    }

                    // Add to unused
                    if (selectedGroup == groupNames.Length - 1)
                    {
                        groups[selectedGroup - 1].motions.Add(ma);
                    }
                    // Add to new group
                    else if (selectedGroup == groupNames.Length - 2)
                    {
                        string newName = "MotionGroup";
                        bool   exists  = true;
                        int    c       = 1;
                        while (exists)
                        {
                            newName = "MotionGroup" + c;
                            exists  = false;
                            for (int j = 0; j < groups.Count; j++)
                            {
                                if (groups[j].name == newName)
                                {
                                    exists = true;
                                }
                            }
                            c++;
                        }
                        groups.Insert(groups.Count - 1, new InspectorAnimationGroup(newName));
                        groups[selectedGroup].motions.Add(ma);
                    }
                    // Add to selected group
                    else
                    {
                        groups[selectedGroup].motions.Add(ma);
                    }
                }
                selectedMotions.Clear();
                clearInspectorGroups = true;
                lc.initialized       = false;
                changed = true;
            }
            GUI.enabled = true;

            if (changed)
            {
                List <MotionAnalyzer> motions = new List <MotionAnalyzer>();
                for (int g = 0; g < groups.Count - 1; g++)
                {
                    for (int m = 0; m < groups[g].motions.Count; m++)
                    {
                        groups[g].motions[m].motionGroup = groups[g].name;
                        motions.Add(groups[g].motions[m]);
                    }
                }
                lc.sourceAnimations = motions.ToArray();
                lc.initialized      = false;
            }

            if (clearInspectorGroups)
            {
                rebuildGroups = true;
            }
        }

        EditorGUILayout.Space();
    }