55using Newtonsoft . Json . Linq ;
66using UnityEngine ;
77using UnityEditor ;
8- using MCPForUnity . Editor . Tools ; // For AssetPathUtility if needed? No, it uses AssetDatabase directly usually.
8+ using MCPForUnity . Editor . Tools ;
99
1010namespace MCPForUnity . Editor . Helpers
1111{
@@ -20,11 +20,19 @@ public static bool ApplyProperties(Material mat, JObject properties, JsonSeriali
2020 return false ;
2121 bool modified = false ;
2222
23+ // Helper for case-insensitive lookup
24+ JToken GetValue ( string key )
25+ {
26+ return properties . Properties ( )
27+ . FirstOrDefault ( p => string . Equals ( p . Name , key , StringComparison . OrdinalIgnoreCase ) ) ? . Value ;
28+ }
29+
2330 // --- Structured / Legacy Format Handling ---
2431 // Example: Set shader
25- if ( properties [ "shader" ] ? . Type == JTokenType . String )
32+ var shaderToken = GetValue ( "shader" ) ;
33+ if ( shaderToken ? . Type == JTokenType . String )
2634 {
27- string shaderRequest = properties [ "shader" ] . ToString ( ) ;
35+ string shaderRequest = shaderToken . ToString ( ) ;
2836 // Set shader
2937 Shader newShader = RenderPipelineUtility . ResolveShader ( shaderRequest ) ;
3038 if ( newShader != null && mat . shader != newShader )
@@ -35,7 +43,8 @@ public static bool ApplyProperties(Material mat, JObject properties, JsonSeriali
3543 }
3644
3745 // Example: Set color property (structured)
38- if ( properties [ "color" ] is JObject colorProps )
46+ var colorToken = GetValue ( "color" ) ;
47+ if ( colorToken is JObject colorProps )
3948 {
4049 string propName = colorProps [ "name" ] ? . ToString ( ) ?? GetMainColorPropertyName ( mat ) ;
4150 if ( colorProps [ "value" ] is JArray colArr && colArr . Count >= 3 )
@@ -58,7 +67,7 @@ public static bool ApplyProperties(Material mat, JObject properties, JsonSeriali
5867 }
5968 }
6069 }
61- else if ( properties [ "color" ] is JArray colorArr ) // Structured shorthand
70+ else if ( colorToken is JArray colorArr ) // Structured shorthand
6271 {
6372 string propName = GetMainColorPropertyName ( mat ) ;
6473 try
@@ -74,7 +83,8 @@ public static bool ApplyProperties(Material mat, JObject properties, JsonSeriali
7483 }
7584
7685 // Example: Set float property (structured)
77- if ( properties [ "float" ] is JObject floatProps )
86+ var floatToken = GetValue ( "float" ) ;
87+ if ( floatToken is JObject floatProps )
7888 {
7989 string propName = floatProps [ "name" ] ? . ToString ( ) ;
8090 if ( ! string . IsNullOrEmpty ( propName ) &&
@@ -95,16 +105,8 @@ public static bool ApplyProperties(Material mat, JObject properties, JsonSeriali
95105
96106 // Example: Set texture property (structured)
97107 {
98- JObject texProps = null ;
99- var direct = properties . Property ( "texture" ) ;
100- if ( direct != null && direct . Value is JObject t0 ) texProps = t0 ;
101- if ( texProps == null )
102- {
103- var ci = properties . Properties ( ) . FirstOrDefault (
104- p => string . Equals ( p . Name , "texture" , StringComparison . OrdinalIgnoreCase ) ) ;
105- if ( ci != null && ci . Value is JObject t1 ) texProps = t1 ;
106- }
107- if ( texProps != null )
108+ var texToken = GetValue ( "texture" ) ;
109+ if ( texToken is JObject texProps )
108110 {
109111 string rawName = ( texProps [ "name" ] ?? texProps [ "Name" ] ) ? . ToString ( ) ;
110112 string texPath = ( texProps [ "path" ] ?? texProps [ "Path" ] ) ? . ToString ( ) ;
@@ -221,19 +223,25 @@ public static bool TrySetShaderProperty(Material material, string propertyName,
221223 {
222224 if ( jArray . Count == 4 )
223225 {
224- if ( material . HasProperty ( propertyName ) )
225- {
226- try { material . SetColor ( propertyName , ParseColor ( value , serializer ) ) ; return true ; } catch { }
227- try { Vector4 vec = value . ToObject < Vector4 > ( serializer ) ; material . SetVector ( propertyName , vec ) ; return true ; } catch { }
228- }
226+ if ( material . HasProperty ( propertyName ) )
227+ {
228+ try { material . SetColor ( propertyName , ParseColor ( value , serializer ) ) ; return true ; } catch { }
229+ try { Vector4 vec = value . ToObject < Vector4 > ( serializer ) ; material . SetVector ( propertyName , vec ) ; return true ; } catch { }
230+ }
229231 }
230232 else if ( jArray . Count == 3 )
231233 {
232- try { material . SetColor ( propertyName , ParseColor ( value , serializer ) ) ; return true ; } catch { }
234+ if ( material . HasProperty ( propertyName ) )
235+ {
236+ try { material . SetColor ( propertyName , ParseColor ( value , serializer ) ) ; return true ; } catch { }
237+ }
233238 }
234239 else if ( jArray . Count == 2 )
235240 {
236- try { Vector2 vec = value . ToObject < Vector2 > ( serializer ) ; material . SetVector ( propertyName , vec ) ; return true ; } catch { }
241+ if ( material . HasProperty ( propertyName ) )
242+ {
243+ try { Vector2 vec = value . ToObject < Vector2 > ( serializer ) ; material . SetVector ( propertyName , vec ) ; return true ; } catch { }
244+ }
237245 }
238246 }
239247 else if ( value . Type == JTokenType . Float || value . Type == JTokenType . Integer )
0 commit comments