Skip to content

Commit a2d823b

Browse files
authoredSep 1, 2020
Merge pull request #6 from notredamstra/migrate-to-unitywebrequest
Migrate to unitywebrequest
2 parents d661771 + fbe3bf0 commit a2d823b

File tree

3 files changed

+367
-335
lines changed

3 files changed

+367
-335
lines changed
 

‎.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ ExportedObj/
1717
*.pidb
1818
*.booproj
1919
*.svd
20+
.vs/
21+
.editorconfig
22+
*.config
2023

2124

2225
# Unity3D generated meta files

‎GlobalstatsIO.cs

+324-315
Original file line numberDiff line numberDiff line change
@@ -1,322 +1,331 @@
1-
using UnityEngine;
1+
using UnityEngine;
22
using UnityEngine.Networking;
33
using System;
44
using System.Text;
55
using System.Collections;
66
using System.Collections.Generic;
77

8-
[System.Serializable]
9-
public class GlobalstatsIO_StatisticValues
8+
namespace GlobalstatsIO
109
{
11-
public string key = null;
12-
public string value = "0";
13-
public string sorting = null;
14-
public string rank = "0";
15-
public string value_change = "0";
16-
public string rank_change = "0";
17-
}
18-
19-
[System.Serializable]
20-
public class GlobalstatsIO_LinkData
21-
{
22-
public string url = null;
23-
public string pin = null;
24-
}
25-
26-
[System.Serializable]
27-
public class GlobalstatsIO_LeaderboardValue
28-
{
29-
public string name = null;
30-
public string user_profile = null;
31-
public string user_icon = null;
32-
public string rank = "0";
33-
public string value = "0";
34-
}
35-
36-
[System.Serializable]
37-
public class GlobalstatsIO_Leaderboard {
38-
39-
public GlobalstatsIO_LeaderboardValue[] data;
40-
}
41-
42-
public class GlobalstatsIO
43-
{
44-
[System.Serializable]
45-
private class GlobalstatsIO_AccessToken
46-
{
47-
public string access_token = null;
48-
public string token_type = null;
49-
public string expires_in = null;
50-
public Int32 created_at = (Int32)(DateTime.UtcNow.Subtract (new DateTime (1970, 1, 1))).TotalSeconds;
51-
52-
public bool isValid()
53-
{
54-
//Check if still valid, allow a 2 minute grace period
55-
return (created_at + int.Parse(expires_in) - 120) > (Int32)(DateTime.UtcNow.Subtract (new DateTime (1970, 1, 1))).TotalSeconds;
56-
}
57-
}
58-
59-
[System.Serializable]
60-
private class GlobalstatsIO_StatisticResponse
61-
{
62-
public string name = null;
63-
public string _id = null;
64-
[SerializeField]
65-
public List<GlobalstatsIO_StatisticValues> values = null;
66-
}
67-
68-
public static string api_id = "";
69-
public static string api_secret = "";
70-
71-
private static GlobalstatsIO_AccessToken api_access_token = null;
72-
73-
private static List<GlobalstatsIO_StatisticValues> statistic_values = new List<GlobalstatsIO_StatisticValues> ();
74-
public static string statistic_id = "";
75-
public static string user_name = "";
76-
public static GlobalstatsIO_LinkData link_data = null;
77-
78-
private bool getAccessToken ()
79-
{
80-
string url = "https://api.globalstats.io/oauth/access_token";
81-
82-
WWWForm form = new WWWForm ();
83-
form.AddField ("grant_type", "client_credentials");
84-
form.AddField ("scope", "endpoint_client");
85-
form.AddField ("client_id", api_id);
86-
form.AddField ("client_secret", api_secret);
87-
88-
WWW www = new WWW (url, form);
89-
90-
while (!www.isDone) {
91-
System.Threading.Thread.Sleep (100);
92-
}
93-
94-
// check for errors
95-
if (www.error == null) {
96-
UnityEngine.Debug.Log ("WWW getAccessToken Ok!: ");
97-
} else {
98-
UnityEngine.Debug.Log ("WWW getAccessToken Error: " + www.error);
99-
UnityEngine.Debug.Log ("WWW Content: " + www.text);
100-
return false;
101-
}
102-
103-
api_access_token = JsonUtility.FromJson<GlobalstatsIO_AccessToken> (www.text);
104-
105-
return true;
106-
}
107-
108-
public bool share (string id = "", string name = "", Dictionary<string, string> values = null)
109-
{
110-
if (api_access_token == null || !api_access_token.isValid()) {
111-
if (!getAccessToken ()) {
112-
return false;
113-
}
114-
}
115-
116-
// If no id is supplied but we have one stored, reuse it.
117-
if (id == "" && statistic_id != "")
118-
id = statistic_id;
119-
120-
string url = "https://api.globalstats.io/v1/statistics";
121-
if (id != "") {
122-
url = "https://api.globalstats.io/v1/statistics/" + id;
123-
} else {
124-
if (name == "")
125-
name = "anonymous";
126-
}
127-
128-
string json_payload = "{\"name\":\"" + name + "\",\"values\":{";
129-
130-
bool semicolon = false;
131-
foreach (KeyValuePair<string,string> value in values) {
132-
if (semicolon)
133-
json_payload += ",";
134-
json_payload += "\"" + value.Key + "\":\"" + value.Value + "\"";
135-
semicolon = true;
136-
}
137-
json_payload += "}}";
138-
139-
byte[] pData = Encoding.ASCII.GetBytes (json_payload.ToCharArray ());
140-
141-
GlobalstatsIO_StatisticResponse statistic = null;
142-
143-
if (id == "") {
144-
Dictionary<string,string> headers = new Dictionary<string,string> ();
145-
headers.Add ("Authorization", "Bearer " + api_access_token.access_token);
146-
headers.Add ("Content-Type", "application/json");
147-
148-
WWW www = new WWW (url, pData, headers);
149-
150-
while (!www.isDone) {
151-
System.Threading.Thread.Sleep (100);
152-
}
153-
154-
// check for errors
155-
if (www.error == null) {
156-
//UnityEngine.Debug.Log ("WWW POST Ok!");
157-
} else {
158-
UnityEngine.Debug.Log ("WWW POST Error: " + www.error);
159-
UnityEngine.Debug.Log ("WWW POST Content: " + www.text);
160-
return false;
161-
}
162-
163-
statistic = JsonUtility.FromJson<GlobalstatsIO_StatisticResponse> (www.text);
164-
} else {
165-
UnityEngine.Networking.UnityWebRequest www = UnityEngine.Networking.UnityWebRequest.Put(url, pData);
166-
www.SetRequestHeader("Authorization", "Bearer " + api_access_token.access_token);
167-
www.SetRequestHeader("Content-Type", "application/json");
168-
169-
UnityEngine.Networking.UploadHandler uploader = new UnityEngine.Networking.UploadHandlerRaw (pData);
170-
www.uploadHandler = uploader;
171-
172-
www.Send ();
173-
174-
while (!www.isDone) {
175-
System.Threading.Thread.Sleep (100);
176-
}
177-
178-
// check for errors
179-
if (www.error == null) {
180-
//UnityEngine.Debug.Log ("WWW PUT Ok!");
181-
} else {
182-
UnityEngine.Debug.Log ("WWW PUT Error: " + www.error);
183-
UnityEngine.Debug.Log ("WWW PUT Content: " + Encoding.ASCII.GetString(www.downloadHandler.data));
184-
return false;
185-
}
186-
187-
statistic = JsonUtility.FromJson<GlobalstatsIO_StatisticResponse> (Encoding.ASCII.GetString(www.downloadHandler.data));
188-
}
189-
190-
// ID is available only on create, not on update, so do not overwrite it
191-
if(statistic._id != null && statistic._id != "")
192-
statistic_id = statistic._id;
193-
194-
user_name = statistic.name;
195-
196-
//Store the returned data statically
197-
foreach (GlobalstatsIO_StatisticValues value in statistic.values) {
198-
bool updated_existing = false;
199-
for (int i = 0; i < statistic_values.Count; i++) {
200-
if (statistic_values[i].key == value.key) {
201-
statistic_values[i] = value;
202-
updated_existing = true;
203-
break;
204-
}
205-
}
206-
if (!updated_existing) {
207-
statistic_values.Add (value);
208-
}
209-
}
210-
211-
return true;
212-
}
213-
214-
public GlobalstatsIO_StatisticValues getStatistic(string key)
215-
{
216-
for (int i = 0; i < statistic_values.Count; i++) {
217-
if (statistic_values[i].key == key) {
218-
return statistic_values [i];
219-
}
220-
}
221-
return null;
222-
}
223-
224-
public bool linkStatistic(string id = "")
225-
{
226-
if (api_access_token == null || !api_access_token.isValid()) {
227-
if (!getAccessToken ()) {
228-
return false;
229-
}
230-
}
231-
232-
// If no id is supplied but we have one stored, reuse it.
233-
if (id == "" && statistic_id != "")
234-
id = statistic_id;
235-
236-
string url = "https://api.globalstats.io/v1/statisticlinks/"+id+"/request";
237-
238-
string json_payload = "{}";
239-
byte[] pData = Encoding.ASCII.GetBytes (json_payload.ToCharArray ());
240-
241-
Dictionary<string,string> headers = new Dictionary<string,string> ();
242-
headers.Add ("Authorization", "Bearer " + api_access_token.access_token);
243-
headers.Add ("Content-Type", "application/json");
244-
headers.Add ("Content-Length", json_payload.Length.ToString ());
245-
246-
WWW www = new WWW (url, pData, headers);
247-
248-
while (!www.isDone) {
249-
System.Threading.Thread.Sleep (100);
250-
}
251-
252-
// check for errors
253-
if (www.error == null) {
254-
//UnityEngine.Debug.Log ("WWW POST Ok!");
255-
} else {
256-
UnityEngine.Debug.Log ("WWW POST Error: " + www.error);
257-
UnityEngine.Debug.Log ("WWW POST Content: " + www.text);
258-
return false;
259-
}
260-
261-
link_data = JsonUtility.FromJson<GlobalstatsIO_LinkData> (www.text);
262-
return true;
263-
}
264-
265-
// numberOfPlayer can be 100 at max.
266-
public GlobalstatsIO_Leaderboard getLeaderboard(string gtd, int numberOfPlayers) {
267-
268-
269-
if (numberOfPlayers < 0) {
270-
return new GlobalstatsIO_Leaderboard();
271-
} else if(numberOfPlayers > 100) { // Number has to be between 0 and 100
272-
numberOfPlayers = 100;
273-
}
274-
275-
if (api_access_token == null || !api_access_token.isValid())
276-
{
277-
if (!getAccessToken())
278-
{
279-
return new GlobalstatsIO_Leaderboard();
280-
}
281-
}
282-
283-
284-
string url = "https://api.globalstats.io/v1/gtdleaderboard/" + gtd;
285-
286-
string json_payload = "{\"limit\":" + numberOfPlayers + "\n}";
287-
288-
Dictionary<string, string> headers = new Dictionary<string, string>();
289-
headers.Add("Authorization", "Bearer " + api_access_token.access_token);
290-
headers.Add("Content-Type", "application/json");
291-
headers.Add("Cache-Control", "no-cache");
292-
headers.Add("Content-Length", json_payload.Length.ToString());
293-
294-
byte[] pData = Encoding.ASCII.GetBytes(json_payload.ToCharArray());
295-
296-
297-
WWW www = new WWW(url, pData, headers);
298-
299-
300-
while (!www.isDone)
301-
{
302-
System.Threading.Thread.Sleep(100);
303-
}
304-
305-
// check for errors
306-
if (www.error == null)
307-
{
308-
//UnityEngine.Debug.Log ("WWW POST Ok!");
309-
}
310-
else
311-
{
312-
UnityEngine.Debug.Log("WWW POST Error: " + www.error);
313-
UnityEngine.Debug.Log("WWW POST Content: " + www.text);
314-
return new GlobalstatsIO_Leaderboard();
315-
}
316-
317-
GlobalstatsIO_Leaderboard leaderboard = JsonUtility.FromJson<GlobalstatsIO_Leaderboard>(www.text);
318-
319-
return leaderboard;
320-
}
321-
322-
}
10+
[Serializable]
11+
public class StatisticValues
12+
{
13+
public string key = null;
14+
public string value = "0";
15+
public string sorting = null;
16+
public string rank = "0";
17+
public string value_change = "0";
18+
public string rank_change = "0";
19+
}
20+
21+
[Serializable]
22+
public class LinkData
23+
{
24+
public string url = null;
25+
public string pin = null;
26+
}
27+
28+
[Serializable]
29+
public class LeaderboardValue
30+
{
31+
public string name = null;
32+
public string user_profile = null;
33+
public string user_icon = null;
34+
public string rank = "0";
35+
public string value = "0";
36+
}
37+
38+
[Serializable]
39+
public class Leaderboard
40+
{
41+
public LeaderboardValue[] data;
42+
}
43+
44+
public class GlobalstatsIOClient
45+
{
46+
private readonly string _apiId;
47+
private readonly string _apiSecret;
48+
private AccessToken _apiAccessToken;
49+
private List<StatisticValues> _statisticValues = new List<StatisticValues>();
50+
51+
[HideInInspector]
52+
public string StatisticId = "";
53+
54+
[HideInInspector]
55+
public string UserName = "";
56+
57+
[HideInInspector]
58+
public LinkData LinkData = null;
59+
60+
[Serializable]
61+
private class AccessToken
62+
{
63+
public string access_token = null;
64+
public string token_type = null;
65+
public string expires_in = null;
66+
public int created_at = (int) DateTimeOffset.UtcNow.ToUnixTimeSeconds();
67+
68+
//Check if still valid, allow a 2 minute grace period
69+
public bool IsValid() =>
70+
(this.created_at + int.Parse(this.expires_in) - 120) > (int) DateTimeOffset.UtcNow.ToUnixTimeSeconds();
71+
}
72+
73+
[Serializable]
74+
private class StatisticResponse
75+
{
76+
public string name = null;
77+
public string _id = null;
78+
79+
[SerializeField]
80+
public List<StatisticValues> values = null;
81+
}
82+
83+
public GlobalstatsIOClient(string apiKey, string apiSecret)
84+
{
85+
this._apiId = apiKey;
86+
this._apiSecret = apiSecret;
87+
}
88+
89+
private IEnumerator GetAccessToken()
90+
{
91+
string url = "https://api.globalstats.io/oauth/access_token";
92+
93+
WWWForm form = new WWWForm();
94+
form.AddField("grant_type", "client_credentials");
95+
form.AddField("scope", "endpoint_client");
96+
form.AddField("client_id", this._apiId);
97+
form.AddField("client_secret", this._apiSecret);
98+
99+
using (UnityWebRequest www = UnityWebRequest.Post(url, form))
100+
{
101+
www.downloadHandler = new DownloadHandlerBuffer();
102+
yield return www.SendWebRequest();
103+
104+
string responseBody = www.downloadHandler.text;
105+
106+
if (www.isNetworkError || www.isHttpError)
107+
{
108+
Debug.LogWarning("Error retrieving access token: " + www.error);
109+
Debug.Log("GlobalstatsIO API Response: " + responseBody);
110+
yield break;
111+
}
112+
else
113+
{
114+
this._apiAccessToken = JsonUtility.FromJson<AccessToken>(responseBody);
115+
}
116+
}
117+
}
118+
119+
public IEnumerator Share(Dictionary<string, string> values, string id = "", string name = "", Action<bool> callback = null)
120+
{
121+
bool update = false;
122+
123+
if (this._apiAccessToken == null || !this._apiAccessToken.IsValid())
124+
{
125+
yield return this.GetAccessToken();
126+
}
127+
128+
// If no id is supplied but we have one stored, reuse it.
129+
if (id == "" && this.StatisticId != "")
130+
{
131+
id = this.StatisticId;
132+
}
133+
134+
string url = "https://api.globalstats.io/v1/statistics";
135+
if (id != "")
136+
{
137+
url = "https://api.globalstats.io/v1/statistics/" + id;
138+
update = true;
139+
}
140+
else
141+
{
142+
if (name == "")
143+
{
144+
name = "anonymous";
145+
}
146+
}
147+
148+
string jsonPayload;
149+
150+
if (update == false)
151+
{
152+
jsonPayload = "{\"name\":\"" + name + "\", \"values\":{";
153+
}
154+
else
155+
{
156+
jsonPayload = "{\"values\":{";
157+
}
158+
159+
bool semicolon = false;
160+
foreach (KeyValuePair<string, string> value in values)
161+
{
162+
if (semicolon)
163+
{
164+
jsonPayload += ",";
165+
}
166+
167+
jsonPayload += "\"" + value.Key + "\":\"" + value.Value + "\"";
168+
semicolon = true;
169+
}
170+
jsonPayload += "}}";
171+
172+
byte[] pData = Encoding.UTF8.GetBytes(jsonPayload);
173+
StatisticResponse statistic = null;
174+
175+
using (UnityWebRequest www = new UnityWebRequest(url))
176+
{
177+
if (update == false)
178+
{
179+
www.method = "POST";
180+
}
181+
else
182+
{
183+
www.method = "PUT";
184+
}
185+
186+
www.uploadHandler = new UploadHandlerRaw(pData);
187+
www.downloadHandler = new DownloadHandlerBuffer();
188+
www.SetRequestHeader("Authorization", "Bearer " + this._apiAccessToken.access_token);
189+
www.SetRequestHeader("Content-Type", "application/json");
190+
yield return www.SendWebRequest();
191+
192+
string responseBody = www.downloadHandler.text;
193+
194+
if (www.isNetworkError || www.isHttpError)
195+
{
196+
Debug.LogWarning("Error submitting statistic: " + www.error);
197+
Debug.Log("GlobalstatsIO API Response: " + responseBody);
198+
callback?.Invoke(false);
199+
}
200+
else
201+
{
202+
statistic = JsonUtility.FromJson<StatisticResponse>(responseBody);
203+
}
204+
};
205+
206+
// ID is available only on create, not on update, so do not overwrite it
207+
if (statistic._id != null && statistic._id != "")
208+
{
209+
this.StatisticId = statistic._id;
210+
}
211+
212+
this.UserName = statistic.name;
213+
214+
//Store the returned data statically
215+
foreach (StatisticValues value in statistic.values)
216+
{
217+
bool updatedExisting = false;
218+
for (int i = 0; i < this._statisticValues.Count; i++)
219+
{
220+
if (this._statisticValues[i].key == value.key)
221+
{
222+
this._statisticValues[i] = value;
223+
updatedExisting = true;
224+
break;
225+
}
226+
}
227+
if (!updatedExisting)
228+
{
229+
this._statisticValues.Add(value);
230+
}
231+
}
232+
233+
callback?.Invoke(true);
234+
}
235+
236+
public StatisticValues GetStatistic(string key)
237+
{
238+
for (int i = 0; i < this._statisticValues.Count; i++)
239+
{
240+
if (this._statisticValues[i].key == key)
241+
{
242+
return this._statisticValues[i];
243+
}
244+
}
245+
return null;
246+
}
247+
248+
public IEnumerator LinkStatistic(string id = "", Action<bool> callback = null)
249+
{
250+
if (this._apiAccessToken == null || !this._apiAccessToken.IsValid())
251+
{
252+
yield return this.GetAccessToken();
253+
}
254+
255+
// If no id is supplied but we have one stored, reuse it.
256+
if (id == "" && this.StatisticId != "")
257+
{
258+
id = this.StatisticId;
259+
}
260+
261+
string url = "https://api.globalstats.io/v1/statisticlinks/" + id + "/request";
262+
263+
string jsonPayload = "{}";
264+
byte[] pData = Encoding.UTF8.GetBytes(jsonPayload);
265+
266+
using (UnityWebRequest www = new UnityWebRequest(url, "POST")
267+
{
268+
uploadHandler = new UploadHandlerRaw(pData),
269+
downloadHandler = new DownloadHandlerBuffer()
270+
})
271+
{
272+
www.SetRequestHeader("Authorization", "Bearer " + this._apiAccessToken.access_token);
273+
www.SetRequestHeader("Content-Type", "application/json");
274+
yield return www.SendWebRequest();
275+
276+
string responseBody = www.downloadHandler.text;
277+
278+
if (www.isNetworkError || www.isHttpError)
279+
{
280+
Debug.LogWarning("Error linking statistic: " + www.error);
281+
Debug.Log("GlobalstatsIO API Response: " + responseBody);
282+
callback?.Invoke(false);
283+
}
284+
285+
this.LinkData = JsonUtility.FromJson<LinkData>(responseBody);
286+
};
287+
288+
callback?.Invoke(true);
289+
}
290+
291+
public IEnumerator GetLeaderboard(string gtd, int numberOfPlayers, Action<Leaderboard> callback)
292+
{
293+
numberOfPlayers = Mathf.Clamp(numberOfPlayers, 0, 100); // make sure numberOfPlayers is between 0 and 100
294+
295+
if (this._apiAccessToken == null || !this._apiAccessToken.IsValid())
296+
{
297+
yield return this.GetAccessToken();
298+
}
299+
300+
string url = "https://api.globalstats.io/v1/gtdleaderboard/" + gtd;
301+
302+
string json_payload = "{\"limit\":" + numberOfPlayers + "\n}";
303+
Leaderboard leaderboard;
304+
byte[] pData = Encoding.UTF8.GetBytes(json_payload);
305+
306+
using (UnityWebRequest www = new UnityWebRequest(url, "POST")
307+
{
308+
uploadHandler = new UploadHandlerRaw(pData),
309+
downloadHandler = new DownloadHandlerBuffer()
310+
})
311+
{
312+
www.SetRequestHeader("Authorization", "Bearer " + this._apiAccessToken.access_token);
313+
www.SetRequestHeader("Content-Type", "application/json");
314+
yield return www.SendWebRequest();
315+
316+
string responseBody = www.downloadHandler.text;
317+
318+
if (www.isNetworkError || www.isHttpError)
319+
{
320+
Debug.LogWarning("Error getting leaderboard: " + www.error);
321+
Debug.Log("GlobalstatsIO API Response: " + responseBody);
322+
callback?.Invoke(null);
323+
}
324+
325+
leaderboard = JsonUtility.FromJson<Leaderboard>(responseBody);
326+
};
327+
328+
callback?.Invoke(leaderboard);
329+
}
330+
}
331+
}

