@@ -27,7 +27,8 @@ public static class ManageTexture
2727 "create_sprite" ,
2828 "apply_pattern" ,
2929 "apply_gradient" ,
30- "apply_noise"
30+ "apply_noise" ,
31+ "set_import_settings"
3132 } ;
3233
3334 private static ErrorResponse ValidateDimensions ( int width , int height , List < string > warnings )
@@ -78,6 +79,8 @@ public static object HandleCommand(JObject @params)
7879 return ApplyGradient ( @params ) ;
7980 case "apply_noise" :
8081 return ApplyNoise ( @params ) ;
82+ case "set_import_settings" :
83+ return SetImportSettings ( @params ) ;
8184 default :
8285 return new ErrorResponse ( $ "Unknown action: '{ action } '") ;
8386 }
@@ -254,20 +257,36 @@ private static object ModifyTexture(JObject @params)
254257
255258 try
256259 {
257- Texture2D texture = AssetDatabase . LoadAssetAtPath < Texture2D > ( fullPath ) ;
258- if ( texture == null )
259- return new ErrorResponse ( $ "Failed to load texture at path: { fullPath } ") ;
260+ var setPixelsToken = @params [ "setPixels" ] as JObject ;
261+ bool hasImportSettings = HasImportSettingsParams ( @params ) ;
260262
261- // Make the texture readable
262- string absolutePath = GetAbsolutePath ( fullPath ) ;
263- byte [ ] fileData = File . ReadAllBytes ( absolutePath ) ;
264- Texture2D editableTexture = new Texture2D ( texture . width , texture . height , TextureFormat . RGBA32 , false ) ;
265- editableTexture . LoadImage ( fileData ) ;
263+ // Validate import settings before any writes
264+ if ( hasImportSettings )
265+ {
266+ var validationError = ValidateImportSettingsParams ( @params ) ;
267+ if ( validationError != null ) return validationError ;
268+ }
266269
267- // Apply modifications
268- var setPixelsToken = @params [ "setPixels" ] as JObject ;
270+ // Fast path: only import settings, no pixel changes
271+ if ( setPixelsToken == null && hasImportSettings )
272+ {
273+ var error = ApplyImportSettingsParams ( fullPath , @params ) ;
274+ if ( error != null ) return error ;
275+ return new SuccessResponse ( $ "Texture modified: { fullPath } ") ;
276+ }
277+
278+ // Pixel modification path
269279 if ( setPixelsToken != null )
270280 {
281+ Texture2D texture = AssetDatabase . LoadAssetAtPath < Texture2D > ( fullPath ) ;
282+ if ( texture == null )
283+ return new ErrorResponse ( $ "Failed to load texture at path: { fullPath } ") ;
284+
285+ string absolutePath = GetAbsolutePath ( fullPath ) ;
286+ byte [ ] fileData = File . ReadAllBytes ( absolutePath ) ;
287+ Texture2D editableTexture = new Texture2D ( texture . width , texture . height , TextureFormat . RGBA32 , false ) ;
288+ editableTexture . LoadImage ( fileData ) ;
289+
271290 int x = setPixelsToken [ "x" ] ? . ToObject < int > ( ) ?? 0 ;
272291 int y = setPixelsToken [ "y" ] ? . ToObject < int > ( ) ?? 0 ;
273292 int w = setPixelsToken [ "width" ] ? . ToObject < int > ( ) ?? 1 ;
@@ -307,22 +326,25 @@ private static object ModifyTexture(JObject @params)
307326 UnityEngine . Object . DestroyImmediate ( editableTexture ) ;
308327 return new ErrorResponse ( "setPixels requires 'color' or 'pixels'." ) ;
309328 }
310- }
311329
312- editableTexture . Apply ( ) ;
330+ editableTexture . Apply ( ) ;
313331
314- // Save back to disk
315- byte [ ] imageData = TextureOps . EncodeTexture ( editableTexture , fullPath ) ;
316- if ( imageData == null || imageData . Length == 0 )
317- {
332+ byte [ ] imageData = TextureOps . EncodeTexture ( editableTexture , fullPath ) ;
333+ if ( imageData == null || imageData . Length == 0 )
334+ {
335+ UnityEngine . Object . DestroyImmediate ( editableTexture ) ;
336+ return new ErrorResponse ( $ "Failed to encode texture for '{ fullPath } '") ;
337+ }
338+ File . WriteAllBytes ( absolutePath , imageData ) ;
339+ AssetDatabase . ImportAsset ( fullPath , ImportAssetOptions . ForceUpdate ) ;
318340 UnityEngine . Object . DestroyImmediate ( editableTexture ) ;
319- return new ErrorResponse ( $ "Failed to encode texture for '{ fullPath } '") ;
320341 }
321- File . WriteAllBytes ( absolutePath , imageData ) ;
322342
323- AssetDatabase . ImportAsset ( fullPath , ImportAssetOptions . ForceUpdate ) ;
324-
325- UnityEngine . Object . DestroyImmediate ( editableTexture ) ;
343+ if ( hasImportSettings )
344+ {
345+ var importError = ApplyImportSettingsParams ( fullPath , @params ) ;
346+ if ( importError != null ) return importError ;
347+ }
326348
327349 return new SuccessResponse ( $ "Texture modified: { fullPath } ") ;
328350 }
@@ -703,6 +725,85 @@ private static void ApplyPerlinNoise(Texture2D texture, List<Color32> palette, f
703725 }
704726 }
705727
728+ private static bool HasImportSettingsParams ( JObject @params )
729+ {
730+ JToken importSettingsToken = @params [ "import_settings" ] ?? @params [ "importSettings" ] ;
731+ JToken asSpriteToken = @params [ "as_sprite" ] ?? @params [ "spriteSettings" ] ;
732+
733+ bool hasImportSettings = importSettingsToken is JObject importObject && importObject . HasValues ;
734+ bool hasSpriteSettings = ( asSpriteToken is JObject spriteObject && spriteObject . HasValues )
735+ || ( asSpriteToken ? . Type == JTokenType . Boolean && asSpriteToken . ToObject < bool > ( ) ) ;
736+
737+ return hasImportSettings || hasSpriteSettings ;
738+ }
739+
740+ private static object ValidateImportSettingsParams ( JObject @params )
741+ {
742+ JToken importSettingsToken = @params [ "import_settings" ] ?? @params [ "importSettings" ] ;
743+ JToken asSpriteToken = @params [ "as_sprite" ] ?? @params [ "spriteSettings" ] ;
744+
745+ if ( importSettingsToken != null && asSpriteToken != null )
746+ {
747+ return new ErrorResponse ( "Cannot specify both 'import_settings' and 'as_sprite'." ) ;
748+ }
749+ return null ;
750+ }
751+
752+ private static object ApplyImportSettingsParams ( string fullPath , JObject @params )
753+ {
754+ JToken importSettingsToken = @params [ "import_settings" ] ?? @params [ "importSettings" ] ;
755+ JToken asSpriteToken = @params [ "as_sprite" ] ?? @params [ "spriteSettings" ] ;
756+
757+ if ( importSettingsToken != null && asSpriteToken != null )
758+ {
759+ return new ErrorResponse (
760+ "Cannot specify both 'import_settings' and 'as_sprite'. " +
761+ "Use 'import_settings' with textureType='Sprite' instead." ) ;
762+ }
763+
764+ if ( importSettingsToken != null )
765+ {
766+ ConfigureTextureImporter ( fullPath , importSettingsToken ) ;
767+ }
768+ else if ( asSpriteToken != null &&
769+ ( asSpriteToken . Type == JTokenType . Boolean ? asSpriteToken . ToObject < bool > ( ) : true ) )
770+ {
771+ ConfigureAsSprite ( fullPath , asSpriteToken . Type == JTokenType . Object ? asSpriteToken : null ) ;
772+ }
773+
774+ return null ;
775+ }
776+
777+ private static object SetImportSettings ( JObject @params )
778+ {
779+ var toolParams = new MCPForUnity . Editor . Helpers . ToolParams ( @params ) ;
780+ var pathResult = toolParams . GetRequired ( "path" , "'path' is required for set_import_settings." ) ;
781+ if ( ! pathResult . IsSuccess )
782+ return new ErrorResponse ( pathResult . ErrorMessage ) ;
783+ string path = pathResult . Value ;
784+
785+ string fullPath = AssetPathUtility . SanitizeAssetPath ( path ) ;
786+ if ( ! AssetExists ( fullPath ) )
787+ return new ErrorResponse ( $ "Texture not found at path: { fullPath } ") ;
788+
789+ try
790+ {
791+ if ( ! HasImportSettingsParams ( @params ) )
792+ {
793+ return new ErrorResponse ( "Either 'import_settings' or 'as_sprite' is required." ) ;
794+ }
795+
796+ var error = ApplyImportSettingsParams ( fullPath , @params ) ;
797+ if ( error != null ) return error ;
798+
799+ return new SuccessResponse ( $ "Import settings updated for: { fullPath } ", new { path = fullPath } ) ;
800+ }
801+ catch ( Exception e )
802+ {
803+ return new ErrorResponse ( $ "Failed to set import settings: { e . Message } ") ;
804+ }
805+ }
806+
706807 private static void ConfigureAsSprite ( string path , JToken spriteSettings )
707808 {
708809 TextureImporter importer = AssetImporter . GetAtPath ( path ) as TextureImporter ;
0 commit comments