Version: 2022.2
准备应用程序进行应用内购 (IAP)
Building and delivering for iOS

Social API

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 () {
        // 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");
        });
    }
}

For more info on the Social API, see Social API Scripting Reference

准备应用程序进行应用内购 (IAP)
Building and delivering for iOS