How to display GameCenter leaderboard on tvOS?

梦想与她 提交于 2020-01-14 14:27:08

问题


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)
}


来源:https://stackoverflow.com/questions/34097474/how-to-display-gamecenter-leaderboard-on-tvos

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!