YouTube Playables SDK 是用於將網頁遊戲連結至 YouTube 環境的網頁 SDK。這個 SDK 提供強大的 API,可透過各種方式支援遊戲,在 YouTube 上打造出色的遊戲體驗。
在遊戲中加入 Playables SDK
遊戲的根目錄中應有 index.html
檔案。如要匯入 YouTube Playables SDK,請在任何遊戲程式碼前方加入這行程式碼:
<script src="https://www.youtube.com/game_api/v1"></script>
當遊戲在本機上提供服務時,SDK 會以無操作方式執行。如要驗證 SDK 整合是否正確,請參閱測試套件指南。
與 Playables SDK 整合
您必須整合幾個 Playables SDK 功能,但也有一些功能是可選的。
必要整合
請詳閱整體發布規定,並著重於整合規定。如需實作詳細資訊,請參閱 Playables SDK 參考資料。
例如:
ytgame.game.firstFrameReady()
ytgame.game.gameReady()
ytgame.IN_PLAYABLES_ENV
ytgame.system.isAudioEnabled()
ytgame.system.onAudioEnabledChange((isAudioEnabled) => {})
ytgame.system.onPause(() => {})
ytgame.system.onResume(() => {})
ytgame.game.loadData()
ytgame.game.saveData(data)
建議的整合項目
除了必要的整合功能外,您還可以使用其他幾項功能,打造極具吸引力的體驗。例如:
ytgame.system.getLanguage()
:用於擷取使用者目前的語言代碼設定。請勿使用其他函式或將語言儲存在雲端儲存中,因為這可能隨時變更。ytgame.engagement.sendScore({ value: newScore })
:將最佳分數傳送至 YouTube 以供顯示。ytgame.engagement.openYTContent({ id: videoID })
:開啟 YouTube 影片。ytgame.health.logError()
和ytgame.health.logWarning()
- 將問題記錄到 YouTube。
如需瞭解實作細節和其他函式,請參閱 Playables SDK 參考資料。
TypeScript 類型定義
如果遊戲使用 TypeScript,請下載類型定義。
範例遊戲
提供範例,說明如何整合 YouTube Playables SDK,包括一般 JavaScript、Flutter 網頁、Godot 和 Unity。
使用測試套件測試遊戲
準備就緒後,您可以使用測試套件驗證整合作業。如要瞭解如何操作,請參閱測試套件指南。