API sosial adalah titik akses ke fitur sosial, seperti:
Ini menyediakan antarmuka terpadu untuk back-end sosial yang berbeda, seperti GameCenter, dan dimaksudkan untuk digunakan terutama oleh programmer pada proyek game.
API sosial terutama merupakan API asinkron, dan cara yang khas untuk menggunakannya adalah dengan membuat panggilan fungsi dan mendaftarkan panggilan ke ketika fungsi tersebut selesai. Fungsi asinkron mungkin memiliki efek samping, seperti mengisi variabel state tertentu di API, dan callback dapat berisi data dari server untuk diproses.
Kelas Sosial berada di ruang nama UnityEngine dan selalu tersedia tetapi kelas API Sosial lainnya disimpan di ruang nama mereka sendiri, UnityEngine. Login Selanjutnya, implementasi API Sosial berada di sub-namespace, seperti SocialPlatforms.GameCenter.
Berikut adalah contoh (JavaScript) tentang bagaimana seseorang dapat menggunakan API Sosial:
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");
});
}
Berikut ini adalah contoh yang sama menggunakan 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");
});
}
}
Untuk info lebih lanjut tentang API Sosial, periksa ruang Referensi Scripting API Sosial%