MenuItems for GameObjects

- CopyObjectId
- AddToAlwaysLoad
- RemoveFromAlwaysLoad
- Added utility to get the TargetObjectId

closes #10
This commit is contained in:
max 2023-07-27 18:23:03 +02:00
parent 6edb307e9c
commit ebb8ba559e
2 changed files with 54 additions and 15 deletions

View File

@ -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.ToString();
Debug.Log($"Copied object id '{id}' from '{go}' to clipboard");
}
[MenuItem("GameObject/ScenePartition/AddToAlwaysLoad", false, 10000)]
public static void AddToAlwaysLoad(MenuCommand menuCommand)
{
EditorGUIUtility.systemCopyBuffer = id.targetObjectId.ToString();
Debug.Log($"object id: {id.targetObjectId}");
// 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})");
}
else
[MenuItem("GameObject/ScenePartition/RemoveFromAlwaysLoad", false, 10000)]
public static void RemoveFromAlwaysLoad(MenuCommand menuCommand)
{
EditorGUIUtility.systemCopyBuffer = id.targetPrefabId.ToString();
Debug.Log($"object id: {id.targetPrefabId}");
}
// 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})");
}
}
}

View File

@ -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;
}
}
}
}