unity的 Social API介紹
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 completesfunction 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"); });}
同樣的這是使用c#樣本
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"); }); }}