unity的 Social API

孙广东  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");
    });
}

同样的使用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");
        });
    }
}

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

还有 See Also: GameCenterPlatform.类

时间: 2024-08-26 07:09:14

unity的 Social API的相关文章

Unity获取Android和iOS手机系统电量及网络状况

最开始考虑使用中间静态链接库来调用手机系统自带的API,但是在研究的过程中发现Android系统将电量等信息记录在了固定的文件中,所以只需要在C#中直接读取就可以而不需要中间库. a.Android版 1.通过C#直接读取,下面的GetBatteryLevel()方法 int GetBatteryLevel() { try { string CapacityString = System.IO.File.ReadAllText("/sys/class/power_supply/battery/c

(转)Unity Assets目录下的特殊文件夹名称

原文:http://wiki.unity3d.com/index.php/Special_Folder_Names_in_your_Assets_Folder 1.隐藏文件夹以.开头的文件夹会被Unity忽略.在这种文件夹中的资源不会被导入,脚本不会被编译.也不会出现在Project视图中.2.Standard Assets在这个文件夹中的脚本最先被编译.这个文件夹中的脚本会被导出到Assembly-CSharp-firstpass, Assembly-UnityScript-firstpass

Unity客户端通信测试问题处理(二)

在客户端的通信测试过程中,场景加载的问题给自己带来了不小的麻烦.因为消息的解析方法在单独的监听线程中调用,这也就意味着无法在消息的解析方法中调用Unity自身的API了.本来是打算在接收到场景切换的消息后,直接在解析方法中调用协同程序StartCoroutine,来实现场景的异步加载,可是现在一旦调用就会提示以下错误: StartCoroutine_Auto can only be called from the main thread... 不能直接在监听线程中调用,那就只能另想办法了.如何来

Unity 触屏缩放模型

现在的手机都是触屏控制的,那么在游戏中我们想通过手指在屏幕上滑动捕获相应的动作呢?Unity官网API中提供了Input类和Touch类,在该类里提供了许多接口.相信只要我们稍微看下,就可以自己应用了!下面以模型的旋转与缩放为例,学习下是如何实现多点触控的! 1,打开Unity建一个空的项目工程. 2,以系统提供的模型为例,此处只做逻辑测试. 3,新建控制类脚本ScaleAndRotate.cs using UnityEngine; using System.Collections; using

Unity中所有特殊的文件夹

1. 隐藏文件夹以.开头的文件夹会被Unity忽略.在这种文件夹中的资源不会被导入,脚本不会被编译.也不会出现在Project视图中.2. Standard Assets在这个文件夹中的脚本最先被编译.这个文件夹中的脚本会被导出到Assembly-CSharp-firstpass, Assembly-UnityScript-firstpass 或 Assembly-Boo-firstpass项目中,依语言而定. 参考http://docs.unity3d.com/Documentation/Ma

【只怕没有几个人能说清楚】系列之二:Unity中的特殊文件夹

参考:http://www.manew.com/thread-99292-1-1.html 1. 隐藏文件夹     以.开头的文件夹会被忽略.在这种文件夹中的资源不会被导入,脚本不会被编译.也不会出现在Project视图中.这种文件我们可以在资源浏览器的时候,能找到这些文件. 2. Standard Assets     在这个文件夹中的脚本最先被编译.一般是放一些Unity 内置的一些资源.     这个文件夹中的脚本会被导出到Assembly-CSharp-firstpass, Assem

Unity 小地图点击位置映射到地图实体位置(类似王者荣耀的小地图点击功能)

小地图自制功能就不多说了,我的小地图制作参考地址:https://www.youtube.com/watch?v=EeyZ2y2Jpz4 建议直接到地址中去看UGUI的小地图的制作,觉得还是讲得比较好的. 下面在参考地址的制作前提下做一个小地图点击位置映射到地图实体位置中去的功能. 准备工作: 搭建场景: 简单来说就是自己随意搭建一个场景,然后俯视将场景截图保存作为UI的Image图片当作小地图的背景图片(这样就不利用另一个摄像机通过RenderTexture来做背景了) UI的目录解析如下:

Unity Assets目录下的特殊文件夹名称

1.隐藏文件夹 以.开头的文件夹会被Unity忽略.在这种文件夹中的资源不会被导入,脚本不会被编译.也不会出现在Project视图中.2.Standard Assets 在这个文件夹中的脚本最先被编译. 这个文件夹中的脚本会被导出到Assembly-CSharp-firstpass, Assembly-UnityScript-firstpass 或 Assembly-Boo-firstpass项目中,依语言而定.参考http://docs.unity3d.com/Documentation/Ma

unity一道有意思的面试题

学校享受的日子一去不复还了,呜呜.话说面试了几个公司,真心没准备好就上了,结果当然是小悲催.还好有容身之处,就算是搬砖,也有可能为自己盖楼,吼吼. 好,下面我来分享一道有意思的面试题,说他有意思,是因为这个题目的意义很重要的,好了不卖官司了,先看看题目: 三维空间,Y轴朝上,重力加速度为大小为g, 已知A(x1, y1, z1), B(x2, y2, z2)是三维空间中的两个点,现在从A点发射一个物体,希望该物体经过时间t之后,落在B点,请给出计算发射速度向量. 说到这,请把unity相关的AP