孙广东 2015.12.23
Social API
Social API 是訪问的Unity 的point 社会功能。如:
• 用户配置文件
• 好友列表
• 成就
• 统计 / 排行榜
它提供了不同的social 后端。如 XBox Live 或 GameCenter,一个统一的接口,主要为了由程序猿在游戏项目上使用。
Social API 主要是异步的 API。并使用它的典型方式是 通过一个函数调用 和注冊一个回调方法向该函数完毕时。异步函数可能有副作用。如 增生某些状态变量在 API 中,和回调可能包括来自服务器要处理的数据。
Social 类驻留在 UnityEngine 命名空间中,所以始终是可用。但其它社会 API 类都保存在自己的命名空间,UnityEngine.SocialPlatforms. Furthermore。
此外。Social API 的实现是在子命名空间,如 SocialPlatforms.GameCenter。
在这里是一个样例 (JavaScript) 怎样使用Social API:
import UnityEngine.SocialPlatforms; function Start () { // Authenticate and register a ProcessAuthentication callback // This call needs to be made before we can proceed to other calls in the Social API Social.localUser.Authenticate (ProcessAuthentication); } // This function gets called when Authenticate completes // Note that if the operation is successful, Social.localUser will contain data from the server. function ProcessAuthentication (success: boolean) { if (success) { Debug.Log ("Authenticated, checking achievements"); // Request loaded achievements, and register a callback for processing them Social.LoadAchievements (ProcessLoadedAchievements); } else Debug.Log ("Failed to authenticate"); } // This function gets called when the LoadAchievement call completes function ProcessLoadedAchievements (achievements: IAchievement[]) { if (achievements.Length == 0) Debug.Log ("Error: no achievements found"); else Debug.Log ("Got " + achievements.Length + " achievements"); // You can also call into the functions like this Social.ReportProgress ("Achievement01", 100.0, function(result) { if (result) Debug.Log ("Successfully reported achievement progress"); else Debug.Log ("Failed to report achievement"); }); }
using UnityEngine; using UnityEngine.SocialPlatforms; public class SocialExample : MonoBehaviour { void Start () { // Authenticate and register a ProcessAuthentication callback // This call needs to be made before we can proceed to other calls in the Social API Social.localUser.Authenticate (ProcessAuthentication); } // This function gets called when Authenticate completes // Note that if the operation is successful, Social.localUser will contain data from the server. void ProcessAuthentication (bool success) { if (success) { Debug.Log ("Authenticated, checking achievements"); // Request loaded achievements, and register a callback for processing them Social.LoadAchievements (ProcessLoadedAchievements); } else Debug.Log ("Failed to authenticate"); } // This function gets called when the LoadAchievement call completes void ProcessLoadedAchievements (IAchievement[] achievements) { if (achievements.Length == 0) Debug.Log ("Error: no achievements found"); else Debug.Log ("Got " + achievements.Length + " achievements"); // You can also call into the functions like this Social.ReportProgress ("Achievement01", 100.0, result => { if (result) Debug.Log ("Successfully reported achievement progress"); else Debug.Log ("Failed to report achievement"); }); } }
对Social API的很多其它信息。看看Social API脚本參考
还有 See Also: GameCenterPlatform.类