22using Newtonsoft . Json . Linq ;
33using UnityEngine . Rendering . Universal ;
44using UnityEngine . Rendering ;
5+ using UnityEditor ;
6+ using System . IO ;
57
68namespace MCPServer . Editor . Commands
79{
@@ -22,26 +24,72 @@ public static object SetMaterial(JObject @params)
2224 // Check if URP is being used
2325 bool isURP = GraphicsSettings . currentRenderPipeline is UniversalRenderPipelineAsset ;
2426
25- // Create material with appropriate shader based on render pipeline
26- Material material ;
27- if ( isURP )
27+ Material material = null ;
28+ string materialName = ( string ) @params [ "material_name" ] ;
29+ bool createIfMissing = ( bool ) ( @params [ "create_if_missing" ] ?? true ) ;
30+ string materialPath = null ;
31+
32+ // If material name is specified, try to find or create it
33+ if ( ! string . IsNullOrEmpty ( materialName ) )
2834 {
29- material = new Material ( Shader . Find ( "Universal Render Pipeline/Lit" ) ) ;
35+ // Ensure Materials folder exists
36+ const string materialsFolder = "Assets/Materials" ;
37+ if ( ! Directory . Exists ( materialsFolder ) )
38+ {
39+ Directory . CreateDirectory ( materialsFolder ) ;
40+ }
41+
42+ materialPath = $ "{ materialsFolder } /{ materialName } .mat";
43+ material = AssetDatabase . LoadAssetAtPath < Material > ( materialPath ) ;
44+
45+ if ( material == null && createIfMissing )
46+ {
47+ // Create new material with appropriate shader
48+ material = new Material ( isURP ? Shader . Find ( "Universal Render Pipeline/Lit" ) : Shader . Find ( "Standard" ) ) ;
49+ material . name = materialName ;
50+
51+ // Save the material asset
52+ AssetDatabase . CreateAsset ( material , materialPath ) ;
53+ AssetDatabase . SaveAssets ( ) ;
54+ }
55+ else if ( material == null )
56+ {
57+ throw new System . Exception ( $ "Material '{ materialName } ' not found and create_if_missing is false.") ;
58+ }
3059 }
3160 else
3261 {
33- material = new Material ( Shader . Find ( "Standard" ) ) ;
62+ // Create a temporary material if no name specified
63+ material = new Material ( isURP ? Shader . Find ( "Universal Render Pipeline/Lit" ) : Shader . Find ( "Standard" ) ) ;
3464 }
3565
36- if ( @params . ContainsKey ( "material_name" ) ) material . name = ( string ) @params [ "material_name" ] ;
66+ // Apply color if specified
3767 if ( @params . ContainsKey ( "color" ) )
3868 {
39- var colorArray = ( JArray ) @params [ "color" ] ?? throw new System . Exception ( "Invalid color parameter." ) ;
40- if ( colorArray . Count != 3 ) throw new System . Exception ( "Color must be an array of 3 floats [r, g, b]." ) ;
41- material . color = new Color ( ( float ) colorArray [ 0 ] , ( float ) colorArray [ 1 ] , ( float ) colorArray [ 2 ] ) ;
69+ var colorArray = ( JArray ) @params [ "color" ] ;
70+ if ( colorArray . Count < 3 || colorArray . Count > 4 )
71+ throw new System . Exception ( "Color must be an array of 3 (RGB) or 4 (RGBA) floats." ) ;
72+
73+ Color color = new Color (
74+ ( float ) colorArray [ 0 ] ,
75+ ( float ) colorArray [ 1 ] ,
76+ ( float ) colorArray [ 2 ] ,
77+ colorArray . Count > 3 ? ( float ) colorArray [ 3 ] : 1.0f
78+ ) ;
79+ material . color = color ;
80+
81+ // If this is a saved material, make sure to save the color change
82+ if ( ! string . IsNullOrEmpty ( materialPath ) )
83+ {
84+ EditorUtility . SetDirty ( material ) ;
85+ AssetDatabase . SaveAssets ( ) ;
86+ }
4287 }
88+
89+ // Apply the material to the renderer
4390 renderer . material = material ;
44- return new { material_name = material . name } ;
91+
92+ return new { material_name = material . name , path = materialPath } ;
4593 }
4694 }
4795}
0 commit comments