Skip to content

Commit dc095d5

Browse files
committed
Fix: Address multiple nitpicks (test robustness, shader resolution, HasProperty checks)
1 parent eef4261 commit dc095d5

File tree

4 files changed

+16
-6
lines changed

4 files changed

+16
-6
lines changed

MCPForUnity/Editor/Helpers/MaterialOps.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,9 @@ public static bool TrySetShaderProperty(Material material, string propertyName,
270270
}
271271
else if (value.Type == JTokenType.Float || value.Type == JTokenType.Integer)
272272
{
273+
if (!material.HasProperty(propertyName))
274+
return false;
275+
273276
try { material.SetFloat(propertyName, value.ToObject<float>(serializer)); return true; }
274277
catch (Exception ex)
275278
{
@@ -278,6 +281,9 @@ public static bool TrySetShaderProperty(Material material, string propertyName,
278281
}
279282
else if (value.Type == JTokenType.Boolean)
280283
{
284+
if (!material.HasProperty(propertyName))
285+
return false;
286+
281287
try { material.SetFloat(propertyName, value.ToObject<bool>(serializer) ? 1f : 0f); return true; }
282288
catch (Exception ex)
283289
{

MCPForUnity/Editor/Tools/ManageMaterial.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@ private static object SetMaterialShaderProperty(JObject @params)
9191
}
9292

9393
Undo.RecordObject(mat, "Set Material Property");
94+
95+
// Normalize alias/casing once for all code paths
96+
property = MaterialOps.ResolvePropertyName(mat, property);
9497

9598
// 1. Try handling Texture instruction explicitly (ManageMaterial special feature)
9699
if (value.Type == JTokenType.Object)
@@ -99,7 +102,7 @@ private static object SetMaterialShaderProperty(JObject @params)
99102
if (value is JObject obj && (obj.ContainsKey("find") || obj.ContainsKey("method")))
100103
{
101104
Texture tex = ManageGameObject.FindObjectByInstruction(obj, typeof(Texture)) as Texture;
102-
if (tex != null)
105+
if (tex != null && mat.HasProperty(property))
103106
{
104107
mat.SetTexture(property, tex);
105108
EditorUtility.SetDirty(mat);
@@ -109,7 +112,6 @@ private static object SetMaterialShaderProperty(JObject @params)
109112
}
110113

111114
// 2. Fallback to standard logic via MaterialOps (handles Colors, Floats, Strings->Path)
112-
property = MaterialOps.ResolvePropertyName(mat, property);
113115
bool success = MaterialOps.TrySetShaderProperty(mat, property, value, ManageGameObject.InputSerializer);
114116

115117
if (success)
@@ -438,7 +440,7 @@ private static object CreateMaterial(JObject @params)
438440
return new { status = "error", message = "Path must start with Assets/ (normalization failed)" };
439441
}
440442

441-
Shader shader = Shader.Find(shaderName);
443+
Shader shader = RenderPipelineUtility.ResolveShader(shaderName);
442444
if (shader == null)
443445
{
444446
return new { status = "error", message = $"Could not find shader: {shaderName}" };

Server/tests/integration/test_manage_asset_json_parsing.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ async def test_component_properties_json_string_parsing(self, monkeypatch):
125125

126126
ctx = DummyContext()
127127

128-
async def fake_send(cmd, params, **kwargs):
128+
async def fake_send(_cmd, params, **_kwargs):
129129
return {"success": True, "message": "GameObject created successfully"}
130130
monkeypatch.setattr(
131131
"services.tools.manage_gameobject.async_send_command_with_retry",
@@ -151,7 +151,7 @@ async def test_component_properties_parsing_verification(self, monkeypatch):
151151
ctx = DummyContext()
152152

153153
captured_params = {}
154-
async def fake_send(cmd, params, **kwargs):
154+
async def fake_send(_cmd, params, **_kwargs):
155155
captured_params.update(params)
156156
return {"success": True, "message": "GameObject created successfully"}
157157

TestProjects/UnityMCPTests/Assets/Tests/EditMode/Tools/ReadConsoleTests.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public void HandleCommand_Clear_Works()
2020

2121
// Verify content exists before clear
2222
var getBefore = ToJObject(ReadConsole.HandleCommand(new JObject { ["action"] = "get", ["count"] = 10 }));
23+
Assert.IsTrue(getBefore.Value<bool>("success"), getBefore.ToString());
2324
var entriesBefore = getBefore["data"] as JArray;
2425

2526
// Ideally we'd assert count > 0, but other tests/system logs might affect this.
@@ -35,6 +36,7 @@ public void HandleCommand_Clear_Works()
3536

3637
// Verify clear effect
3738
var getAfter = ToJObject(ReadConsole.HandleCommand(new JObject { ["action"] = "get", ["count"] = 10 }));
39+
Assert.IsTrue(getAfter.Value<bool>("success"), getAfter.ToString());
3840
var entriesAfter = getAfter["data"] as JArray;
3941
Assert.IsTrue(entriesAfter == null || entriesAfter.Count == 0, "Console should be empty after clear.");
4042
}
@@ -49,7 +51,7 @@ public void HandleCommand_Get_Works()
4951
var paramsObj = new JObject
5052
{
5153
["action"] = "get",
52-
["count"] = 10 // Fetch enough to likely catch our message
54+
["count"] = 1000 // Fetch enough to likely catch our message
5355
};
5456

5557
// Act

0 commit comments

Comments
 (0)