Daniel
Daniel

Reputation: 21

Can't call GameCenter in my Unity game for iOS

I'm trying to integrate Game Center into my Unity 5 project and facings troubles. It seems that authentification is OK when the game starts on iOS device, a notification appears, but leaderboard button does not work, nothing happens when I tap GC button. I assigned the initiation of "ShowLeaderboard()" to GC button. Please, take a look at the script I use below. Hope to find a resolution for my issue. Thank you in advance.

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

Upvotes: 2

Views: 3928

Answers (2)

Hamza Hasan
Hamza Hasan

Reputation: 1359

Actually now you even don't have to get GameCenterPlatforms. You can simply use Social. Like.

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

Upvotes: 0

Jay Nebhwani
Jay Nebhwani

Reputation: 976

To show the iOS Game Centre Leaderboard you need to use the GameCenterPlatform.

Your code would then be:

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

Upvotes: 5

Related Questions