Skip to main content
Glama
GameObjectUtils.Editor.cs2.31 kB
/* ┌──────────────────────────────────────────────────────────────────┐ │ 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 using com.IvanMurzak.McpPlugin.Common; using UnityEngine; using UnityEngine.SceneManagement; namespace com.IvanMurzak.Unity.MCP.Runtime.Utils { public static partial class GameObjectUtils { /// <summary> /// Find Root GameObject in opened Prefab. Of array of GameObjects in a scene. /// </summary> /// <param name="scene">Scene for the search, if null the current active scene would be used</param> /// <returns>Array of root GameObjects</returns> public static 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 GameObject? FindByInstanceID(int instanceID) { if (instanceID == 0) return null; var obj = UnityEditor.EditorUtility.InstanceIDToObject(instanceID); if (obj is not GameObject go) return null; return go; } } } #endif

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/IvanMurzak/Unity-MCP'

If you have feedback or need assistance with the MCP directory API, please join our Discord server