Cocos Creator 3.x
已用真正的 Cocos Creator 3.4.2 web-mobile 建置驗證。 將 Gamealia 套件加入該建置後,通過了自動測試的 SDK 檢查(初始化、載入、遊戲開始/停止、語言)。尚未驗證:由安裝了套件的 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(支援)。