How to display GameCenter leaderboard on tvOS?

2019-07-15 08:04发布

问题:

I think I followed all the required steps to support leaderboards in my game (and they work just fine on iOS), however on tvOS it is not possible to configure the GKGameCenterViewController to show a specific leaderboard, the LeaderboardIdentifier property is simply missing (just like the ViewState):

var leaderboardController = new GKGameCenterViewController ();

// Unavailable on tvOS
/*
leaderboardController.ViewState = GKGameCenterViewControllerState.Default;
leaderboardController.LeaderboardIdentifier = "myLeaderboardId";
*/ 

leaderboardController.Finished += (sender, e) =>
{
  leaderboardController.DismissViewController (true, null);
}

PresentViewController (leaderboardController, true, null);

Instead of using these properties, I followed the instructions here. I notice that this will generate a GKGameCenterContent.plist file in the final app bundle. I double checked the content:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>GKLeaderboards</key>
    <array>
        <dict>
            <key>identifier</key>
            <string>myLeaderboardId</string>
            <key>onDemandResourcesTag</key>
            <string>com.apple.gamecenter.Leaderboard</string>
            <key>posterImageName</key>
            <string>Leaderboard/Poster</string>
        </dict>
    </array>
</dict>
</plist>

This looks absolutely correct, also the images are of course in the bundle. Still, using the code above to show the game center controller will only give me the achievements screen and nothing else.

回答1:

You have to add a leaderboard to Assets.xassets. There you can enter the identifier:

The code to show the leaderboard:

@IBAction func openLeaderboard(sender: AnyObject) {

    let gcViewController = GKGameCenterViewController()
    gcViewController.gameCenterDelegate = self
    self.presentViewController(gcViewController, animated: true, completion: nil)
}