/*
┌──────────────────────────────────────────────────────────────────┐
│ Author: Ivan Murzak (https://github.com/IvanMurzak) │
│ Repository: GitHub (https://github.com/IvanMurzak/Unity-MCP) │
│ Copyright (c) 2025 Ivan Murzak │
│ Licensed under the Apache License, Version 2.0. │
│ See the LICENSE file in the project root for more information. │
└──────────────────────────────────────────────────────────────────┘
*/
#nullable enable
#if UNITY_EDITOR && UNITY_6000_5_OR_NEWER
using com.IvanMurzak.McpPlugin.Common;
using UnityEngine.SceneManagement;
namespace com.IvanMurzak.Unity.MCP.Runtime.Utils
{
public static partial class GameObjectUtils
{
///
/// Find Root GameObject in opened Prefab. Of array of GameObjects in a scene.
///
/// Scene for the search, if null the current active scene would be used
/// Array of root GameObjects
public static UnityEngine.GameObject[] FindRootGameObjects(Scene? scene = null)
{
var prefabStage = UnityEditor.SceneManagement.PrefabStageUtility.GetCurrentPrefabStage();
if (prefabStage != null)
return prefabStage.prefabContentsRoot.MakeArray();
if (scene == null)
{
var rootGos = UnityEditor.SceneManagement.EditorSceneManager
.GetActiveScene()
.GetRootGameObjects();
return rootGos;
}
else
{
return scene.Value.GetRootGameObjects();
}
}
public static UnityEngine.GameObject? FindByInstanceID(UnityEngine.EntityId instanceID)
{
if (instanceID == UnityEngine.EntityId.None)
return null;
var obj = UnityEditor.EditorUtility.EntityIdToObject(instanceID);
if (obj is not UnityEngine.GameObject go)
return null;
return go;
}
}
}
#endif