Skip to content

Commit faf9aff

Browse files
fix: JSON material property handling + tests (manage_asset) #90 (CoplayDev#349)
* feat: add JSON property handling for materials; add tests for JSON coercion and end-to-end; update test project manifest and ProjectVersion * fix(manage_asset): support structured texture blocks case-insensitively; resolve _BaseMap/_MainTex automatically and apply when missing name * Update MCPForUnity/Editor/Tools/ManageAsset.cs Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com> * refactor(manage_asset): remove goto; reuse alias resolver for structured texture (supports 'albedo'); tests: self-sufficient texture asset and _BaseColor/_Color guards; python: assert success in invalid JSON case * chore(manage_asset): remove duplicate return in modify case * tests: fix mocks/patching for manage_asset/manage_gameobject; make invalid-json case tolerant; ensure prefab modify test patches transport correctly * ci: allow manual dispatch for Unity EditMode tests (workflow_dispatch) --------- Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com>
1 parent 4643802 commit faf9aff

File tree

8 files changed

+843
-35
lines changed

8 files changed

+843
-35
lines changed

.github/workflows/unity-tests.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
name: Unity Tests
22

33
on:
4+
workflow_dispatch: {}
45
push:
56
branches: [main]
67
paths:

MCPForUnity/Editor/Tools/ManageAsset.cs

Lines changed: 49 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ public static object HandleCommand(JObject @params)
8888
case "create":
8989
return CreateAsset(@params);
9090
case "modify":
91-
return ModifyAsset(path, @params["properties"] as JObject);
91+
var properties = @params["properties"] as JObject;
92+
return ModifyAsset(path, properties);
9293
case "delete":
9394
return DeleteAsset(path);
9495
case "duplicate":
@@ -988,28 +989,43 @@ private static bool ApplyMaterialProperties(Material mat, JObject properties)
988989
}
989990
}
990991
}
991-
// Example: Set texture property
992-
if (properties["texture"] is JObject texProps)
992+
// Example: Set texture property (case-insensitive key and subkeys)
993993
{
994-
string propName = texProps["name"]?.ToString() ?? "_MainTex"; // Default main texture
995-
string texPath = texProps["path"]?.ToString();
996-
if (!string.IsNullOrEmpty(texPath))
994+
JObject texProps = null;
995+
var direct = properties.Property("texture");
996+
if (direct != null && direct.Value is JObject t0) texProps = t0;
997+
if (texProps == null)
997998
{
998-
Texture newTex = AssetDatabase.LoadAssetAtPath<Texture>(
999-
AssetPathUtility.SanitizeAssetPath(texPath)
1000-
);
1001-
if (
1002-
newTex != null
1003-
&& mat.HasProperty(propName)
1004-
&& mat.GetTexture(propName) != newTex
1005-
)
1006-
{
1007-
mat.SetTexture(propName, newTex);
1008-
modified = true;
1009-
}
1010-
else if (newTex == null)
999+
var ci = properties.Properties().FirstOrDefault(
1000+
p => string.Equals(p.Name, "texture", StringComparison.OrdinalIgnoreCase));
1001+
if (ci != null && ci.Value is JObject t1) texProps = t1;
1002+
}
1003+
if (texProps != null)
1004+
{
1005+
string rawName = (texProps["name"] ?? texProps["Name"])?.ToString();
1006+
string texPath = (texProps["path"] ?? texProps["Path"])?.ToString();
1007+
if (!string.IsNullOrEmpty(texPath))
10111008
{
1012-
Debug.LogWarning($"Texture not found at path: {texPath}");
1009+
var newTex = AssetDatabase.LoadAssetAtPath<Texture>(
1010+
AssetPathUtility.SanitizeAssetPath(texPath));
1011+
if (newTex == null)
1012+
{
1013+
Debug.LogWarning($"Texture not found at path: {texPath}");
1014+
}
1015+
else
1016+
{
1017+
// Reuse alias resolver so friendly names like 'albedo' work here too
1018+
string candidateName = string.IsNullOrEmpty(rawName) ? "_BaseMap" : rawName;
1019+
string targetProp = ResolvePropertyName(candidateName);
1020+
if (!string.IsNullOrEmpty(targetProp) && mat.HasProperty(targetProp))
1021+
{
1022+
if (mat.GetTexture(targetProp) != newTex)
1023+
{
1024+
mat.SetTexture(targetProp, newTex);
1025+
modified = true;
1026+
}
1027+
}
1028+
}
10131029
}
10141030
}
10151031
}
@@ -1026,15 +1042,20 @@ string ResolvePropertyName(string name)
10261042
{
10271043
if (string.IsNullOrEmpty(name)) return name;
10281044
string[] candidates;
1029-
switch (name)
1045+
var lower = name.ToLowerInvariant();
1046+
switch (lower)
10301047
{
1031-
case "_Color": candidates = new[] { "_Color", "_BaseColor" }; break;
1032-
case "_BaseColor": candidates = new[] { "_BaseColor", "_Color" }; break;
1033-
case "_MainTex": candidates = new[] { "_MainTex", "_BaseMap" }; break;
1034-
case "_BaseMap": candidates = new[] { "_BaseMap", "_MainTex" }; break;
1035-
case "_Glossiness": candidates = new[] { "_Glossiness", "_Smoothness" }; break;
1036-
case "_Smoothness": candidates = new[] { "_Smoothness", "_Glossiness" }; break;
1037-
default: candidates = new[] { name }; break;
1048+
case "_color": candidates = new[] { "_Color", "_BaseColor" }; break;
1049+
case "_basecolor": candidates = new[] { "_BaseColor", "_Color" }; break;
1050+
case "_maintex": candidates = new[] { "_MainTex", "_BaseMap" }; break;
1051+
case "_basemap": candidates = new[] { "_BaseMap", "_MainTex" }; break;
1052+
case "_glossiness": candidates = new[] { "_Glossiness", "_Smoothness" }; break;
1053+
case "_smoothness": candidates = new[] { "_Smoothness", "_Glossiness" }; break;
1054+
// Friendly names → shader property names
1055+
case "metallic": candidates = new[] { "_Metallic" }; break;
1056+
case "smoothness": candidates = new[] { "_Smoothness", "_Glossiness" }; break;
1057+
case "albedo": candidates = new[] { "_BaseMap", "_MainTex" }; break;
1058+
default: candidates = new[] { name }; break; // keep original as-is
10381059
}
10391060
foreach (var candidate in candidates)
10401061
{

0 commit comments

Comments
 (0)