【Unity】 ios遊戲開發中登陸 GameCenter的問題和腳本

想通過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
usingUnityEngine;
usingUnityEngine.SocialPlatforms;
usingUnityEngine.SocialPlatforms.GameCenter;
 
publicclassGameCenterManager : System.Object
{
    privatestaticGameCenterManager instance;
    privatestaticobject _lock=newobject();
    privateGameCenterManager(){}
    publicstaticGameCenterManager GetInstance()
    {
        if(instance==null)
        {
            lock(_lock)
            {
                if(instance==null)
                {
                    instance=newGameCenterManager();
                }
            }
        }
        returninstance;
    }
     
    publicvoidStart()
    {
        Social.localUser.Authenticate(HandleAuthenticated);
    }
     
    privatevoidHandleAuthenticated(boolsuccess)
    {
        Debug.Log("*** HandleAuthenticated: success = " + success);
        if(success)
        {
            stringuserInfo = "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);
        }
    }
     
    privatevoidHandleFriendsLoaded(boolsuccess)
    {
        Debug.Log("*** HandleFriendsLoaded: success = " + success);
        foreach(IUserProfile friend inSocial.localUser.friends)
        {
            Debug.Log("* friend = " + friend.ToString());
        }
    }
     
    privatevoidHandleAchievementsLoaded(IAchievement[] achievements)
    {
        Debug.Log("* HandleAchievementsLoaded");
        foreach(IAchievement achievement inachievements)
        {
            Debug.Log("* achievement = " + achievement.ToString());
        }
    }
     
    privatevoidHandleAchievementDescriptionsLoaded(IAchievementDescription[] achievementDescriptions)
    {
        Debug.Log("*** HandleAchievementDescriptionsLoaded");
        foreach(IAchievementDescription achievementDescription inachievementDescriptions)
        {
            Debug.Log("* achievementDescription = " + achievementDescription.ToString());
        }
    }
     
    // achievements
     
    publicvoidReportProgress(stringachievementId,doubleprogress)
    {
        if(Social.localUser.authenticated) {
            Social.ReportProgress(achievementId, progress, HandleProgressReported);
        }
    }
     
    privatevoidHandleProgressReported(boolsuccess)
    {
        Debug.Log("*** HandleProgressReported: success = " + success);
    }
     
    publicvoidShowAchievements()
    {
        if(Social.localUser.authenticated) {
            Social.ShowAchievementsUI();
        }
    }
     
    // leaderboard
     
    publicvoidReportScore(stringleaderboardId,longscore)
    {
        if(Social.localUser.authenticated) {
            Social.ReportScore(score, leaderboardId, HandleScoreReported);
        }
    }
     
    publicvoidHandleScoreReported(boolsuccess)
    {
        Debug.Log("*** HandleScoreReported: success = " + success);
    }
     
    publicvoidShowLeaderboard()
    {
        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.
  
  相信有了這前車之鑑,大家的開發過程會變的順暢很多。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章