Quantcast
Channel: CSDN博客移动开发推荐文章
Viewing all articles
Browse latest Browse all 5930

【Unity】 ios游戏开发中登陆 GameCenter的问题和脚本

$
0
0
想通过Unity 登录ios GameCenter,需要借助unity自带的Social.localUser方法调用来实现,鉴于有些开发者没有过这方面的经验,下面就给大家介绍下成功登陆进ios GameCenter的方法,想看的可以学习一下。
  
  下面是代码实现:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
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();
        }
    }
    
  
  当然在登陆过程中也出现过一些列问题,例如说在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.
  
  相信有了这前车之鉴,大家的开发过程会变的顺畅很多。
作者:swj524152416 发表于2017/2/16 17:33:01 原文链接
阅读:2 评论:0 查看评论

Viewing all articles
Browse latest Browse all 5930

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>