diff --git a/Editor/ScenePartitionMenuEditor.cs b/Editor/ScenePartitionMenuEditor.cs index 1973b83..d168345 100644 --- a/Editor/ScenePartitionMenuEditor.cs +++ b/Editor/ScenePartitionMenuEditor.cs @@ -5,25 +5,50 @@ namespace VertexColor.ScenePartition.Editor { public static class ScenePartitionMenuEditor { - [MenuItem("GameObject/CopyFileId", false, 10000)] - public static void CreateTextArea5() + [MenuItem("GameObject/ScenePartition/CopyObjectId", false, 10000)] + public static void CopyObjectId(MenuCommand menuCommand) { - GameObject go = Selection.activeGameObject; - + // Get context from menu command instead of selection because the menu command is executed for each selected object when executed from the hierarchy. + if (menuCommand.context is not GameObject go) return; if (go == null) return; + if (go.scene == null) return; - GlobalObjectId id = GlobalObjectId.GetGlobalObjectIdSlow(go); + ulong id = ScenePartitionUtils.GetTargetObjectId(go); - if (id.targetPrefabId == 0) // 0 = no prefab. - { - EditorGUIUtility.systemCopyBuffer = id.targetObjectId.ToString(); - Debug.Log($"object id: {id.targetObjectId}"); - } - else - { - EditorGUIUtility.systemCopyBuffer = id.targetPrefabId.ToString(); - Debug.Log($"object id: {id.targetPrefabId}"); - } + EditorGUIUtility.systemCopyBuffer = id.ToString(); + Debug.Log($"Copied object id '{id}' from '{go}' to clipboard"); + } + + [MenuItem("GameObject/ScenePartition/AddToAlwaysLoad", false, 10000)] + public static void AddToAlwaysLoad(MenuCommand menuCommand) + { + // Get context from menu command instead of selection because the menu command is executed for each selected object when executed from the hierarchy. + if (menuCommand.context is not GameObject go) return; + if (go == null) return; + if (go.scene == null) return; + if (!ScenePartitionUtils.TryGetScenePartitionSOForActiveScene(out var scenePartitionSO)) return; + + ulong id = ScenePartitionUtils.GetTargetObjectId(go); + + if (scenePartitionSO.alwaysLoadIds.Contains(id)) return; + + scenePartitionSO.alwaysLoadIds.Add(id); + Debug.Log($"Added '{go}' ({id}) to '{scenePartitionSO.name}' ({scenePartitionSO.SceneName})"); + } + + [MenuItem("GameObject/ScenePartition/RemoveFromAlwaysLoad", false, 10000)] + public static void RemoveFromAlwaysLoad(MenuCommand menuCommand) + { + // Get context from menu command instead of selection because the menu command is executed for each selected object when executed from the hierarchy. + if (menuCommand.context is not GameObject go) return; + if (go == null) return; + if (go.scene == null) return; + if (!ScenePartitionUtils.TryGetScenePartitionSOForActiveScene(out var scenePartitionSO)) return; + + ulong id = ScenePartitionUtils.GetTargetObjectId(go); + + scenePartitionSO.alwaysLoadIds.Remove(id); + Debug.Log($"Removed '{go}' ({id}) from '{scenePartitionSO.name}' ({scenePartitionSO.SceneName})"); } } } \ No newline at end of file diff --git a/Editor/ScenePartitionUtils.cs b/Editor/ScenePartitionUtils.cs index 29d32ec..afa61eb 100644 --- a/Editor/ScenePartitionUtils.cs +++ b/Editor/ScenePartitionUtils.cs @@ -93,5 +93,19 @@ public static bool TryGetScenePartitionSOForActiveScene(out ScenePartitionSO sce return false; } } + + public static ulong GetTargetObjectId(Object targetObject) + { + GlobalObjectId globalId = GlobalObjectId.GetGlobalObjectIdSlow(targetObject); + + if (globalId.targetPrefabId == 0) // 0 = no prefab. + { + return globalId.targetObjectId; + } + else + { + return globalId.targetPrefabId; + } + } } } \ No newline at end of file