|
| 1 | +using System; |
| 2 | +using MCPForUnity.Editor.Helpers; |
| 3 | +using Newtonsoft.Json.Linq; |
| 4 | +using UnityEditor.SceneManagement; |
| 5 | + |
| 6 | +namespace MCPForUnity.Editor.Resources.Editor |
| 7 | +{ |
| 8 | + /// <summary> |
| 9 | + /// Returns information about the currently open Prefab Stage (Isolation or |
| 10 | + /// In-Context mode), or { isOpen = false } when no prefab is being edited. |
| 11 | + /// |
| 12 | + /// Wires up the existing <c>mcpforunity://editor/prefab-stage</c> resource |
| 13 | + /// (Server/src/services/resources/prefab_stage.py) which dispatches the |
| 14 | + /// <c>get_prefab_stage</c> command name expected by this handler. |
| 15 | + /// </summary> |
| 16 | + [McpForUnityResource("get_prefab_stage")] |
| 17 | + public static class GetPrefabStage |
| 18 | + { |
| 19 | + public static object HandleCommand(JObject @params) |
| 20 | + { |
| 21 | + try |
| 22 | + { |
| 23 | + var stage = PrefabStageUtility.GetCurrentPrefabStage(); |
| 24 | + if (stage == null) |
| 25 | + return new SuccessResponse("No prefab stage open.", new { isOpen = false }); |
| 26 | + |
| 27 | + var root = stage.prefabContentsRoot; |
| 28 | + return new SuccessResponse("Retrieved prefab stage info.", new |
| 29 | + { |
| 30 | + isOpen = true, |
| 31 | + assetPath = stage.assetPath, |
| 32 | + prefabRootName = root != null ? root.name : null, |
| 33 | + mode = stage.mode.ToString(), |
| 34 | + isDirty = stage.scene.isDirty, |
| 35 | + }); |
| 36 | + } |
| 37 | + catch (Exception e) |
| 38 | + { |
| 39 | + return new ErrorResponse($"Error getting prefab stage: {e.Message}"); |
| 40 | + } |
| 41 | + } |
| 42 | + } |
| 43 | +} |
0 commit comments