Unity3D GameCenter 排行榜, 成就实现

Unity3D内部已经集成了对 GameCenter的支持 在

UnityEngine.SocialPlatforms命名空间下,基本已经满足目前需求

 http://wiki.ceeger.com/manual:social_api?s[]=socialplatforms


C#:

using UnityEngine;
using System.Collections;
using UnityEngine.SocialPlatforms;
using UnityEngine.SocialPlatforms.GameCenter;

public class IOSManager : MonoBehaviour {
	
	public bool   GameCenterState;
	public string userInfo;
	/// 
	/// 初始化 GameCenter 登陆
	/// 
	void Start () {
		Social.localUser.Authenticate(HandleAuthenticated);
	}
		
	/// 
	/// 初始化 GameCenter 结果回调函数
	/// 
	/// If set to true success.
	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);
	}

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

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

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


 


}
 别着急此时你要是在真机运行估计还不可以,你还需要在 ITunes Connect 里面设置 排行榜单成就

当然此时你的APP 应该是已经申请好的咯,并且 “GameCenter” 功能应该是开启的


Unity3D GameCenter 排行榜, 成就实现_第1张图片


然后就是 设置你的 排行榜ID  和  成就ID

Unity3D GameCenter 排行榜, 成就实现_第2张图片


代码中的“XXXX” 的地方就是 填这两个东西


完事了然后你就可以在你的 游戏中用 GameCenter 了!

你可能感兴趣的:(Unity,Itunes,Connect,Gamecenter)