@@ -481,29 +481,113 @@ def sprite(path: str, width: int, height: int, image_path: Optional[str], color:
481481 print_success (f"Created sprite: { path } " )
482482
483483
484+ def _build_import_settings_from_flags (
485+ texture_type : Optional [str ],
486+ sprite_mode : Optional [str ],
487+ sprite_ppu : Optional [float ],
488+ max_size : Optional [str ],
489+ compression : Optional [str ],
490+ generate_mipmaps : Optional [bool ],
491+ srgb : Optional [bool ],
492+ readable : Optional [bool ],
493+ ) -> dict [str , Any ]:
494+ """Build importSettings dict from CLI flags. Returns empty dict if no flags set."""
495+ import_settings : dict [str , Any ] = {}
496+ if texture_type :
497+ import_settings ["textureType" ] = _TEXTURE_TYPES [texture_type ]
498+ if sprite_mode :
499+ import_settings ["spriteImportMode" ] = _SPRITE_MODES [sprite_mode ]
500+ if sprite_ppu is not None :
501+ import_settings ["spritePixelsPerUnit" ] = sprite_ppu
502+ if max_size :
503+ import_settings ["maxTextureSize" ] = int (max_size )
504+ if compression :
505+ import_settings ["textureCompression" ] = _COMPRESSIONS [compression ]
506+ if generate_mipmaps is not None :
507+ import_settings ["mipmapEnabled" ] = generate_mipmaps
508+ if srgb is not None :
509+ import_settings ["sRGBTexture" ] = srgb
510+ if readable is not None :
511+ import_settings ["isReadable" ] = readable
512+ return import_settings
513+
514+
515+ def _apply_import_flags_to_params (
516+ params : dict [str , Any ],
517+ texture_type : Optional [str ],
518+ sprite_mode : Optional [str ],
519+ sprite_ppu : Optional [float ],
520+ max_size : Optional [str ],
521+ compression : Optional [str ],
522+ generate_mipmaps : Optional [bool ],
523+ srgb : Optional [bool ],
524+ readable : Optional [bool ],
525+ as_sprite : bool ,
526+ ) -> bool :
527+ """Validate and apply import-setting flags to params dict. Returns True if any import setting present."""
528+ has_other_flags = any (v is not None for v in (
529+ texture_type , sprite_mode , sprite_ppu , max_size , compression , generate_mipmaps , srgb , readable ))
530+
531+ if as_sprite :
532+ if has_other_flags :
533+ print_error ("--as-sprite cannot be combined with other import-setting flags" )
534+ sys .exit (1 )
535+ params ["spriteSettings" ] = {"pivot" : [0.5 , 0.5 ], "pixelsPerUnit" : 100 }
536+ return True
537+
538+ if has_other_flags :
539+ import_settings = _build_import_settings_from_flags (
540+ texture_type , sprite_mode , sprite_ppu , max_size , compression ,
541+ generate_mipmaps , srgb , readable )
542+ if import_settings :
543+ params ["importSettings" ] = import_settings
544+ return True
545+
546+ return False
547+
548+
484549@texture .command ("modify" )
485550@click .argument ("path" )
486- @click .option ("--set-pixels" , required = True , help = "Modification args as JSON" )
551+ @click .option ("--set-pixels" , default = None , help = "Modification args as JSON" )
552+ @click .option ("--texture-type" , type = click .Choice (list (_TEXTURE_TYPES .keys ())), help = "Texture type" )
553+ @click .option ("--sprite-mode" , type = click .Choice (list (_SPRITE_MODES .keys ())), help = "Sprite import mode" )
554+ @click .option ("--sprite-ppu" , type = float , help = "Sprite pixels per unit" )
555+ @click .option ("--max-size" , type = click .Choice (["32" , "64" , "128" , "256" , "512" , "1024" , "2048" , "4096" , "8192" , "16384" ]), help = "Max texture size" )
556+ @click .option ("--compression" , type = click .Choice (list (_COMPRESSIONS .keys ())), help = "Compression quality" )
557+ @click .option ("--generate-mipmaps/--no-mipmaps" , default = None , help = "Generate mipmaps" )
558+ @click .option ("--srgb/--linear" , default = None , help = "sRGB color texture" )
559+ @click .option ("--readable/--no-readable" , default = None , help = "Read/Write enabled" )
560+ @click .option ("--as-sprite" , is_flag = True , help = "Shorthand: set texture type to Sprite with defaults" )
487561@handle_unity_errors
488- def modify (path : str , set_pixels : str ):
562+ def modify (path : str , set_pixels : Optional [str ], texture_type : Optional [str ], sprite_mode : Optional [str ],
563+ sprite_ppu : Optional [float ], max_size : Optional [str ], compression : Optional [str ],
564+ generate_mipmaps : Optional [bool ], srgb : Optional [bool ], readable : Optional [bool ],
565+ as_sprite : bool ):
489566 """Modify an existing texture.
490567
491568 \b
492569 Examples:
493570 unity-mcp texture modify Assets/Tex.png --set-pixels '{"x":0,"y":0,"width":10,"height":10,"color":[255,0,0]}'
494571 unity-mcp texture modify Assets/Tex.png --set-pixels '{"x":0,"y":0,"width":2,"height":2,"pixels":[[255,0,0,255],[0,255,0,255],[0,0,255,255],[255,255,0,255]]}'
572+ unity-mcp texture modify Assets/UI/icon.png --as-sprite
573+ unity-mcp texture modify Assets/UI/bg.png --texture-type sprite --max-size 2048
495574 """
496575 config = get_config ()
497576
498- params : dict [str , Any ] = {
499- "action" : "modify" ,
500- "path" : path ,
501- }
577+ params : dict [str , Any ] = {"action" : "modify" , "path" : path }
502578
503- try :
504- params ["setPixels" ] = _normalize_set_pixels (set_pixels )
505- except ValueError as e :
506- print_error (str (e ))
579+ has_import = _apply_import_flags_to_params (
580+ params , texture_type , sprite_mode , sprite_ppu , max_size ,
581+ compression , generate_mipmaps , srgb , readable , as_sprite )
582+
583+ if set_pixels is not None :
584+ try :
585+ params ["setPixels" ] = _normalize_set_pixels (set_pixels )
586+ except ValueError as e :
587+ print_error (str (e ))
588+ sys .exit (1 )
589+ elif not has_import :
590+ print_error ("At least one of --set-pixels or an import-setting flag must be provided" )
507591 sys .exit (1 )
508592
509593 result = run_command ("manage_texture" , params , config )
@@ -538,3 +622,46 @@ def delete(path: str, force: bool):
538622 click .echo (format_output (result , config .format ))
539623 if result .get ("success" ):
540624 print_success (f"Deleted texture: { path } " )
625+
626+
627+ @texture .command ("set-import-settings" )
628+ @click .argument ("path" )
629+ @click .option ("--texture-type" , type = click .Choice (list (_TEXTURE_TYPES .keys ())), help = "Texture type" )
630+ @click .option ("--sprite-mode" , type = click .Choice (list (_SPRITE_MODES .keys ())), help = "Sprite import mode" )
631+ @click .option ("--sprite-ppu" , type = float , help = "Sprite pixels per unit" )
632+ @click .option ("--max-size" , type = click .Choice (["32" , "64" , "128" , "256" , "512" , "1024" , "2048" , "4096" , "8192" , "16384" ]), help = "Max texture size" )
633+ @click .option ("--compression" , type = click .Choice (list (_COMPRESSIONS .keys ())), help = "Compression quality" )
634+ @click .option ("--generate-mipmaps/--no-mipmaps" , default = None , help = "Generate mipmaps" )
635+ @click .option ("--srgb/--linear" , default = None , help = "sRGB color texture" )
636+ @click .option ("--readable/--no-readable" , default = None , help = "Read/Write enabled" )
637+ @click .option ("--as-sprite" , is_flag = True , help = "Shorthand: set texture type to Sprite with defaults" )
638+ @handle_unity_errors
639+ def set_import_settings (path : str , texture_type : Optional [str ], sprite_mode : Optional [str ],
640+ sprite_ppu : Optional [float ], max_size : Optional [str ],
641+ compression : Optional [str ], generate_mipmaps : Optional [bool ],
642+ srgb : Optional [bool ], readable : Optional [bool ], as_sprite : bool ):
643+ """Change import settings on an existing texture.
644+
645+ \b
646+ Examples:
647+ unity-mcp texture set-import-settings Assets/UI/icon.png --texture-type sprite
648+ unity-mcp texture set-import-settings Assets/UI/icon.png --as-sprite
649+ unity-mcp texture set-import-settings Assets/UI/icon.png --texture-type sprite --sprite-mode single --sprite-ppu 100
650+ unity-mcp texture set-import-settings Assets/UI/bg.png --max-size 2048 --compression high_quality
651+ """
652+ config = get_config ()
653+
654+ params : dict [str , Any ] = {"action" : "set_import_settings" , "path" : path }
655+
656+ has_import = _apply_import_flags_to_params (
657+ params , texture_type , sprite_mode , sprite_ppu , max_size ,
658+ compression , generate_mipmaps , srgb , readable , as_sprite )
659+
660+ if not has_import :
661+ print_error ("At least one import setting must be specified" )
662+ sys .exit (1 )
663+
664+ result = run_command ("manage_texture" , params , config )
665+ click .echo (format_output (result , config .format ))
666+ if result .get ("success" ):
667+ print_success (f"Import settings updated: { path } " )
0 commit comments