Skip to content

Commit fe685c2

Browse files
committed
Fix ManageMaterial shader reflection for Unity 6 and improve texture logging
1 parent ceef08b commit fe685c2

2 files changed

Lines changed: 58 additions & 4 deletions

File tree

MCPForUnity/Editor/Helpers/MaterialOps.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,10 @@ public static bool TrySetShaderProperty(Material material, string propertyName,
310310
}
311311
}
312312
}
313-
catch { }
313+
catch (Exception ex)
314+
{
315+
McpLog.Warn($"SetTexture (string path) for '{propertyName}' failed: {ex.Message}");
316+
}
314317
}
315318

316319
if (value.Type == JTokenType.Object)
@@ -324,7 +327,10 @@ public static bool TrySetShaderProperty(Material material, string propertyName,
324327
return true;
325328
}
326329
}
327-
catch { }
330+
catch (Exception ex)
331+
{
332+
McpLog.Warn($"SetTexture (object) for '{propertyName}' failed: {ex.Message}");
333+
}
328334
}
329335

330336
Debug.LogWarning(

MCPForUnity/Editor/Tools/ManageMaterial.cs

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -361,9 +361,56 @@ private static object GetMaterialInfo(JObject @params)
361361
}
362362

363363
Shader shader = mat.shader;
364-
int propertyCount = ShaderUtil.GetPropertyCount(shader);
365364
var properties = new List<object>();
366-
365+
366+
#if UNITY_6000_0_OR_NEWER
367+
int propertyCount = shader.GetPropertyCount();
368+
for (int i = 0; i < propertyCount; i++)
369+
{
370+
string name = shader.GetPropertyName(i);
371+
var type = shader.GetPropertyType(i);
372+
string description = shader.GetPropertyDescription(i);
373+
374+
object currentValue = null;
375+
try
376+
{
377+
if (mat.HasProperty(name))
378+
{
379+
switch (type)
380+
{
381+
case UnityEngine.Rendering.ShaderPropertyType.Color:
382+
var c = mat.GetColor(name);
383+
currentValue = new { r = c.r, g = c.g, b = c.b, a = c.a };
384+
break;
385+
case UnityEngine.Rendering.ShaderPropertyType.Vector:
386+
var v = mat.GetVector(name);
387+
currentValue = new { x = v.x, y = v.y, z = v.z, w = v.w };
388+
break;
389+
case UnityEngine.Rendering.ShaderPropertyType.Float:
390+
case UnityEngine.Rendering.ShaderPropertyType.Range:
391+
currentValue = mat.GetFloat(name);
392+
break;
393+
case UnityEngine.Rendering.ShaderPropertyType.Texture:
394+
currentValue = mat.GetTexture(name)?.name ?? "null";
395+
break;
396+
}
397+
}
398+
}
399+
catch (Exception ex)
400+
{
401+
currentValue = $"<error: {ex.Message}>";
402+
}
403+
404+
properties.Add(new
405+
{
406+
name = name,
407+
type = type.ToString(),
408+
description = description,
409+
value = currentValue
410+
});
411+
}
412+
#else
413+
int propertyCount = ShaderUtil.GetPropertyCount(shader);
367414
for (int i = 0; i < propertyCount; i++)
368415
{
369416
string name = ShaderUtil.GetPropertyName(shader, i);
@@ -399,6 +446,7 @@ private static object GetMaterialInfo(JObject @params)
399446
value = currentValue
400447
});
401448
}
449+
#endif
402450

403451
return new {
404452
status = "success",

0 commit comments

Comments
 (0)