使用Powershell将Cocos游戏打包为抖音小玩法包体

前言

Cocos Creator开发的游戏是可以直接发布在原生平台上的。目前在运行的抖音弹幕小玩法就需要打包成exe文件后提交给抖音平台,但是每次都需要手动打包压缩等比较麻烦。本文通过摸索来使用 powershell 实现命令行打包和压缩的过程,让下面的操作一条命令实现。

  • Cocos Creator 点击构建,等待完成
  • Cocos Creator 点击生成,等待完成
  • 找到生成的目录文件,重命名为 gameName_version 形式
  • 压缩该目录为 gameName_version 形式

涉及工具

  • Visual Studio 2022
  • Cocos Creator 3.8
  • Powershell

具体流程

1、安装Visual Studio及组件

这里就不在赘述Cocos Creator的安装过程了,在安装Visual Studio的时候需要选择一些组件,缺少组件可能会导致构建和生成exe失败。

2、配置MSBuild环境变量

命令行打包,我们需要用到MSBuild,它是Visual Studio内置的,如果你Visual Studio是安装在C盘的,目录应该如下(这里根据你安装的位置来定):

makefile 复制代码
C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Current\Bin

修改环境变量,将上述的目录添加到Path变量中。

3、编写powershell脚本

在项目的根目录创建一个build.ps1的文件,以打包和生成抖音的exe包体为例写入下面的内容,hello_world 是一个示例名称。这里是 MSBuild说明文档 也可以参考下。

powershell 复制代码
# cocos creator可执行文件位置
$creatorPath = "C:\ProgramData\cocos\editors\Creator\3.8.0\CocosCreator.exe"
# cocos构建结果路径
$projectPath = ".\build\douyin\proj"
# cocos构建配置文件
$buildConfig = "./douyin.json"
# 抖音的小玩法版本号码
$douyinVersion = "1.0.0"
# 小玩法名称
$gameName = "hello_world"
# 压缩文件名
$zipPath = ".${gameName}_${douyinVersion}.zip"
# msbuild结果路径
$releasePath = "$projectPath\Release"
# 开始编译
& $creatorPath --project . --build "configPath=$buildConfig" | Out-Host
# 使用msbuild去生成exe文件
MSBuild.exe "$projectPath$gameName.sln" -p:Configuration=Release | Out-Host
# 判断文件是否存在
$Exist = (Test-Path $zipPath)
if ($Exist -eq "True")
{
    Remove-Item $zipPath
}
$targetPath = ".${gameName}_${douyinVersion}"
# 先复制打包的结果出来,重名为${gameName}_${douyinVersion}
Copy-Item -Path $releasePath -Destination $targetPath -Recurse;
# 压缩文件夹
Compress-Archive -Path $targetPath -DestinationPath $zipFileName
# 删除临时文件夹
Remove-Item $targetPath -Recurse

4、生成Cocos Creator命令行构建的配置

在Cocos Creator构建窗口修改好抖音打包配置的然后导出,存放在项目的根目录并且命名为douyin.json,如下所示:

运行测试

在powershell环境下运行build.ps1即可生成抖音小玩法包体文件

复制代码
.\build.ps1

如果你使用的是vscode,也可以将默认的终端替换为powershell,方便运行脚本,操作方式是:

Ctrl + P 打开命令面板,输入 Select Default 回车。

选择下面的菜单切换为Powershell即可,重新打开终端时默认已经是Powershell了。

相关推荐
LcGero18 天前
TypeScript 快速上手:泛型与工具类型
typescript·cocos creator·游戏开发
LcGero18 天前
Cocos Creator 3.x 高维护性打字机对话系统设计与实现
cocos creator·打字机
LcGero19 天前
Cocos Creator 三端接入穿山甲 SDK
sdk·cocos creator·穿山甲
LcGero20 天前
Cocos Creator平台适配层框架设计
cocos creator·平台·框架设计
LcGero21 天前
Cocos Creator 业务与原生通信详解
android·ios·cocos creator·游戏开发·jsb
LcGero22 天前
TypeScript 快速上手:前言
typescript·cocos creator·游戏开发
Setsuna_F_Seiei22 天前
CocosCreator 游戏开发 - 多维度状态机架构设计与实现
前端·cocos creator·游戏开发
CodeCaptain3 个月前
cocoscreator 2.4.x 场景运行时的JS生命周期浅析
cocos creator·开发经验
CodeCaptain4 个月前
CocosCreator 3.8.x [.gitignore]文件内容,仅供参考
经验分享·cocos creator
VaJoy5 个月前
Cocos Creator Shader 入门 (21) —— 高斯模糊的高性能实现
前端·cocos creator