某些模拟器上的GKLocalPlayer.LocalPlayer为null

时间:2016-01-28 10:55:36

标签: c# ios xamarin game-center game-center-leaderboard

我在将Game Center与我的Monogame游戏集成时遇到了这个问题。

我有什么:

void InitGameCenter()
    {
        gameCenterManager = new GameCenterManager();
        SetAuthenticateHandler();
    }

    void SetAuthenticateHandler()
    {
        GKLocalPlayer.LocalPlayer.AuthenticateHandler = (ui, error) => {
            if (ui != null)
            {
                controller.PresentViewController(ui, true, null);
            }
            else if (GKLocalPlayer.LocalPlayer.Authenticated)
            {
                currentLeaderBoard = gameCenterManager.ReloadLeaderboard(leaderboardId);
            }
            else {
                var alert = new UIAlertView("Game Center Account Required", "Need login the game center!", null, "Retry", null);
                alert.Clicked += (sender, e) => {
                    //GKLocalPlayer.LocalPlayer.Authenticated();
                };
                alert.Show();
            }
        };
    }

在某些模拟器上,它停止在SetAuthenticateHandler()方法的第一行代码中抛出异常,因为GKLocalPlayer.LocalPlayer为空

工作地点:

iPad 2(9.2)设备 iPhone的每个模拟器都比5更新(iPhone 5s,6,6s,无论iOS版本)。

它不起作用的地方:每台iPhone模拟器老款5s(iPhone 5,4s)

我向你展示的方法是在AppDelegate的FinishedLaunching()方法中,因为Monogame中没有ViewController.cs。

任何线索?

2 个答案:

答案 0 :(得分:0)

可能是签名问题。看看class details。签名在iOS 6上发生了变化。

来自文档:

//
// This shows how to authenticate on both iOS 6.0 and older versions
//
if (UIDevice.CurrentDevice.CheckSystemVersion (6, 0)) {
    //
    // iOS 6.0 and newer
    //
    GKLocalPlayer.LocalPlayer.AuthenticateHandler = (ui, error) => {

        // If ui is null, that means the user is already authenticated,
    // for example, if the user used Game Center directly to log in

    if (ui != null)
            current.PresentModalViewController (ui, true);
    else {
        // Check if you are authenticated:
        var authenticated = GKLocalPlayer.LocalPlayer.Authenticated;
    }
    Console.WriteLine ("Authentication result: {0}",err);
    };
} else {
    // Versions prior to iOS 6.0
    GKLocalPlayer.LocalPlayer.Authenticate ((err) => {
        Console.WriteLine ("Authentication result: {0}",err);
    });
};

答案 1 :(得分:0)

好的,所以如果有人想在未来将游戏中心与Monogame集成,那么就有一个非常简单的解决方案(内置Monogame):

// Define gamer variable
SignedInGamer gamer;

// Launch Game Center authentication
gamer = new SignedInGamer();

// Later on, interact with Game Center in various ways
gamer.UpdateScore(category, score);
Guide.ShowLeaderboard();
Guide.ShowAchievements();

通过picobots @ Monogame社区论坛(link