Skip to content

feat: Support direct material property assignment from JSON params #120

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions UnityMcpBridge/Editor/Tools/ManageAsset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -854,6 +854,64 @@ private static bool ApplyMaterialProperties(Material mat, JObject properties)
return false;
bool modified = false;

// Loop through all properties for direct assignment
foreach (var prop in properties.Properties())
{
string propName = prop.Name;
JToken value = prop.Value;
if (mat.HasProperty(propName))
{
// Handle color arrays (e.g., _Color: [0,1,0,1])
if (value is JArray arr && (arr.Count == 3 || arr.Count == 4))
{
try
{
Color newColor = new Color(
arr[0].ToObject<float>(),
arr[1].ToObject<float>(),
arr[2].ToObject<float>(),
arr.Count > 3 ? arr[3].ToObject<float>() : 1.0f
);
if (mat.GetColor(propName) != newColor)
{
mat.SetColor(propName, newColor);
modified = true;
}
}
catch (Exception ex)
{
Debug.LogWarning($"Error parsing color array for '{propName}': {ex.Message}");
}
}
// Handle float/int
else if (value.Type == JTokenType.Float || value.Type == JTokenType.Integer)
{
float newVal = value.ToObject<float>();
if (mat.GetFloat(propName) != newVal)
{
mat.SetFloat(propName, newVal);
modified = true;
}
}
// Handle string (could be texture path)
else if (value.Type == JTokenType.String)
{
string strVal = value.ToString();
// Try to load as texture
Texture tex = AssetDatabase.LoadAssetAtPath<Texture>(SanitizeAssetPath(strVal));
if (tex != null)
{
if (mat.GetTexture(propName) != tex)
{
mat.SetTexture(propName, tex);
modified = true;
}
}
}
}
}

// Existing structured handling (for backward compatibility)
// Example: Set shader
if (properties["shader"]?.Type == JTokenType.String)
{
Expand Down