包含此页的版本:
不含此页的版本:
注意:社交 API 已弃用,并将在将来的版本中删除。
社交 API 是 Unity 访问社交功能的点,例如:
它为不同的社交后端(例如 GameCenter)提供了统一的接口,主要供游戏项目的程序员使用。
社交 API 主要是一个异步 API,它的典型使用方法是进行函数调用并注册回调到该函数完成时。异步函数可能会产生副作用,例如在 API 中填充某些状态变量,并且回调可能包含来自要处理的服务器的数据。
Social 类驻留在 UnityEngine 命名空间中,因此始终可用,但其他 Social API 类保留在自己的命名空间 UnityEngine.SocialPlatforms 中。此外,社交 API 的实现位于子命名空间中,例如 SocialPlatforms.GameCenter。
以下是如何使用社交 API 的示例:
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");
});
}
}
有关社交 API 的详细信息,请参阅社交 API 脚本参考