Social API 是 Unity 访问如下社交功能的关键点:
此 API 为不同的社交后端(例如 __GameCenter__)提供了统一的接口,主要供程序员在游戏项目中使用。
Social API 主要是一个异步 API,典型的用法是进行函数调用并该函数完成时注册回调。异步函数可能具有副作用,例如在 API 中填充某些状态变量,并且回调可能包含要处理的服务器数据。
Social 类位于 UnityEngine 命名空间中,因此该类始终可用,但其他 Social API 类保存在它们自己的命名空间 (UnityEngine.SocialPlatforms) 中。此外,Social API 的实现位于子命名空间(如 SocialPlatforms.GameCenter)中。
Here is an example of how to use the Social API:
using UnityEngine;
using UnityEngine.SocialPlatforms;
public class SocialExample : MonoBehaviour {
void Start () {
// 验证并注册 ProcessAuthentication 回调
// 需要进行此调用才能继续进行 Social API 中的其他调用
Social.localUser.Authenticate (ProcessAuthentication);
}
// 当验证完成时将调用此函数
// 请注意,如果操作成功,Social.localUser 将包含来自服务器的数据。
void ProcessAuthentication (bool success) {
if (success) {
Debug.Log ("Authenticated, checking achievements");
// 请求加载的成就,并注册回调来处理它们
Social.LoadAchievements (ProcessLoadedAchievements);
}
else
Debug.Log ("Failed to authenticate");
}
// LoadAchievement 调用完成时将调用此函数
void ProcessLoadedAchievements (IAchievement[] achievements) {
if (achievements.Length == 0)
Debug.Log ("Error: no achievements found");
else
Debug.Log ("Got " + achievements.Length + " achievements");
// 也可以按照以下方式调用函数
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, see Social API Scripting Reference
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.