unity的 Social API介绍

页面导航:首页 > 网络编程 > JavaScript > unity的 Social API介绍

unity的 Social API介绍

来源: 作者: 时间:2016-01-19 18:32 【

2015 12 23Social APISocial API 是访问的Unity 的point 社会功能,如:? 用户配置文件? 好友列表? 成就? 统计 排行榜 它提供了不同的social 后端,如 XBox Live 或 GameCe
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");
        });
    }
}
Tags:

文章评论

最 近 更 新
热 点 排 行
Js与CSS工具
代码转换工具

<