码迷,mamicode.com
首页 > 编程语言 > 详细

unity的 Social API

时间:2015-12-24 09:23:29      阅读:254      评论:0      收藏:0      [点我收藏+]

标签:

孙广东  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.类



unity的 Social API

标签:

原文地址:http://blog.csdn.net/u010019717/article/details/50390255

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!