Cocos Creator 3.x
已用真实的 Cocos Creator 3.4.2 web-mobile 构建验证。 加入该构建的 Gamealia 包通过了自动测试的 SDK 检查(init、加载、游戏开始/结束、语言)。尚未验证:安装了该包后由 Cocos Creator 编辑器生成的构建,以及 Cocos Creator 3.6 或更新版本。如果有地方不工作,请在控制台的“支持”聊天中或通过 hello@gamealia.app 告诉我们:附上你的 Cocos Creator 版本、构建平台(Web Mobile 或 Web Desktop),以及你预期会发生什么。
Gamealia 包是供 Cocos Creator 3.x 使用的两个 TypeScript 文件:Gamealia.ts,一个基于 JavaScript SDK 的带类型、基于 Promise 的封装层(它会自己加载 SDK,因此不需要构建模板);以及 GamealiaBootstrap,一个组件,负责启动 SDK、调用 loadingStop(),并在门户要求时暂停游戏循环、将所有 AudioSource 静音。
安装
- 在你的 Cocos 项目文件夹中运行
gamealia init(或让你的 AI 助手通过 MCP 服务器运行它):它会将包复制到assets/gamealia/。也可以下载 gamealia-cocos.zip 并解压到项目文件夹中(其中包含assets/gamealia/)。Cocos Creator 会导入这些文件并写入它们的.meta。 - 在你的第一个场景中,在场景根部添加一个空节点(例如 "Gamealia"),并为它添加 GamealiaBootstrap 组件。该节点会在场景之间保留。
最小流程
import { _decorator, Component } from "cc";
import { Gamealia } from "./gamealia/Gamealia"; // path relative to this script
const { ccclass } = _decorator;
@ccclass("Game")
export class Game extends Component {
async start() {
const info = await Gamealia.init(); // same result GamealiaBootstrap got
this.setLanguage(info.locale); // "en", "es", "pt-BR", ...: use your closest language
Gamealia.events.on("localeChange", (locale: string) => this.setLanguage(locale));
Gamealia.game.gameplayStart();
}
async onLevelEnd() {
Gamealia.game.gameplayStop();
await Gamealia.ad.requestMidgame(); // the game is paused and muted while the ad plays
Gamealia.game.gameplayStart();
}
async onWatchAdForCoins() {
Gamealia.game.gameplayStop();
const ad = await Gamealia.ad.requestRewarded();
if (ad.rewarded) this.coins += 10; // only when rewarded is true
Gamealia.game.gameplayStart();
}
setLanguage(locale: string) { /* ... */ }
coins = 0;
}
在门户之外(编辑器预览、你自己的服务器),SDK 以模拟模式运行:广告是模拟的,存档保留在浏览器中,因此游戏可以继续正常运行。
GamealiaBootstrap 属性
| 属性 | 默认值 | 作用 |
|---|---|---|
| Manual Loading Stop | 关 | 开:在游戏可玩时自己调用 Gamealia.game.loadingStop()。 |
| Mute Audio | 开 | 在门户将游戏静音时(广告、玩家的静音按钮)将所有 AudioSource 的音量设为 0,之后恢复;静音期间加载的场景会以静音状态开始。 |
| Pause Game | 开 | 门户暂停游戏时调用 game.pause(),之后调用 game.resume()。 |
以其他方式播放的音频(你自己的 WebAudio、video 元素):监听 Gamealia.events.on("mute") 和 on("unmute"),自己将其静音。
调用和事件
每个调用都返回一个 Promise(即发即弃的调用不返回任何内容),名称与 SDK 参考中相同:
| 领域 | 调用 |
|---|---|
| 启动 | Gamealia.init() → { environment, locale, gameId, user, device, deviceClass, country, app, joinRoom, joinKey, joinParams, instantMultiplayer, chatOff, os, browser } |
| 游戏 | game.loadingStart()、game.loadingStop()、game.gameplayStart()、game.gameplayStop()、game.happyTime()、game.setProgress(percent)、game.setContext({ level: 3 })、game.reportError(error)、game.track(category, what, action?) |
| 广告 | ad.requestMidgame()、ad.requestRewarded() → { shown, rewarded, reason }、ad.hasAdblock()、ad.requestBanner(id, size, anchorX, anchorY, posX, posY) 和 ad.requestResponsiveBanner(id, anchorX, anchorY, posX, posY, maxWidth, maxHeight) → { ok, reason, size }(由门户画在游戏之上的游戏内横幅;要等 Gamealia 的广告上线后才会显示,在那之前 reason: "not-live")、ad.clearBanner(id)、ad.clearAllBanners() |
| 玩家账号 | user.get()、user.getToken()、user.requestLogin()、user.onChange(cb) |
| 云存档 | data.load() → { ok, data }、data.get(key)、data.set(key, value)、data.remove(key)、data.clear() |
| 房间 | room.getJoinCode()、room.getJoinKey()、room.getJoinPass(code?)、room.joinFailed(reason, code?)、room.inviteLink(code)、room.showInvite(code)、room.onJoin(cb)、room.publish(room)、room.unpublish() |
| 在线状态和好友 | presence.setStatus(text)、friends.list()、friends.invite(id, code)、friends.onChange(cb) |
| 排行榜 | leaderboard.submit(board, value)、leaderboard.get(board, { scope, season, limit, around })、leaderboard.show(board?) |
Gamealia.events(一个 cc.EventTarget)上的事件:localeChange(locale)、adStart、adEnd、pause、resume、mute、unmute、userChange(user)、roomJoin(code, key)、friendsChange。
仅限 JavaScript 的功能
聊天、商店以及 SDK 参考中的其他附加功能位于原始 SDK 对象 Gamealia.sdk 上(在 init() 之后),没有类型:
const r = await Gamealia.sdk.chat.join("global");
构建
- Project > Build,平台选 Web Mobile(或 Web Desktop),然后点击 Build。
- 上传
build/web-mobile文件夹,index.html位于其根目录(或直接将其打包为 zip)。 - 保留构建自带的文件:
index.html、application.js、src/(settings.json、system.bundle.js、import-map.json)、cocos-js/和assets/。如果你使用物理或其他附带.wasm文件的引擎模块,请确保它们包含在上传内容中。
可选:要在引擎启动之前加载 SDK,请将 <script src="https://gamealia.app/sdk/v1.js"></script> 添加到自定义构建模板(build-templates/web-mobile/index.ejs,基于默认模板制作)的 head 中。没有它时,Gamealia.ts 会在 init() 运行时加载 SDK。
已经在使用其他门户的 Cocos 包?
移除它,并将它的调用改为 Gamealia 的调用:加载完成、游玩开始和停止、插页广告和激励广告、静音。如果它的 SDK 是通过构建模板中的 script 标签加载的,我们的兼容层可以取而代之,如迁移指南中所述。
常见的自动测试失败
gameplay_start/gameplay_stop:没有调用Gamealia.game.gameplayStart()/gameplayStop()。loading_stop:GamealiaBootstrap 不在第一个场景中,或者开启了 Manual Loading Stop 但从未执行到该调用。translations:文本从init().locale设置了一次,却没有Gamealia.events.on("localeChange", ...)。- 页面加载的某个文件缺失:请上传整个
build/web-mobile文件夹,而不是其中一部分。
遇到问题或有疑问:控制台中的“支持”聊天,或 hello@gamealia.app(支持)。