Files
test/Assets/Editor/PrefabApplyOverrideShortcut.cs
2026-04-21 14:43:26 +07:00

72 lines
2.0 KiB
C#

using UnityEditor;
using UnityEditor.ShortcutManagement;
using UnityEngine;
public static class PrefabApplyOverrideShortcut
{
private const string MenuPath = "Tools/Prefab/Apply Overrides %#a";
[MenuItem(MenuPath)]
private static void ApplyOverridesFromSelection()
{
var selectedObjects = Selection.gameObjects;
if (selectedObjects == null || selectedObjects.Length == 0)
{
Debug.LogWarning("No GameObject selected.");
return;
}
int appliedCount = 0;
foreach (var selectedObject in selectedObjects)
{
if (selectedObject == null || !PrefabUtility.IsPartOfPrefabInstance(selectedObject))
{
continue;
}
var rootInstance = PrefabUtility.GetOutermostPrefabInstanceRoot(selectedObject);
if (rootInstance == null)
{
continue;
}
PrefabUtility.ApplyPrefabInstance(rootInstance, InteractionMode.UserAction);
appliedCount++;
}
if (appliedCount == 0)
{
Debug.LogWarning("Selected object(s) are not prefab instances.");
return;
}
Debug.Log($"Applied prefab overrides for {appliedCount} instance(s).");
}
[MenuItem(MenuPath, true)]
private static bool ValidateApplyOverridesFromSelection()
{
var selectedObjects = Selection.gameObjects;
if (selectedObjects == null || selectedObjects.Length == 0)
{
return false;
}
foreach (var selectedObject in selectedObjects)
{
if (selectedObject != null && PrefabUtility.IsPartOfPrefabInstance(selectedObject))
{
return true;
}
}
return false;
}
[Shortcut("Prefab/Apply Overrides", KeyCode.A, ShortcutModifiers.Action | ShortcutModifiers.Shift)]
private static void ApplyOverridesShortcut(ShortcutArguments _)
{
ApplyOverridesFromSelection();
}
}