Skip to content
88 changes: 66 additions & 22 deletions MCPForUnity/Editor/Tools/ManageAsset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,9 @@ public static object HandleCommand(JObject @params)
case "create":
return CreateAsset(@params);
case "modify":
return ModifyAsset(path, @params["properties"] as JObject);
var properties = @params["properties"] as JObject;
Debug.Log($"[ManageAsset] Modify properties type: {@params["properties"]?.GetType()}, value: {@params["properties"]}");
return ModifyAsset(path, properties);
case "delete":
return DeleteAsset(path);
case "duplicate":
Expand Down Expand Up @@ -988,31 +990,68 @@ private static bool ApplyMaterialProperties(Material mat, JObject properties)
}
}
}
// Example: Set texture property
if (properties["texture"] is JObject texProps)
// Example: Set texture property (case-insensitive key and subkeys)
{
string propName = texProps["name"]?.ToString() ?? "_MainTex"; // Default main texture
string texPath = texProps["path"]?.ToString();
JObject texProps = null;
var direct = properties.Property("texture");
if (direct != null && direct.Value is JObject t0) texProps = t0;
if (texProps == null)
{
var ci = properties.Properties().FirstOrDefault(p => string.Equals(p.Name, "texture", StringComparison.OrdinalIgnoreCase));
if (ci != null && ci.Value is JObject t1) texProps = t1;
}
if (texProps == null) goto AfterTexture;

string propName = (texProps["name"] ?? texProps["Name"])?.ToString();
string texPath = (texProps["path"] ?? texProps["Path"])?.ToString();
if (!string.IsNullOrEmpty(texPath))
{
Texture newTex = AssetDatabase.LoadAssetAtPath<Texture>(
AssetPathUtility.SanitizeAssetPath(texPath)
);
if (
newTex != null
&& mat.HasProperty(propName)
&& mat.GetTexture(propName) != newTex
)
if (newTex == null)
{
mat.SetTexture(propName, newTex);
modified = true;
Debug.LogWarning($"Texture not found at path: {texPath}");
}
else if (newTex == null)
else
{
Debug.LogWarning($"Texture not found at path: {texPath}");
// Resolve candidate property names across pipelines
string[] candidates;
string lower = (propName ?? string.Empty).ToLowerInvariant();
if (string.IsNullOrEmpty(propName))
{
candidates = new[] { "_BaseMap", "_MainTex" };
}
else if (lower == "_maintex")
{
candidates = new[] { "_MainTex", "_BaseMap" };
}
else if (lower == "_basemap")
{
candidates = new[] { "_BaseMap", "_MainTex" };
}
else
{
candidates = new[] { propName };
}

foreach (var candidate in candidates)
{
if (mat.HasProperty(candidate))
{
if (mat.GetTexture(candidate) != newTex)
{
mat.SetTexture(candidate, newTex);
modified = true;
}
// Stop after first applicable candidate
break;
}
}
}
}
}
AfterTexture:

// --- Flexible direct property assignment ---
// Allow payloads like: { "_Color": [r,g,b,a] }, { "_Glossiness": 0.5 }, { "_MainTex": "Assets/.." }
Expand All @@ -1026,15 +1065,20 @@ string ResolvePropertyName(string name)
{
if (string.IsNullOrEmpty(name)) return name;
string[] candidates;
switch (name)
var lower = name.ToLowerInvariant();
switch (lower)
{
case "_Color": candidates = new[] { "_Color", "_BaseColor" }; break;
case "_BaseColor": candidates = new[] { "_BaseColor", "_Color" }; break;
case "_MainTex": candidates = new[] { "_MainTex", "_BaseMap" }; break;
case "_BaseMap": candidates = new[] { "_BaseMap", "_MainTex" }; break;
case "_Glossiness": candidates = new[] { "_Glossiness", "_Smoothness" }; break;
case "_Smoothness": candidates = new[] { "_Smoothness", "_Glossiness" }; break;
default: candidates = new[] { name }; break;
case "_color": candidates = new[] { "_Color", "_BaseColor" }; break;
case "_basecolor": candidates = new[] { "_BaseColor", "_Color" }; break;
case "_maintex": candidates = new[] { "_MainTex", "_BaseMap" }; break;
case "_basemap": candidates = new[] { "_BaseMap", "_MainTex" }; break;
case "_glossiness": candidates = new[] { "_Glossiness", "_Smoothness" }; break;
case "_smoothness": candidates = new[] { "_Smoothness", "_Glossiness" }; break;
// Friendly names → shader property names
case "metallic": candidates = new[] { "_Metallic" }; break;
case "smoothness": candidates = new[] { "_Smoothness", "_Glossiness" }; break;
case "albedo": candidates = new[] { "_BaseMap", "_MainTex" }; break;
default: candidates = new[] { name }; break; // keep original as-is
}
foreach (var candidate in candidates)
{
Expand Down
Loading