Sovled:Powershell - 如何将参数存储到变量并使用它?

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

我写了一个用于批处理图集包的 CMD 脚本,它工作正常。

命令脚本

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"

.
.
最近在学习PowerShell,尝试写一个可以像上面那样工作的脚本。

PowerShell 脚本

$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"
}

.
.
但这似乎不是声明 $AtlasOptions 变量的正确方法。
我认为可能需要某种方式来存储选项,有人可以帮助我或提供一些关键字吗? .
.
.
.
.

更新

感谢@gvee 和@TobyU
我已经编辑了脚本。 .

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
}

不过好像不行。 那是我收到的错误消息:

TexturePacker::错误:未知参数
--format unity-texture2d --smart-update --max-size 4096 --scale 0.5 - 请检查参数或访问http://www.codeandweb.com/texturepacker获取更新版本

您需要像下面这样将变量的值放在引号内以正确声明它:

$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'"
}

如果您的值不是数字,则应始终这样做。

您需要扩展您的函数以具有您可以传入的其他参数:

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" "D:\xxx"
}

然后您可以传入额外的参数:

Pack-Atlas -FileName "/temp/foo.bar" -AtlasMaxSize 4096 -AtlasScale 0.5