实验性 YouTube 游戏大本营 Godot 封装容器
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
借助此封装容器,您可以使用提供的 .gd
在 Godot 中访问 YouTube Playables SDK。封装容器包含一个 .js SDK 连接器和一个 .gd
文件,可帮助您加快开发速度。我们还提供了一个示例项目,展示了如何在项目中使用这些脚本。
您可以从我们的游戏大本营示例代码库下载 Godot ZIP 软件包。
用法
- 在您希望这些脚本所在的项目文件夹中解压缩
GoogleYTGameWrapper-godot.zip
。此 ZIP 文件包含两个文件:
YTGameWrapper.gd
:用作 YT Playables Javascript SDK 的桥梁,可提供从游戏中调用 SDK 的路径。
YTGameSDK.js
:用于连接到 YT Playables SDK。在本例中,我们将此文件放在项目的根目录中。您可以更改此设置,但需要更新您的首要包含位置。
- 您还可以从我们的游戏大本营示例代码库下载完整的项目示例版本。对于此版本,请使用名为
GoogleYTGameWrapper-godot-with-sample.zip
的 ZIP 文件。
- 为解压缩的
YTGameWrapper.gd
脚本添加了新的 Autoload Global。
您可以在 Project
-> Project Settings...
-> Globals
下进行设置。
在 Path
输入框中选择 YTGameWrapper.gd
文件,并在 Node Name
输入框中添加 YTGameWrapper
。最后,选择 + Add
。
- 这样,脚本就会在代码中全局可用,您可以根据需要调用
YTGameWrapper
。
- 验证您的 Godot 项目平台是否已设置为针对
Web
导出。此设置位于“导出”标签页中,依次选择 Project
和 Export...
。
- 在“导出”面板中,依次选择
Add...
和 Web
以添加新的预设。创建后,将以下代码添加到 Head Include
设置框中。
<script src="https://www.youtube.com/game_api/v1">// Load YT Game API code</script>
<script src="YTGameSDK.js"></script>
- 在
Export
面板中,依次选择 Export Project...
和 build 位置,以导出 Web 项目。
- 项目构建完成后,请前往 build 文件夹并将
YTGameSDK.js
文件复制到此文件夹中,并确保在提交 build 时将其包含在内。
- 请验证您是否遵循了项目的技术要求,并务必使用 Playables 测试套件。
示例
本部分提供了一些有关如何使用 .gd
封装容器的示例,但并未列出所有可用 API。如需查看可用 API 的完整列表,请参阅 YouTube Playables SDK。这还假定您的项目的设置方式与“使用”部分中所述的一致。
sendScore
以下示例展示了 .gd
中的 sendScore(int points)
实现:
var current_game_score: int = 0
...
func add_score(scoreAdded: int):
current_game_score += scoreAdded
YTGameWrapper.send_score(current_game_score)
onPause
以下示例展示了游戏如何监听来自 YT Playables 的 Pause
事件,以便在需要时暂停其引擎:
func _ready() -> void:
# hook up on game paused
YTGameWrapper.game_paused.connect(self._on_game_paused)
# hook up on game resumed and game save data received events
YTGameWrapper.game_resumed.connect(self._on_game_resumed)
func _on_game_paused():
# Pause your game logic, audio, etc.
get_tree().paused = true
func _on_game_resumed():
# Resume your game logic, audio, etc.
get_tree().paused = false
saveData
以下示例展示了如何使用 saveData
并将其发送到 YT Playables SDK:
var current_game_score: int = 0
var time_elapsed: float = 0.0
...
func save_game_data():
YTGameWrapper.save_data('{"BestScore": "' + str(current_game_score) + '","BestTime": "' +str(time_elapsed)+ '"}')
loadData
以下示例展示了如何使用 loadData
并将其发送到 YT Playables SDK:
func _ready() -> void:
# hook up game load data received event
YTGameWrapper.load_data_received.connect(self._on_load_data)
# Request to load data when the game starts
YTGameWrapper.load_data()
func _on_load_data(save_data_string: String):
if not save_data_string.is_empty():
# Add logic to parse the save_data_string and restore game state
print("Loaded data: ", save_data_string)
requestInterstitialAd
以下示例展示了如何使用 requestInterstitialAd
,表示当前是展示插页式广告(如果有)的好时机。为获得最佳效果,请在游戏休息时(例如关卡结束时)进行此调用。
func set_game_over(didWin: bool):
YTGameWrapper.request_interstitial_ad()
# logic for game over
如何在 Godot 项目中拆分资源(延迟加载)
开发者在使用 Godot 时强调的一个主要问题是,要确保各个文件大小要求和总软件包大小要求不超标。
延迟加载资源是一项非常有用的优化,因为您可以根据需要加载资源、关卡和数据。如果您能正确执行此操作,我们的认证团队可能会豁免总文件大小限制,因为您的完整游戏不会预加载,而是在用户浏览您的产品时加载。
为帮助正确加载,Godot 提供了“导出资源包、补丁和模组”功能(英语链接)。我们建议生成可根据需要加载的 .pck
文件。
请参阅完整的 YT Playables API 参考文档。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-29。
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["没有我需要的信息","missingTheInformationINeed","thumb-down"],["太复杂/步骤太多","tooComplicatedTooManySteps","thumb-down"],["内容需要更新","outOfDate","thumb-down"],["翻译问题","translationIssue","thumb-down"],["示例/代码问题","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-08-29。"],[],[],null,["# Experimental YouTube Playables Godot Wrapper\n\nWith this wrapper you can access the [YouTube Playables SDK](/youtube/gaming/playables/reference/sdk) in Godot\nwith provided `.gd`. The wrapper has a .js SDK connector and a `.gd` file to\nhelp speed up your development. There is also a sample project available that\nshows how you can use these scripts in your project.\n\nGodot zip Packages can be downloaded from our Playables [sample repo](/youtube/gaming/playables/samples/oss_samples).\n\nUsage\n-----\n\n1. Extract `GoogleYTGameWrapper-godot.zip` in your project folder, where you want these scripts to reside. This zip contains two files:\n - `YTGameWrapper.gd`: used as a bridge to the YT Playables Javascript SDK allowing a path to call the SDK from in game.\n - `YTGameSDK.js`: used to connect to the YT Playables SDK. For this example we will place this file in the root of your project. You can change this, but it requires updating your Head Include location.\n - There is also a full project sample version available for download from our Playables [sample repo](/youtube/gaming/playables/samples/oss_samples). For this version use the zip named `GoogleYTGameWrapper-godot-with-sample.zip`.\n2. Add a new Autoload Global for `YTGameWrapper.gd` script that was unzipped. This can be set under `Project` -\\\u003e `Project Settings...` -\\\u003e `Globals`. Select the `YTGameWrapper.gd` file in the `Path` input box and add `YTGameWrapper` in for the `Node Name` input box. Finally, select `+ Add`.\n - This will make the script globally available in your code, allowing you to call `YTGameWrapper` as needed.\n3. Verify your Godot project Platform is set to export for `Web`. This setting is found in the Export tab, select `Project` then `Export...`.\n4. In the Export panel add a new Preset by selecting `Add...` then select `Web`. Once created, add this code into the `Head Include` setting box.\n\n \u003cscript src=\"https://www.youtube.com/game_api/v1\"\u003e// Load YT Game API code\u003c/script\u003e\n \u003cscript src=\"YTGameSDK.js\"\u003e\u003c/script\u003e\n\n1. Export your web project in the `Export` panel by selecting `Export Project...` and selecting the build location.\n2. Once your project is built, navigate to the build folder and copy the `YTGameSDK.js` file into this folder and make sure to include it when you submit your build.\n3. Verify you are following [technical requirements](/youtube/gaming/playables/certification/requirements_integration) for your project and be sure to use the Playables [test suite](/youtube/gaming/playables/test_suite).\n\nExamples\n--------\n\nThis section has some examples of how to use the `.gd` wrapper, it is not the\nfull list of available APIs. For the full list of available APIs, refer to the\n[YouTube Playables SDK](/youtube/gaming/playables/reference/sdk). This also assumes that your project is setup\nas described in the Usage section.\n\n### `sendScore`\n\nThis example shows an implementation of `sendScore(int points)` in `.gd`: \n\n var current_game_score: int = 0\n\n ...\n\n func add_score(scoreAdded: int):\n current_game_score += scoreAdded\n YTGameWrapper.send_score(current_game_score)\n\n### `onPause`\n\nThis is an example of how a game can listen to `Pause` events coming from YT\nPlayables, to pause its engine when needed: \n\n func _ready() -\u003e void:\n # hook up on game paused\n YTGameWrapper.game_paused.connect(self._on_game_paused)\n # hook up on game resumed and game save data received events\n YTGameWrapper.game_resumed.connect(self._on_game_resumed)\n\n func _on_game_paused():\n # Pause your game logic, audio, etc.\n get_tree().paused = true\n \n func _on_game_resumed():\n # Resume your game logic, audio, etc.\n get_tree().paused = false\n\n### `saveData`\n\nThis is an example of how to use `saveData`, sending it to YT Playables SDK: \n\n var current_game_score: int = 0\n var time_elapsed: float = 0.0\n\n ...\n\n func save_game_data():\n YTGameWrapper.save_data('{\"BestScore\": \"' + str(current_game_score) + '\",\"BestTime\": \"' +str(time_elapsed)+ '\"}')\n\n### `loadData`\n\nThis is an example of how to use `loadData`, sending it to YT Playables SDK: \n\n func _ready() -\u003e void:\n # hook up game load data received event\n YTGameWrapper.load_data_received.connect(self._on_load_data)\n # Request to load data when the game starts\n YTGameWrapper.load_data()\n\n func _on_load_data(save_data_string: String):\n if not save_data_string.is_empty():\n # Add logic to parse the save_data_string and restore game state\n print(\"Loaded data: \", save_data_string)\n\n### `requestInterstitialAd`\n\nThis is an example of how to use `requestInterstitialAd`, indicating it is a\ngood time to show an interstitial Ad, if available. For the best results, make\nthis call during a break in gameplay, for example, at the end of a level. \n\n func set_game_over(didWin: bool):\n YTGameWrapper.request_interstitial_ad()\n\n # logic for game over\n\nHow to break up assets in your Godot project (Lazy Loading)\n-----------------------------------------------------------\n\nOne of the main problems developers have highlighted when using Godot is staying\nunder the individual [file size](/youtube/gaming/playables/certification/requirements_stability) requirements and the\ntotal [bundle size](/youtube/gaming/playables/certification/requirements_stability) requirements.\n\nLazy loading of assets is a great optimization you can make for your project as\nyou can load assets, levels, and data as they are needed. Our certification team\nmay waive the overall file size restrictions if this is properly done, as your\nfull game won't be loaded up front, but as a user navigates your product.\n\nTo help with proper loading, Godot offers Exporting packs, patches, and mods\n([English link](https://docs.godotengine.org/en/stable/tutorials/export/exporting_pcks.html)). We suggest generating `.pck` files that are loaded\nas needed.\n\nSee the full [YT Playables API reference](/youtube/gaming/playables/reference/sdk#ytgame)."]]