- 最后登录
- 2018-12-19
- 注册时间
- 2012-8-20
- 阅读权限
- 90
- 积分
- 54706
- 纳金币
- 32328
- 精华
- 41
|
Social API is Unity's point of access to social features, such as:网络交互API是unity的访问网络交互(如:ios的GameCenter)的功能点。比如: User profiles 用户配置文件
Friends lists 好友列表
Achievements 成就
Statistics / Leaderboards 统计/排行榜It provides a unified interface to different social back-ends, such as XBox Live or GameCenter, and is meant to be used primarily by programmers on the game project.它在不同的网络交互(social)的两端(back-ends)提供了一个统一的接口,比如 XBox Live 或者 GameCenter,主要由程序员用于游戏项目上。The Social API is mainly an asynchronous API, and the typical way to use it is by making a function call and registering for a callback to when that function completes. The asynchronous function may have side effects, such as populating certain state variables in the API, and the callback could contain data from the server to be processed.网络交互API主要是一个异步API,使用它的典型方法是通过一个函数调用和在函数结束时注册一个回调函数。异步函数可能会有一些副作用,比如:在API中添入某些状态变量,回调可能包含数据,数据来自服务器或者是处理好的。The Social class resides in the UnityEngine namespace and so is always available but the other Social API classes are kept in their own namespace, UnityEngine.SocialPlatforms. Furthermore, implementations of the Social API are in a sub-namespace, like SocialPlatforms.GameCenter.Social 类是属于UnityEngine命名空间,所以可以直接使用,但是其他的网络交互API类都有自己的命名空间:UnityEngine.SocialPlatforms。 此外网络交互API的实现是在一个子命名空间,像这样:SocialPlatforms.GameCenter。Here is an example (JavaScript) of how one might use the Social API:下面是如何使用网络交互API的JavaScript例子:import UnityEngine.SocialPlatforms;function Start () {
// Authenticate and register a ProcessAuthentication callback
// 验证和注册一个ProcessAuthentication回调函数
// This call needs to be made before we can proceed to other calls in the Social API
//这个调用需要在其他网络交互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.
// 注意:如果操作陈功,Social.localUser将包含来自服务器的数据。
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
// 当LoadAchievement调用完成时这个函数被调用。
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");
});
}Here is the same example using C#.下面是C#的例子:using UnityEngine;
using UnityEngine.SocialPlatforms;public class SocialExample : MonoBehaviour { void Start () {
// Authenticate and register a ProcessAuthentication callback
// 验证和注册一个ProcessAuthentication回调函数
// This call needs to be made before we can proceed to other calls in the Social API
//这个调用需要在其他网络交互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.
// 注意:如果操作陈功,Social.localUser将包含来自服务器的数据。
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
// 当LoadAchievement调用完成时这个函数被调用。
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");
});
}
}For more info on the Social API, check out the Social API Scripting Reference关于网络交互API的更多信息,请参考网络交互API的脚本参考
【来源:互联网】
更多精彩教程,尽在web3D纳金网http://www.narkii.com/college/ |
|