Unity 登陸Ios GameCenter

我是使用unity自帶的Social.localUser方法調用的,其中也遇到一些問題,好比:在ios設備上沒有反映,剛開始感受這個腳本出問題。而後使用第三方插件,也是不能登陸, 查看源碼,打印錯誤日誌。ios

最後才發現必須開啓gameCenter的沙盒模式 Sandbox in Settings--> Game Center --> Sandbox.   錯誤提示爲:c#

 Domain=GKErrorDomain Code=15 "The requested operation could not be completed because this application is not recognized by Game Center." UserInfo=0x17d08ee0 {NSLocalizedDescription=The requested operation could not be completed because this application is not recognized by Game Center.app


大功告成把Unity腳本也和你們分享下:this

using UnityEngine;
using UnityEngine.SocialPlatforms;
using UnityEngine.SocialPlatforms.GameCenter;

public class GameCenterManager : System.Object 
{
    private static GameCenterManager instance;
    private static object _lock=new object();
    private GameCenterManager(){}
    public static GameCenterManager GetInstance()
    {
        if(instance==null)
        {
            lock(_lock)
            {
                if(instance==null)
                {
                    instance=new GameCenterManager();
                }
            }
        }
        return instance;
    }
    
    public void Start()
    {
        Social.localUser.Authenticate(HandleAuthenticated);
    }
    
    private void HandleAuthenticated(bool success)
    {
        Debug.Log("*** HandleAuthenticated: success = " + success);
        if(success)
        {
            string userInfo = "UserName:" + Social.localUser.userName +"\nUser ID:"+
                Social.localUser.id + " \nIsUnderage: "+ Social.localUser.underage;
            Debug.Log(userInfo);

            //下面三行看我的須要,須要什麼信息就取什麼信息,這裏註釋掉是由於擔憂有的朋友沒有在iTunesConnect裏設置排行、成就之類的東西,運行起來可能會報錯
        //    Social.localUser.LoadFriends(HandleFriendsLoaded);
        //    Social.LoadAchievements(HandleAchievementsLoaded);
        //    Social.LoadAchievementDescriptions(HandleAchievementDescriptionsLoaded);
        }
    }
    
    private void HandleFriendsLoaded(bool success)
    {
        Debug.Log("*** HandleFriendsLoaded: success = " + success);
        foreach(IUserProfile friend in Social.localUser.friends)
        {
            Debug.Log("* friend = " + friend.ToString());
        }
    }
    
    private void HandleAchievementsLoaded(IAchievement[] achievements)
    {
        Debug.Log("* HandleAchievementsLoaded");
        foreach(IAchievement achievement in achievements)
        {
            Debug.Log("* achievement = " + achievement.ToString());
        }
    }
    
    private void HandleAchievementDescriptionsLoaded(IAchievementDescription[] achievementDescriptions)
    {
        Debug.Log("*** HandleAchievementDescriptionsLoaded");
        foreach(IAchievementDescription achievementDescription in achievementDescriptions)
        {
            Debug.Log("* achievementDescription = " + achievementDescription.ToString());
        }
    }
    
    // achievements
    
    public void ReportProgress(string achievementId, double progress)
    {
        if (Social.localUser.authenticated) {
            Social.ReportProgress(achievementId, progress, HandleProgressReported);
        }
    }
    
    private void HandleProgressReported(bool success)
    {
        Debug.Log("*** HandleProgressReported: success = " + success);
    }
    
    public void ShowAchievements()
    {
        if (Social.localUser.authenticated) {
            Social.ShowAchievementsUI();
        }
    }
    
    // leaderboard
    
    public void ReportScore(string leaderboardId, long score)
    {
        if (Social.localUser.authenticated) {
            Social.ReportScore(score, leaderboardId, HandleScoreReported);
        }
    }
    
    public void HandleScoreReported(bool success)
    {
        Debug.Log("*** HandleScoreReported: success = " + success);
    }
    
    public void ShowLeaderboard()
    {
        if (Social.localUser.authenticated) {
            Social.ShowLeaderboardUI();
        }
    }
    
}
相關文章
相關標籤/搜索