Unity 登录Ios GameCenter

我是使用unity自带的Social.localUser方法调用的,其中也遇到一些问题,比如:在ios设备上没有反映,刚开始感觉这个脚本出问题。然后使用第三方插件,也是不能登录, 查看源码,打印错误日志。

最后才发现必须开启gameCenter的沙盒模式 Sandbox in Settings--> Game Center --> Sandbox.   错误提示为:

 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.


大功告成把Unity脚本也和大家分享下:

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



转载于:https://my.oschina.net/u/698044/blog/503745

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值