@@ -63,6 +63,21 @@ public static object HandleCommand(JObject @params)
6363 // Common parameters
6464 string path = @params [ "path" ] ? . ToString ( ) ;
6565
66+ // Coerce string JSON to JObject for 'properties' if provided as a JSON string
67+ var propertiesToken = @params [ "properties" ] ;
68+ if ( propertiesToken != null && propertiesToken . Type == JTokenType . String )
69+ {
70+ try
71+ {
72+ var parsed = JObject . Parse ( propertiesToken . ToString ( ) ) ;
73+ @params [ "properties" ] = parsed ;
74+ }
75+ catch ( Exception e )
76+ {
77+ Debug . LogWarning ( $ "[ManageAsset] Could not parse 'properties' JSON string: { e . Message } ") ;
78+ }
79+ }
80+
6681 try
6782 {
6883 switch ( action )
@@ -999,7 +1014,108 @@ private static bool ApplyMaterialProperties(Material mat, JObject properties)
9991014 }
10001015 }
10011016
1002- // TODO: Add handlers for other property types (Vectors, Ints, Keywords, RenderQueue, etc.)
1017+ // --- Flexible direct property assignment ---
1018+ // Allow payloads like: { "_Color": [r,g,b,a] }, { "_Glossiness": 0.5 }, { "_MainTex": "Assets/.." }
1019+ // while retaining backward compatibility with the structured keys above.
1020+ // This iterates all top-level keys except the reserved structured ones and applies them
1021+ // if they match known shader properties.
1022+ var reservedKeys = new HashSet < string > ( StringComparer . OrdinalIgnoreCase ) { "shader" , "color" , "float" , "texture" } ;
1023+
1024+ // Helper resolves common URP/Standard aliasing (e.g., _Color <-> _BaseColor, _MainTex <-> _BaseMap, _Glossiness <-> _Smoothness)
1025+ string ResolvePropertyName ( string name )
1026+ {
1027+ if ( string . IsNullOrEmpty ( name ) ) return name ;
1028+ string [ ] candidates ;
1029+ switch ( name )
1030+ {
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 ;
1038+ }
1039+ foreach ( var candidate in candidates )
1040+ {
1041+ if ( mat . HasProperty ( candidate ) ) return candidate ;
1042+ }
1043+ return name ; // fall back to original
1044+ }
1045+
1046+ foreach ( var prop in properties . Properties ( ) )
1047+ {
1048+ if ( reservedKeys . Contains ( prop . Name ) ) continue ;
1049+ string shaderProp = ResolvePropertyName ( prop . Name ) ;
1050+ JToken v = prop . Value ;
1051+
1052+ // Color: numeric array [r,g,b,(a)]
1053+ if ( v is JArray arr && arr . Count >= 3 && arr . All ( t => t . Type == JTokenType . Float || t . Type == JTokenType . Integer ) )
1054+ {
1055+ if ( mat . HasProperty ( shaderProp ) )
1056+ {
1057+ try
1058+ {
1059+ var c = new Color (
1060+ arr [ 0 ] . ToObject < float > ( ) ,
1061+ arr [ 1 ] . ToObject < float > ( ) ,
1062+ arr [ 2 ] . ToObject < float > ( ) ,
1063+ arr . Count > 3 ? arr [ 3 ] . ToObject < float > ( ) : 1f
1064+ ) ;
1065+ if ( mat . GetColor ( shaderProp ) != c )
1066+ {
1067+ mat . SetColor ( shaderProp , c ) ;
1068+ modified = true ;
1069+ }
1070+ }
1071+ catch ( Exception ex )
1072+ {
1073+ Debug . LogWarning ( $ "Error setting color '{ shaderProp } ': { ex . Message } ") ;
1074+ }
1075+ }
1076+ continue ;
1077+ }
1078+
1079+ // Float: single number
1080+ if ( v . Type == JTokenType . Float || v . Type == JTokenType . Integer )
1081+ {
1082+ if ( mat . HasProperty ( shaderProp ) )
1083+ {
1084+ try
1085+ {
1086+ float f = v . ToObject < float > ( ) ;
1087+ if ( ! Mathf . Approximately ( mat . GetFloat ( shaderProp ) , f ) )
1088+ {
1089+ mat . SetFloat ( shaderProp , f ) ;
1090+ modified = true ;
1091+ }
1092+ }
1093+ catch ( Exception ex )
1094+ {
1095+ Debug . LogWarning ( $ "Error setting float '{ shaderProp } ': { ex . Message } ") ;
1096+ }
1097+ }
1098+ continue ;
1099+ }
1100+
1101+ // Texture: string path
1102+ if ( v . Type == JTokenType . String )
1103+ {
1104+ string texPath = v . ToString ( ) ;
1105+ if ( ! string . IsNullOrEmpty ( texPath ) && mat . HasProperty ( shaderProp ) )
1106+ {
1107+ var tex = AssetDatabase . LoadAssetAtPath < Texture > ( AssetPathUtility . SanitizeAssetPath ( texPath ) ) ;
1108+ if ( tex != null && mat . GetTexture ( shaderProp ) != tex )
1109+ {
1110+ mat . SetTexture ( shaderProp , tex ) ;
1111+ modified = true ;
1112+ }
1113+ }
1114+ continue ;
1115+ }
1116+ }
1117+
1118+ // TODO: Add handlers for other property types (Vectors, Ints, Keywords, RenderQueue, etc.)
10031119 return modified ;
10041120 }
10051121
0 commit comments