Action disabled: source

Social API

Social API는 다음과 같은 Unity의 사회적 기능 점입니다:

  • 사용자 프로파일
  • 친구 리스트(Friends lists)
  • 성취(Achievements)
  • 통계/지도자보드(Leaderboards)

이는 XBox Live 혹은 GameCenter와 같은 다양한 사회적 배경(back-ends)에 대해 통합 인터페이스를 제공하고, 이는 주로 게임 프로젝트의 프로그래머의 사용을 위한 것 입니다.

Social API는 기본적으로 비동기 API 이며, 이를 사용하는 일반적인 방법은 함수 호출을 수행하고 그 함수가 완료되면 콜백(callback)을 등록하는 것 입니다. 비동기적 함수는 API에서 일부 상태 변수를 채우는 등의 부작용이 있으며, 콜백은 서버가 처리해야 할 데이터를 포함할 수 있습니다.

Social 클래스는 UnityEngine 네임공간(namespace)에 존재하고 그러므로 항상 사용할 수 있지만 다른 Social API는 자신만의 네임공간인 UnityEngine.SocialPlatforms 에 보관됩니다. 추가적으로, Social API의 구현은 SocialPlatforms.GameCenter과 같은 하위 네임공간에 있습니다.

Social API 다음은 Social API을 사용하는 한 예제(JavaScript) 입니다:

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 Scripting Reference을 확인합니다.

역링크