Skip to content

Commit 932703d

Browse files
committed
feat: add Unity editor state and project info resources
- Implemented resources for querying active tool, editor state, prefab stage, selection, and open windows - Added project configuration resources for layers and project metadata - Organized new resources into Editor and Project namespaces for better structure
1 parent c3186cd commit 932703d

37 files changed

+741
-299
lines changed

TestProjects/UnityMCPTests/Assets/Materials.meta renamed to MCPForUnity/Editor/Resources/Editor.meta

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
using System;
2+
using MCPForUnity.Editor.Helpers;
3+
using Newtonsoft.Json.Linq;
4+
using UnityEditor;
5+
6+
namespace MCPForUnity.Editor.Resources.Editor
7+
{
8+
/// <summary>
9+
/// Provides information about the currently active editor tool.
10+
/// </summary>
11+
[McpForUnityResource("get_active_tool")]
12+
public static class ActiveTool
13+
{
14+
public static object HandleCommand(JObject @params)
15+
{
16+
try
17+
{
18+
Tool currentTool = UnityEditor.Tools.current;
19+
string toolName = currentTool.ToString();
20+
bool customToolActive = UnityEditor.Tools.current == Tool.Custom;
21+
string activeToolName = customToolActive ? EditorTools.GetActiveToolName() : toolName;
22+
23+
var toolInfo = new
24+
{
25+
activeTool = activeToolName,
26+
isCustom = customToolActive,
27+
pivotMode = UnityEditor.Tools.pivotMode.ToString(),
28+
pivotRotation = UnityEditor.Tools.pivotRotation.ToString(),
29+
handleRotation = new
30+
{
31+
x = UnityEditor.Tools.handleRotation.eulerAngles.x,
32+
y = UnityEditor.Tools.handleRotation.eulerAngles.y,
33+
z = UnityEditor.Tools.handleRotation.eulerAngles.z
34+
},
35+
handlePosition = new
36+
{
37+
x = UnityEditor.Tools.handlePosition.x,
38+
y = UnityEditor.Tools.handlePosition.y,
39+
z = UnityEditor.Tools.handlePosition.z
40+
}
41+
};
42+
43+
return Response.Success("Retrieved active tool information.", toolInfo);
44+
}
45+
catch (Exception e)
46+
{
47+
return Response.Error($"Error getting active tool: {e.Message}");
48+
}
49+
}
50+
}
51+
52+
// Helper class for custom tool names
53+
internal static class EditorTools
54+
{
55+
public static string GetActiveToolName()
56+
{
57+
if (UnityEditor.Tools.current == Tool.Custom)
58+
{
59+
return "Unknown Custom Tool";
60+
}
61+
return UnityEditor.Tools.current.ToString();
62+
}
63+
}
64+
}

MCPForUnity/Editor/Resources/Editor/ActiveTool.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using System;
2+
using MCPForUnity.Editor.Helpers;
3+
using Newtonsoft.Json.Linq;
4+
using UnityEditor;
5+
using UnityEditor.SceneManagement;
6+
7+
namespace MCPForUnity.Editor.Resources.Editor
8+
{
9+
/// <summary>
10+
/// Provides dynamic editor state information that changes frequently.
11+
/// </summary>
12+
[McpForUnityResource("get_editor_state")]
13+
public static class EditorState
14+
{
15+
public static object HandleCommand(JObject @params)
16+
{
17+
try
18+
{
19+
var activeScene = EditorSceneManager.GetActiveScene();
20+
var state = new
21+
{
22+
isPlaying = EditorApplication.isPlaying,
23+
isPaused = EditorApplication.isPaused,
24+
isCompiling = EditorApplication.isCompiling,
25+
isUpdating = EditorApplication.isUpdating,
26+
timeSinceStartup = EditorApplication.timeSinceStartup,
27+
activeSceneName = activeScene.name ?? "",
28+
selectionCount = UnityEditor.Selection.count,
29+
activeObjectName = UnityEditor.Selection.activeObject?.name
30+
};
31+
32+
return Response.Success("Retrieved editor state.", state);
33+
}
34+
catch (Exception e)
35+
{
36+
return Response.Error($"Error getting editor state: {e.Message}");
37+
}
38+
}
39+
}
40+
}

