Sun Guangdong 2015.12.23
Social API
The social API is a point social feature that accesses unity, such as:
? User Configuration Files
? Friends List
? Achievement
? Statistics/Leaderboard
It offers different social backend, such as XBox Live or Gamecenter, a unified interface, primarily for use by programmers on game projects.
The social API is primarily an asynchronous API, and the typical way to use it is through a function call and registering a callback method to the function when it is completed. Asynchronous functions may have side effects, such as hyperplasia of certain state variables in the API, and callbacks may contain data from the server to process.
The social class resides in the Unityengine namespace, so it is always available, but other social API classes are saved in their own namespace, unityengine.socialplatforms. furthermore. In addition, the implementation of the social API is in a child namespace, such as Socialplatforms.gamecenter.
Here is an example (JavaScript) of how to use the social API:
Import Unityengine.socialplatforms;function Start () {//Authenticate and register a processauthentication callback This call needs to being made before we can proceed to other calls in the social API Social.localUser.Authenticate (Pr ocessauthentication);} This function gets called when authenticate completes//Note that if the operation is successful, Social.localuser would Contain data from the server. function Processauthentication (Success:boolean) {if (success) {Debug.Log ("authenticated, checking Achieveme NTS "); Request loaded achievements, and register a callback for processing them social.loadachievements (Processloadeda chievements); } else Debug.Log ("Failed to Authenticate");} This function gets called when the loadachievement call Completesfunction processloadedachievements (Achievements:iach Ievement[]) {if (achievements. Length = = 0) Debug.Log ("Error:no achievements found"); else Debug.Log ("Got"+ achievements. Length + "achievements"); can also call to 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"); });}
the sameitis aUseC #Example
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 proc Eed 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.local User'll contain data from the server. void Processauthentication (bool success) {if (success) {Debug.Log ("authenticated, checking Achieveme NTS "); Request loaded achievements, and register a callback for processing them social.loadachievements (Processloa dedachievements); } 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"); can also call to 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 information on the social API, see the Social API scripting reference
and see Also:gamecenterplatform. class
Unity's Social API