Social API
Social API 是访问的Unity 的point 社会功能,下面就给大家介绍下Social API的作用,如:
• 用户配置文件
• 好友列表
• 成就
• 统计 / 排行榜
它提供了不同的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 () {
-
-
- Social.localUser.Authenticate (ProcessAuthentication);
- }
-
-
-
- function ProcessAuthentication (success: boolean) {
- if (success) {
- Debug.Log ("Authenticated, checking achievements");
-
-
- Social.LoadAchievements (ProcessLoadedAchievements);
- }
- else
- Debug.Log ("Failed to authenticate");
- }
-
-
- function ProcessLoadedAchievements (achievements: IAchievement[]) {
- if (achievements.Length == 0)
- Debug.Log ("Error: no achievements found");
- else
- Debug.Log ("Got " achievements.Length " achievements");
-
-
- 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 () {
-
-
- Social.localUser.Authenticate (ProcessAuthentication);
- }
-
-
-
- void ProcessAuthentication (bool success) {
- if (success) {
- Debug.Log ("Authenticated, checking achievements");
-
-
- Social.LoadAchievements (ProcessLoadedAchievements);
- }
- else
- Debug.Log ("Failed to authenticate");
- }
-
-
- void ProcessLoadedAchievements (IAchievement[] achievements) {
- if (achievements.Length == 0)
- Debug.Log ("Error: no achievements found");
- else
- Debug.Log ("Got " achievements.Length " achievements");
-
-
- 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.类