public override void OnInspectorGUI()
        {
            base.OnInspectorGUI();

            EditorGUILayout.BeginVertical(EditorStyles.inspectorFullWidthMargins);

            // Enable/Disable button
            if (controllerExtra == null)
            {
                if (GUILayout.Button("Enable Controller Merger"))
                {
                    if (EditorUtility.DisplayDialog("Enable Controller Merger",
                                                    "This will remove any layers already on this controller. Are you sure?", "Yes", "Cancel"))
                    {
                        // Enable
                        controllerExtra      = CreateInstance <AnimatorControllerMerger>();
                        controllerExtra.name = "Animator Controller Merger";

                        AssetDatabase.AddObjectToAsset(controllerExtra, controllerPath);
                        Save();
                        Save();
                    }
                }
            }
            else
            {
                if (GUILayout.Button("Disable Controller Merger"))
                {
                    // Disable
                    foreach (Object o in AssetDatabase.LoadAllAssetsAtPath(controllerPath))
                    {
                        if (o.GetType() == typeof(AnimatorControllerMerger))
                        {
                            AssetDatabase.RemoveObjectFromAsset(o);
                        }
                    }

                    controllerExtra = null;
                    controllerList  = null;

                    Save();
                }
            }

            if (controllerExtra != null)
            {
                // Rename base layers button
                bool renameBaseLayersPrev = controllerExtra.renameBaseLayers;
                controllerExtra.renameBaseLayers = EditorGUILayout.Toggle("Rename Base Layers", controllerExtra.renameBaseLayers);
                if (controllerExtra.renameBaseLayers != renameBaseLayersPrev)
                {
                    Save();
                }

                // Layer list
                DrawList();
            }

            EditorGUILayout.EndVertical();
        }
 private void OnEnable()
 {
     controller      = (AnimatorController)target;
     controllerPath  = AssetDatabase.GetAssetPath(controller);
     controllerExtra = AssetDatabase.LoadAssetAtPath <AnimatorControllerMerger>(controllerPath);
 }
Exemplo n.º 3
0
        public static string[] OnWillSaveAssets(string[] paths)
        {
            List <AnimatorController> modifiedControllers = new List <AnimatorController>();

            foreach (string path in paths)
            {
                if (AssetDatabase.GetMainAssetTypeAtPath(path) == typeof(AnimatorController))
                {
                    modifiedControllers.Add(AssetDatabase.LoadAssetAtPath <AnimatorController>(path));
                }
            }

            foreach (string guid in AssetDatabase.FindAssets("t:AnimatorControllerMerger"))
            {
                string controllerPath = AssetDatabase.GUIDToAssetPath(guid);
                AnimatorControllerMerger controllerMerger = AssetDatabase.LoadAssetAtPath <AnimatorControllerMerger>(controllerPath);

                if (controllerMerger == null)
                {
                    continue;
                }

                if (!controllerMerger.controllers.Intersect(modifiedControllers).Any() &&
                    !paths.Contains(controllerPath))
                {
                    continue;
                }

                AnimatorController controller = AssetDatabase.LoadAssetAtPath <AnimatorController>(controllerPath);

                while (controller.parameters.Length > 0)
                {
                    controller.RemoveParameter(0);
                }

                List <AnimatorControllerLayer> newLayers = new List <AnimatorControllerLayer>();
                foreach (AnimatorController c in controllerMerger.controllers)
                {
                    if (c == null)
                    {
                        continue;
                    }

                    bool isBaseLayer = true;
                    foreach (var l in c.layers)
                    {
                        newLayers.Add(new AnimatorControllerLayer()
                        {
                            avatarMask               = l.avatarMask,
                            blendingMode             = l.blendingMode,
                            defaultWeight            = isBaseLayer ? 1f : l.defaultWeight,
                            iKPass                   = l.iKPass,
                            name                     = controllerMerger.renameBaseLayers && isBaseLayer ? $"=== {c.name} ===" : l.name,
                            syncedLayerAffectsTiming = l.syncedLayerAffectsTiming,
                            stateMachine             = l.stateMachine
                        });
                        isBaseLayer = false;
                    }

                    foreach (var p in c.parameters)
                    {
                        if (controller.parameters.Count(pp => pp.name == p.name) == 0)
                        {
                            controller.AddParameter(new AnimatorControllerParameter()
                            {
                                defaultBool  = p.defaultBool,
                                defaultFloat = p.defaultFloat,
                                defaultInt   = p.defaultInt,
                                name         = p.name,
                                type         = p.type
                            });
                        }
                    }
                }
                controller.layers = newLayers.ToArray();
            }
            return(paths);
        }