Skip to content

Commit 55f7c55

Browse files
committed
Update for ManageShader
Bug fix and error handling check for the PR. Great work and I love what I built!
1 parent d4bd504 commit 55f7c55

File tree

5 files changed

+77
-44
lines changed

5 files changed

+77
-44
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Unity MCP acts as a bridge, allowing AI assistants (like Claude, Cursor) to inte
2525
* `manage_editor`: Controls and queries the editor's state and settings.
2626
* `manage_scene`: Manages scenes (load, save, create, get hierarchy, etc.).
2727
* `manage_asset`: Performs asset operations (import, create, modify, delete, etc.).
28+
* `manage_shader`: Performs shader CRUD operations (create, read, modify, delete).
2829
* `manage_gameobject`: Manages GameObjects: create, modify, delete, find, and component operations.
2930
* `execute_menu_item`: Executes a menu item via its path (e.g., "File/Save Project").
3031
</details>
@@ -81,7 +82,9 @@ Unity MCP connects your tools using two components:
8182

8283
Connect your MCP Client (Claude, Cursor, etc.) to the Python server you installed in Step 1.
8384

84-
**Option A: Auto-Configure (Recommended for Claude/Cursor)**
85+
<img width="609" alt="image" src="https://github.com/user-attachments/assets/cef3a639-4677-4fd8-84e7-2d82a04d55bb" />
86+
87+
**Option A: Auto-Configure (Recommended for Claude/Cursor/VSC Copilot)**
8588

8689
1. In Unity, go to `Window > Unity MCP`.
8790
2. Click `Auto Configure Claude` or `Auto Configure Cursor`.

UnityMcpBridge/Editor/Tools/CommandRegistry.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public static class CommandRegistry
2020
{ "HandleManageAsset", ManageAsset.HandleCommand },
2121
{ "HandleReadConsole", ReadConsole.HandleCommand },
2222
{ "HandleExecuteMenuItem", ExecuteMenuItem.HandleCommand },
23+
{ "HandleManageShader", ManageShader.HandleCommand},
2324
};
2425

2526
/// <summary>

UnityMcpBridge/Editor/Tools/ManageShader.cs

Lines changed: 60 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,12 @@ public static object HandleCommand(JObject @params)
8989
{
9090
try
9191
{
92-
Directory.CreateDirectory(fullPathDir);
92+
if (!Directory.Exists(fullPathDir))
93+
{
94+
Directory.CreateDirectory(fullPathDir);
95+
// Refresh AssetDatabase to recognize new folders
96+
AssetDatabase.Refresh();
97+
}
9398
}
9499
catch (Exception e)
95100
{
@@ -150,6 +155,14 @@ string contents
150155
);
151156
}
152157

158+
// Add validation for shader name conflicts in Unity
159+
if (Shader.Find(name) != null)
160+
{
161+
return Response.Error(
162+
$"A shader with name '{name}' already exists in the project. Choose a different name."
163+
);
164+
}
165+
153166
// Generate default content if none provided
154167
if (string.IsNullOrEmpty(contents))
155168
{
@@ -184,6 +197,7 @@ private static object ReadShader(string fullPath, string relativePath)
184197
string contents = File.ReadAllText(fullPath);
185198

186199
// Return both normal and encoded contents for larger files
200+
//TODO: Consider a threshold for large files
187201
bool isLarge = contents.Length > 10000; // If content is large, include encoded version
188202
var responseData = new
189203
{
@@ -227,6 +241,7 @@ string contents
227241
{
228242
File.WriteAllText(fullPath, contents);
229243
AssetDatabase.ImportAsset(relativePath);
244+
AssetDatabase.Refresh();
230245
return Response.Success(
231246
$"Shader '{Path.GetFileName(relativePath)}' updated successfully.",
232247
new { path = relativePath }
@@ -268,58 +283,60 @@ private static object DeleteShader(string fullPath, string relativePath)
268283
}
269284
}
270285

286+
//This is a CGProgram template
287+
//TODO: making a HLSL template as well?
271288
private static string GenerateDefaultShaderContent(string name)
272289
{
273290
return @"Shader """ + name + @"""
274-
{
275-
Properties
276-
{
277-
_MainTex (""Texture"", 2D) = ""white"" {}
278-
}
279-
SubShader
280-
{
281-
Tags { ""RenderType""=""Opaque"" }
282-
LOD 100
283-
284-
Pass
285291
{
286-
CGPROGRAM
287-
#pragma vertex vert
288-
#pragma fragment frag
289-
#include ""UnityCG.cginc""
290-
291-
struct appdata
292+
Properties
292293
{
293-
float4 vertex : POSITION;
294-
float2 uv : TEXCOORD0;
295-
};
296-
297-
struct v2f
294+
_MainTex (""Texture"", 2D) = ""white"" {}
295+
}
296+
SubShader
298297
{
299-
float2 uv : TEXCOORD0;
300-
float4 vertex : SV_POSITION;
301-
};
298+
Tags { ""RenderType""=""Opaque"" }
299+
LOD 100
302300
303-
sampler2D _MainTex;
304-
float4 _MainTex_ST;
301+
Pass
302+
{
303+
CGPROGRAM
304+
#pragma vertex vert
305+
#pragma fragment frag
306+
#include ""UnityCG.cginc""
305307
306-
v2f vert (appdata v)
307-
{
308-
v2f o;
309-
o.vertex = UnityObjectToClipPos(v.vertex);
310-
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
311-
return o;
312-
}
308+
struct appdata
309+
{
310+
float4 vertex : POSITION;
311+
float2 uv : TEXCOORD0;
312+
};
313313
314-
fixed4 frag (v2f i) : SV_Target
315-
{
316-
fixed4 col = tex2D(_MainTex, i.uv);
317-
return col;
314+
struct v2f
315+
{
316+
float2 uv : TEXCOORD0;
317+
float4 vertex : SV_POSITION;
318+
};
319+
320+
sampler2D _MainTex;
321+
float4 _MainTex_ST;
322+
323+
v2f vert (appdata v)
324+
{
325+
v2f o;
326+
o.vertex = UnityObjectToClipPos(v.vertex);
327+
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
328+
return o;
329+
}
330+
331+
fixed4 frag (v2f i) : SV_Target
332+
{
333+
fixed4 col = tex2D(_MainTex, i.uv);
334+
return col;
335+
}
336+
ENDCG
337+
}
318338
}
319-
ENDCG
320-
}
321-
}
322-
}";
339+
}";
323340
}
324341
}
325342
}

UnityMcpBridge/Editor/Tools/ManageShader.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.

UnityMcpBridge/Editor/UnityMcpBridge.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,7 @@ private static string ExecuteCommand(Command command)
378378
"manage_editor" => ManageEditor.HandleCommand(paramsObject),
379379
"manage_gameobject" => ManageGameObject.HandleCommand(paramsObject),
380380
"manage_asset" => ManageAsset.HandleCommand(paramsObject),
381+
"manage_shader" => ManageShader.HandleCommand(paramsObject),
381382
"read_console" => ReadConsole.HandleCommand(paramsObject),
382383
"execute_menu_item" => ExecuteMenuItem.HandleCommand(paramsObject),
383384
_ => throw new ArgumentException(

0 commit comments

Comments
 (0)