Unity的 Social API使用解析

发表于2017-10-12
评论0 1.5k浏览

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:

  1. import UnityEngine.SocialPlatforms;  
  2.   
  3. function Start () {  
  4.     // Authenticate and register a ProcessAuthentication callback  
  5.     // This call needs to be made before we can proceed to other calls in the Social API  
  6.     Social.localUser.Authenticate (ProcessAuthentication);  
  7. }  
  8.   
  9. // This function gets called when Authenticate completes  
  10. // Note that if the operation is successful, Social.localUser will contain data from the server.   
  11. function ProcessAuthentication (success: boolean) {  
  12.     if (success) {  
  13.         Debug.Log ("Authenticated, checking achievements");  
  14.   
  15.         // Request loaded achievements, and register a callback for processing them  
  16.         Social.LoadAchievements (ProcessLoadedAchievements);  
  17.     }  
  18.     else  
  19.         Debug.Log ("Failed to authenticate");  
  20. }  
  21.   
  22. // This function gets called when the LoadAchievement call completes  
  23. function ProcessLoadedAchievements (achievements: IAchievement[]) {  
  24.     if (achievements.Length == 0)  
  25.         Debug.Log ("Error: no achievements found");  
  26.     else  
  27.         Debug.Log ("Got "   achievements.Length   " achievements");  
  28.       
  29.     // You can also call into the functions like this  
  30.     Social.ReportProgress ("Achievement01", 100.0, function(result) {  
  31.         if (result)  
  32.             Debug.Log ("Successfully reported achievement progress");  
  33.         else  
  34.             Debug.Log ("Failed to report achievement");  
  35.     });  
  36. }  


  1. using UnityEngine;  
  2. using UnityEngine.SocialPlatforms;  
  3.   
  4. public class SocialExample : MonoBehaviour {  
  5.       
  6.     void Start () {  
  7.         // Authenticate and register a ProcessAuthentication callback  
  8.         // This call needs to be made before we can proceed to other calls in the Social API  
  9.         Social.localUser.Authenticate (ProcessAuthentication);  
  10.     }  
  11.   
  12.     // This function gets called when Authenticate completes  
  13.     // Note that if the operation is successful, Social.localUser will contain data from the server.   
  14.     void ProcessAuthentication (bool success) {  
  15.         if (success) {  
  16.             Debug.Log ("Authenticated, checking achievements");  
  17.   
  18.             // Request loaded achievements, and register a callback for processing them  
  19.             Social.LoadAchievements (ProcessLoadedAchievements);  
  20.         }  
  21.         else  
  22.             Debug.Log ("Failed to authenticate");  
  23.     }  
  24.   
  25.     // This function gets called when the LoadAchievement call completes  
  26.     void ProcessLoadedAchievements (IAchievement[] achievements) {  
  27.         if (achievements.Length == 0)  
  28.             Debug.Log ("Error: no achievements found");  
  29.         else  
  30.             Debug.Log ("Got "   achievements.Length   " achievements");  
  31.        
  32.         // You can also call into the functions like this  
  33.         Social.ReportProgress ("Achievement01", 100.0, result => {  
  34.             if (result)  
  35.                 Debug.Log ("Successfully reported achievement progress");  
  36.             else  
  37.                 Debug.Log ("Failed to report achievement");  
  38.         });  
  39.     }  
  40. }  

对Social API的更多信息,看看Social API脚本参考

还有 See Also: GameCenterPlatform.类

如社区发表内容存在侵权行为,您可以点击这里查看侵权投诉指引