Unity接入GameCenter

脚本名称

IOSManager.cs

代码

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
115
116
117
118
119
120
121
122
123
124
125
using UnityEngine;
using System.Collections;
using UnityEngine.SocialPlatforms;
using UnityEngine.SocialPlatforms.GameCenter;

public class IOSManager : MonoBehaviour {

public bool GameCenterState;
public string userInfo;
/// <summary>
/// 初始化 GameCenter 登陆
/// </summary>
void Start () {
Social.localUser.Authenticate(HandleAuthenticated);
}

/// <summary>
/// 初始化 GameCenter 结果回调函数
/// </summary>
/// <param name="success">If set to <c>true</c> success.</param>
private void HandleAuthenticated(bool success)
{
GameCenterState = success;
Debug.Log("*** HandleAuthenticated: success = " + success);
///初始化成功
if (success) {
userInfo = "Username: " + Social.localUser.userName +
"\nUser ID: " + Social.localUser.id +
"\nIsUnderage: " + Social.localUser.underage;
Debug.Log (userInfo);
} else {
///初始化失败

}
}


void OnGUI(){

GUI.TextArea ( new Rect( Screen.width -200, 0, 200, 100), "GameCenter:"+GameCenterState );
GUI.TextArea ( new Rect( Screen.width -200, 100, 200, 100), "userInfo:"+userInfo );

if (GUI.Button (new Rect (0, 0, 110, 75), "打开成就")) {

if (Social.localUser.authenticated) {
Social.ShowAchievementsUI();
}
}

if (GUI.Button (new Rect (0, 150, 110, 75), "打开排行榜")) {

if (Social.localUser.authenticated) {
Social.ShowLeaderboardUI();
}
}

if (GUI.Button (new Rect (0, 300, 110, 75), "排行榜设置分数")) {

if (Social.localUser.authenticated) {
Social.ReportScore(1000, "XXXX", HandleScoreReported);
}
}

if (GUI.Button (new Rect (0, 300, 110, 75), "设置成就")) {

if (Social.localUser.authenticated) {
Social.ReportProgress("XXXX", 15, HandleProgressReported);
}
}

}


//上传排行榜分数
public void HandleScoreReported(bool success)
{
Debug.Log("*** HandleScoreReported: success = " + success);
}
//设置 成就
private void HandleProgressReported(bool success)
{
Debug.Log("*** HandleProgressReported: success = " + success);
}

/// <summary>
/// 加载好友回调
/// </summary>
/// <param name="success">If set to <c>true</c> success.</param>
private void HandleFriendsLoaded(bool success)
{
Debug.Log("*** HandleFriendsLoaded: success = " + success);
foreach(IUserProfile friend in Social.localUser.friends)
{
Debug.Log("* friend = " + friend.ToString());
}
}

/// <summary>
/// 加载成就回调
/// </summary>
/// <param name="achievements">Achievements.</param>
private void HandleAchievementsLoaded(IAchievement[] achievements)
{
Debug.Log("* HandleAchievementsLoaded");
foreach(IAchievement achievement in achievements)
{
Debug.Log("* achievement = " + achievement.ToString());
}
}

/// <summary>
///
/// 成就回调描述
/// </summary>
/// <param name="achievementDescriptions">Achievement descriptions.</param>
private void HandleAchievementDescriptionsLoaded(IAchievementDescription[] achievementDescriptions)
{
Debug.Log("*** HandleAchievementDescriptionsLoaded");
foreach(IAchievementDescription achievementDescription in achievementDescriptions)
{
Debug.Log("* achievementDescription = " + achievementDescription.ToString());
}
}

}

解决问题不易,转载请注明”gamemode1.com”