‎README.md

+40-20
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,13 @@ If you are looking for a .Net implementation that you can also use in Unity you
88
## Usage
99
Simply add the .cs file to your project.
1010

11-
### Preparations
12-
First, before using the library in your project, you have to set a few paramters:
13-
```
14-
GlobalstatsIO.api_id = "Your_API_ID";
15-
GlobalstatsIO.api_secret = "Your_API_Secret";
16-
```
17-
You can set those anywhere in the code. The values are static and will be kept as long as you game is running.
18-
1911
### Submitting Scores
20-
To submit a player score instatiante a object, add the values and call share()
12+
To submit a player score instatiante a object, add the values and call Share()
2113
```
22-
GlobalstatsIO gs = new GlobalstatsIO();
14+
private const string GlobalstatsIOApiId = "YourApiIdHere";
15+
private const string GlobalstatsIOApiSecret = "YourApiSecretHere";
16+
17+
GlobalstatsIO gs = new GlobalstatsIO(GlobalstatsIOApiId, GlobalstatsIOApiSecret);
2318
2419
string user_name = "Nickname";
2520
@@ -28,19 +23,25 @@ values.Add ("score", Score.score.ToString());
2823
values.Add ("shots", Score.shots_fired.ToString());
2924
values.Add ("time", (Score.play_time * 1000).ToString());
3025
31-
if (gs.share ("", user_name, values)) {
32-
// Success
33-
}
34-
else {
35-
// An Error occured
26+
// use StartCoroutine to submit the score asynchronously and use the optional callback parameter
27+
StartCoroutine(gs.Share ("", user_name, values, CallbackMethod)));
28+
29+
void CallbackMethod(bool success){
30+
if (success){
31+
// do something with success
32+
}
33+
else {
34+
// do something with error
35+
}
3636
}
37+
3738
```
38-
The share call will store the users name and the ID it received back from globalstst.io.
39+
The Share call will store the users name and the ID it received back from globalstats.io.
3940
This allows you to send updated to the score by simply doing the same call again with the new values.
4041

