跳转到内容

游戏

把应用标记为游戏,读取游戏手柄,使用 Game Center 和 Play 游戏服务。

在 nsgo.yaml 中设置 game: true 将应用声明为游戏。Android 将应用归为游戏并使用系统的游戏模式优化。macOS 和 iOS 启用游戏模式支持,macOS 的默认分类为游戏,Linux 在桌面入口中加入 Game 分类。显式设置的 macos.category 或 apple.info 中的游戏模式键优先。

应用支持物理手柄时,设置 gamepad: true。Android 将手柄和触摸屏声明为可选。macOS、iOS 和 tvOS 声明 Extended Gamepad 配置,tvOS 还声明 Siri Remote 的 Micro Gamepad 配置,apple.info 中的手柄键优先。

gameservices 包在 cx.Env().CanUseGameServices() 为 true 的平台上提供与服务商无关的接口,用于玩家登录、成就、排行榜以及原生的 Game Center / Play Games 界面。所有回调都在引擎 goroutine 上执行,可直接更新信号而不阻塞渲染帧。

if cx.Env().CanUseGameServices() {
gameservices.Authenticate(cx, func(p gameservices.Player, err error) {
if err == nil && p.Authenticated {
playerName.Set(p.DisplayName)
gameservices.ReportAchievement(cx, "first-run", 100, nil)
gameservices.SubmitScore(cx, "high-scores", 1250, nil)
}
})
}

gameservices.ReportAchievement 将进度限制在 0..100(100 解锁成就)。gameservices.LoadScores 按 Global 或 Friends 以及 AllTime、Week 或 Today 读取带排名的 gameservices.ScoreEntry 列表,gameservices.ShowAchievements 和 gameservices.ShowLeaderboard 打开平台原生界面。macOS、iOS 和 tvOS 通过 purego 调用 GameKit;Android 在应用打包了 com.google.android.gms:play-services-games-v2 时通过反射调用 Google Play Games Services v2(此时 CanUseGameServices() 为 true,部分进度对应 Play Console 中配置的 100 步增量成就,LoadScores 受限于 Play Games 每页最多 25 条),缺少该 SDK 时报告 CanUseGameServices() == false 并返回 gameservices.ErrUnavailable;Windows、Linux、Web、watchOS 和小程序报告 CanUseGameServices() == false 并返回 gameservices.ErrUnsupported。

在 Android 上,请在应用或 Gradle 宿主中加入 com.google.android.gms:play-services-games-v2 依赖,并在 AndroidManifest.xml 中声明 Play Games 项目 ID:

<meta-data
android:name="com.google.android.gms.games.APP_ID"
android:value="@string/game_services_project_id" />

若启用了代码压缩,请保留 com.google.android.gms.games.** 和 com.google.android.gms.tasks.**,以便反射找到 Play Games Services v2 客户端。

测试时,用 nsgotest.Environment(core.Environment{CanUseGameServices: true}) 开启游戏服务,并用 App.SetGameServicesPlayer、App.SetGameServicesFriends、App.SeedLeaderboardScores、App.SetGameServicesError、App.GameServicesAchievements、App.GameServicesScores、App.GameServicesShownAchievements 和 App.GameServicesShownLeaderboards 驱动每个应用隔离的测试替身。