Version: 2018.2
빌드 파일 크기 축소
JSON 직렬화

소셜 API

소셜 API는 Unity에서 다음과 같은 소셜 기능에 액세스하는 방법입니다.

  • 사용자 프로필
  • 친구 리스트
  • 목표 달성
  • 통계 / 리더보드

API는 GameCenter 같은 다양한 소셜 백엔드용 통합 인터페이스를 제공하고, 주로 게임 프로젝트의 프로그래머가 사용하도록 고안되었습니다.

소셜 API는 기본적으로 비동기 API이며, 일반적으로 함수를 호출하고 해당 함수가 완료되면 콜백을 등록하는 방식으로 사용됩니다. 비동기 함수는 특정 상태 변수를 API에 채우고 콜백에 서버에서 전송된 처리할 데이터가 포함될 수 있는 등 몇 가지 부작용이 있습니다.

Social 클래스는 UnityEngine 네임스페이스에 있으므로 항상 사용 가능하지만, 다른 소셜 API 클래스는 자체 네임스페이스인 UnityEngine.SocialPlatforms에 저장됩니다. 나아가 소셜 API의 구현은 SocialPlatforms.GameCenter 같은 하위 네임스페이스에 있습니다.

다음은 소셜 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");
        });
    }
}


소셜 API에 대한 자세한 내용은 소셜 API 스크립팅 레퍼런스를 참조하십시오.

빌드 파일 크기 축소
JSON 직렬화