Search code examples
powershellargumentstexturepacker

Sovled: Powershell - How to store arguments to a variable and use it?


I have written a CMD script for batching atlas packs, and it works fine.

CMD Script

set OutputDir=%1
set MaxSize=%2
set Scale=%3

set TpCmd=--format unity-texture2d --smart-update --max-size %MaxSize% --scale %Scale%
TexturePacker %TpCmd% --data "%OutputDir%.tpsheet" --sheet "%OutputDir%.png" "D:\xxx"

.
.
Recently I am learning PowerShell and try to write a script that could works like above.

PowerShell Script

$AtlasMaxSize = 4096
$AtlasScale = 0.5

function Pack-Atlas($FileName) {
    $AtlasOptions = --format unity-texture2d --smart-update --max-size $AtlasMaxSize --scale $AtlasScale
    TexturePacker $AtlasOptions --data "$FileName.tpsheet" --sheet "$FileName.png" "D:\xxx"
}

.
.
But it seems not a correct way for declaring the $AtlasOptions variable.
I think maybe need some way to store the options, could someone help me or provide some keywords? .
.
.
.
.

Update

Thanks for @gvee and @TobyU
I have edited the script. .

function Pack-Atlas($FileName, $AtlasMaxSize, $AtlasScale) {
    $AtlasOptions = "--format unity-texture2d --smart-update --max-size $AtlasMaxSize --scale $AtlasScale"
    TexturePacker $AtlasOptions --data "$FileName.tpsheet" --sheet "$FileName.png" $TargetPath
}

But it seems not work. That's the error message I got:

TexturePacker:: error: Unknown argument
--format unity-texture2d --smart-update --max-size 4096 --scale 0.5 - please check parameters or visit http://www.codeandweb.com/texturepacker for newer version


Solution

  • You need to put the value of your variable within quotes like below to declare it properly:

    $AtlasMaxSize = 4096
    $AtlasScale = 0.5
    
    function Pack-Atlas($FileName) {
        $AtlasOptions = "--format unity-texture2d --smart-update --max-size $global:AtlasMaxSize --scale $global:AtlasScale"
        TexturePacker "$AtlasOptions --data '$($FileName).tpsheet' --sheet '$($FileName).png' 'D:\xxx'"
    }
    

    This should always be done like that if your value is not a number.