4142
You can check if there is a ID stored via this variable
4243
```
43-
if (GlobalstatsIO.statistic_id != "") {
44+
if (gs.statistic_id != "") {
4445
// A statistic was already shared, new calls to share() will do an update
4546
}
4647
```
@@ -51,8 +52,17 @@ Most of the time you want to allow the user to link his scores with his globalst
5152
In this case you can do this with following lines
5253
```
5354
GlobalstatsIO gs = new GlobalstatsIO();
54-
gs.linkStatistic ();
55-
Application.OpenURL (GlobalstatsIO.link_data.url);
55+
StartCoroutine(gs.LinkStatistic(CallbackMethod));
56+
57+
void CallbackMethod(bool success){
58+
if (success){
59+
// do something with success, like redirecting to the webpage
60+
Application.OpenURL (gs.link_data.url);
61+
}
62+
else {
63+
// do something with error
64+
}
65+
}
5666
```
5767
Of course, this will use the data from the prior share() call for linking.
5868

@@ -63,7 +73,17 @@ You can also fetch the current top positions of your leaderboard with a GTD of y
6373
GlobalstatsIO gs = new GlobalstatsIO();
6474
string gtd = "score"
6575
int limit = 2;
66-
gs.getLeaderboard (gtd, limit);
76+
77+
StartCoroutine(gs.GetLeaderboard(gtd, limit, CallbackMethod));
78+
79+
void CallbackMethod(Leaderboard leaderboard){
80+
if (leaderboard != null){
81+
// do something with leaderboard
82+
}
83+
else {
84+
// do something with error
85+
}
86+
}
6787
```
6888

6989
In this case we want the leaderboard of the GTD score. The limit is the number players you want to fetch, which has to be between 1 and 100.

0 commit comments

Comments
 (0)
Please sign in to comment.