【问题标题】:Can't call GameCenter in my Unity game for iOS无法在我的 iOS 版 Unity 游戏中调用 GameCenter
【发布时间】:2016-01-27 08:31:10
【问题描述】:

我正在尝试将 Game Center 集成到我的 Unity 5 项目中,但遇到了麻烦。在iOS设备上启动游戏时似乎验证正常,出现通知,但排行榜按钮不起作用,当我点击GC按钮时没有任何反应。我将“ShowLeaderboard()”的启动分配给 GC 按钮。请看一下我在下面使用的脚本。希望为我的问题找到解决方案。提前谢谢你。

using UnityEngine;
using UnityEngine.SocialPlatforms;
using UnityEngine.SocialPlatforms.GameCenter;
using System.Collections;

public class GameCenter : MonoBehaviour {

    public string leaderboardID = "mygameleaderboard";

    void Start () {
        AuthenticateToGameCenter();
    }

    private bool isAuthenticatedToGameCenter;
    public static void  AuthenticateToGameCenter() {
        #if UNITY_IPHONE
        Social.localUser.Authenticate(success => {
        if (success) {
            Debug.Log("Authentication successful");
        } else {
            Debug.Log("Authentication failed");
            }
        });
        #endif
    }

    public static void ReportScore(long score, string leaderboardID) {
        #if UNITY_IPHONE
        Debug.Log("Reporting score " + score + " on leaderboard " + leaderboardID);
        Social.ReportScore(score, leaderboardID, success => {
            if (success) {
            Debug.Log("Reported score successfully");
            } else {
                Debug.Log("Failed to report score");
                }
            });
        #endif
    }

    //call to show leaderboard
    public static void ShowLeaderboard() {
        #if UNITY_IPHONE
        Social.ShowLeaderboardUI();
        #endif
    }
}

【问题讨论】:

  • 我个人使用 Prime31 插件专门用于 Unity 和 GameCenter 集成。它通过漂亮的界面整齐地放置社交平台的所有内容和摘要。 prime31.com

标签: c# unity3d game-engine unity5 game-center-leaderboard


【解决方案1】:

要显示 iOS Game Center 排行榜,您需要使用 GameCenterPlatform

您的代码将是:

void ShowLeaderboard() {
    #if UNITY_IOS
    GameCenterPlatform.ShowLeaderboardUI(leaderboardId, UnityEngine.SocialPlatforms.TimeScope.AllTime);
    #endif
}

【讨论】:

  • 同意,GameCenterPlatform 是最新最好的。社交在 Unity 4 中就像一个魅力,但在 5 中,他们正在推动人们使用这个 API。不过,我仍然使用适用于 iOS 的 Google Play 游戏进行社交。它真的很好用,而且很容易使用
【解决方案2】:

实际上,现在您甚至不必获得 GameCenterPlatforms。您可以简单地使用Social。喜欢。

public void OnLeaderboard()
{
    // Checking if user already authenticated or not.
    if (Social.localUser.authenticated)
                Social.ShowAchievementsUI ();
            else
                // If not then authenticate first then show Leaderboard
                Social.localUser.Authenticate ((bool success) => {
                    // handle success or failure
                    if (success)
                        Social.ShowAchievementsUI ();
                });
        }
}

【讨论】:

    猜你喜欢
    • 2015-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-31
    • 2011-08-12
    • 1970-01-01
    相关资源
    最近更新 更多