GameKitUI 由 Sascha Muellner 用心 ❥ 创建并维护。
这是一个支持 iOS 的 Swift 包,允许您在 SwiftUI 中使用 GameKit。
最新版本的 GameKitUI 需要
使用 SPM 将以下内容添加到您的依赖项中
'GameKitUI', 'main', 'https://github.com/SwiftPackageRepository/GameKitUI.swift.git'
要使用 GameCenter 验证玩家身份,只需显示身份验证视图 GKAuthenticationView。
import SwiftUI
import GameKitUI
struct ContentView: View {
var body: some View {
GKAuthenticationView { (state) in
switch state {
case .started:
print("Authentication Started")
break
case .failed:
print("Failed")
break
case .deauthenticated:
print("Deauthenticated")
break
case .succeeded:
break
}
} failed: { (error) in
print("Failed: \(error.localizedDescription)")
} authenticated: { (playerName) in
print("Hello \(playerName)")
}
}
}
可以使用 GKMatchMakerView 处理由 GameKit MatchMaker 或 TurnBasedMatchmaker 创建的邀请。
import SwiftUI
import GameKitUI
struct ContentView: View {
var body: some View {
GKInviteView(
invite: GKInvite()
) {
} failed: { (error) in
print("Invitation Failed: \(error.localizedDescription)")
} started: { (match) in
print("Match Started")
}
}
}
可以通过 GKMatchMakerView 启动实时对战的匹配。
import SwiftUI
import GameKitUI
struct ContentView: View {
var body: some View {
GKMatchMakerView(
minPlayers: 2,
maxPlayers: 4,
inviteMessage: "Let us play together!"
) {
print("Player Canceled")
} failed: { (error) in
print("Match Making Failed: \(error.localizedDescription)")
} started: { (match) in
print("Match Started")
}
}
}
要开始一个回合制对战,请使用 GKTurnBasedMatchmakerView。
import SwiftUI
import GameKitUI
struct ContentView: View {
var body: some View {
GKTurnBasedMatchmakerView(
minPlayers: 2,
maxPlayers: 4,
inviteMessage: "Let us play together!"
) {
print("Player Canceled")
} failed: { (error) in
print("Match Making Failed: \(error.localizedDescription)")
} started: { (match) in
print("Match Started")
}
}
}
GameKitUI 视图依赖于一个管理器单例 GKMatchManager,它监听对战匹配过程中 GameKit 状态的变化。可以使用提供的公共 subjects CurrentValueSubject 观察本地玩家 GKLocalPlayer、邀请 GKInvite 或比赛 GKMatch 的更改。
import SwiftUI
import GameKitUI
class ViewModel: ObservableObject {
@Published public var gkInvite: GKInvite?
@Published public var gkMatch: GKMatch?
private var cancellableInvite: AnyCancellable?
private var cancellableMatch: AnyCancellable?
private var cancellableLocalPlayer: AnyCancellable?
public init() {
self.cancellableInvite = GKMatchManager
.shared
.invite
.sink { (invite) in
self.gkInvite = invite.gkInvite
}
self.cancellableMatch = GKMatchManager
.shared
.match
.sink { (match) in
self.gkMatch = match.gkMatch
}
self.cancellableLocalPlayer = GKMatchManager
.shared
.localPlayer
.sink { (localPlayer) in
// current GKLocalPlayer.local
}
}
deinit() {
self.cancellableInvite?.cancel()
self.cancellableMatch?.cancel()
self.cancellableLocalPlayer?.cancel()
}
}
提供的 GKMatchMaker 示例包含一个完整的 SwiftUI 解决方案,用于处理 GameKit 对战匹配。 只需将文件 Config.xcconfig-example 复制到 Config.xcconfig 并为变量 XCCONFIG_DEVELOPMENT_TEAM 添加您的开发团队 ID,并为 XCCONFIG_BUNDLE_ID 添加具有 GameCenter 支持的有效 Bundle ID。 Config.xcconfig 现在应该看起来像这样
// Configuration settings file format documentation can be found at:
// https://help.apple.com/xcode/#/dev745c5c974
XCCONFIG_DEVELOPMENT_TEAM = 9988XX7D42 // YOUR DEVELOPMENT TEAM ID
XCCONFIG_BUNDLE_ID = com.yourcompany.ProductName // A BUNDLE ID WITH SUPPORT FOR THE GAMECENTER CAPABILITY
然后打开 GKMatchMaker.xcodeproj 并在尽可能多的 真实硬件 设备上运行它,以测试 GameKit 对战匹配。