MCPForUnity/Editor/Resources/Editor/EditorState.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
/// Provides information about the current prefab editing context.
10+
/// </summary>
11+
[McpForUnityResource("get_prefab_stage")]
12+
public static class PrefabStage
13+
{
14+
public static object HandleCommand(JObject @params)
15+
{
16+
try
17+
{
18+
PrefabStageUtility.GetCurrentPrefabStage();
19+
var stage = PrefabStageUtility.GetCurrentPrefabStage();
20+
21+
if (stage == null)
22+
{
23+
return Response.Success("No prefab stage is currently open.", new { isOpen = false });
24+
}
25+
26+
var stageInfo = new
27+
{
28+
isOpen = true,
29+
assetPath = stage.assetPath,
30+
prefabRootName = stage.prefabContentsRoot?.name,
31+
mode = stage.mode.ToString(),
32+
isDirty = stage.scene.isDirty
33+
};
34+
35+
return Response.Success("Prefab stage info retrieved.", stageInfo);
36+
}
37+
catch (Exception e)
38+
{
39+
return Response.Error($"Error getting prefab stage info: {e.Message}");
40+
}
41+
}
42+
}
43+
}

MCPForUnity/Editor/Resources/Editor/PrefabStage.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using System;
2+
using System.Linq;
3+
using MCPForUnity.Editor.Helpers;
4+
using Newtonsoft.Json.Linq;
5+
using UnityEditor;
6+
7+
namespace MCPForUnity.Editor.Resources.Editor
8+
{
9+
/// <summary>
10+
/// Provides detailed information about the current editor selection.
11+
/// </summary>
12+
[McpForUnityResource("get_selection")]
13+
public static class Selection
14+
{
15+
public static object HandleCommand(JObject @params)
16+
{
17+
try
18+
{
19+
var selectionInfo = new
20+
{
21+
activeObject = UnityEditor.Selection.activeObject?.name,
22+
activeGameObject = UnityEditor.Selection.activeGameObject?.name,
23+
activeTransform = UnityEditor.Selection.activeTransform?.name,
24+
activeInstanceID = UnityEditor.Selection.activeInstanceID,
25+
count = UnityEditor.Selection.count,
26+
objects = UnityEditor.Selection.objects
27+
.Select(obj => new
28+
{
29+
name = obj?.name,
30+
type = obj?.GetType().FullName,
31+
instanceID = obj?.GetInstanceID()
32+
})
33+
.ToList(),
34+
gameObjects = UnityEditor.Selection.gameObjects
35+
.Select(go => new
36+
{
37+
name = go?.name,
38+
instanceID = go?.GetInstanceID()
39+
})
40+
.ToList(),
41+
assetGUIDs = UnityEditor.Selection.assetGUIDs
42+
};
43+
44+
return Response.Success("Retrieved current selection details.", selectionInfo);
45+
}
46+
catch (Exception e)
47+
{
48+
return Response.Error($"Error getting selection: {e.Message}");
49+
}
50+
}
51+
}
52+
}

MCPForUnity/Editor/Resources/Editor/Selection.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using MCPForUnity.Editor.Helpers;
4+
using Newtonsoft.Json.Linq;
5+
using UnityEditor;
6+
using UnityEngine;
7+
8+
namespace MCPForUnity.Editor.Resources.Editor
9+
{
10+
/// <summary>
11+
/// Provides list of all open editor windows.
12+
/// </summary>
13+
[McpForUnityResource("get_windows")]
14+
public static class Windows
15+
{
16+
public static object HandleCommand(JObject @params)
17+
{
18+
try
19+
{
20+
EditorWindow[] allWindows = UnityEngine.Resources.FindObjectsOfTypeAll<EditorWindow>();
21+
var openWindows = new List<object>();
22+
23+
foreach (EditorWindow window in allWindows)
24+
{
25+
if (window == null)
26+
continue;
27+
28+
try
29+
{
30+
openWindows.Add(new
31+
{
32+
title = window.titleContent.text,
33+
typeName = window.GetType().FullName,
34+
isFocused = EditorWindow.focusedWindow == window,
35+
position = new
36+
{
37+
x = window.position.x,
38+
y = window.position.y,
39+
width = window.position.width,
40+
height = window.position.height
41+
},
42+
instanceID = window.GetInstanceID()
43+
});
44+
}
45+
catch (Exception ex)
46+
{
47+
Debug.LogWarning($"Could not get info for window {window.GetType().Name}: {ex.Message}");
48+
}
49+
}
50+
51+
return Response.Success("Retrieved list of open editor windows.", openWindows);
52+
}
53+
catch (Exception e)
54+
{
55+
return Response.Error($"Error getting editor windows: {e.Message}");
56+
}
57+
}
58+
}
59+
}

0 commit comments

Comments
 (0)