-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathCallWebService.cs
40 lines (29 loc) · 1.02 KB
/
CallWebService.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
using UnityEngine;
using UnityEngine.UI;
using System.Net;
using System.Threading;
using System.Collections;
public class CallWebService : MonoBehaviour {
public Text displayText;
public void CallIPAddressServiceSync() {
displayText.text = "Fetching Ip address... brb.";
// just to be extra rude
Thread.Sleep(10000);
// this blocks on the return of DownloadString
displayText.text = new WebClient().DownloadString("http://api.ipify.org");
}
public void CallIPAddressServiceAsync() {
var handle = Async.Run(CallIPAddressAsync());
}
IEnumerator CallIPAddressAsync() {
string ip = "IP n/a";
displayText.text = "Fetching Ip address... brb.";
yield return Async.ToAsync; // leave the unity thread and do background processing
// just to be extra rude
Thread.Sleep(10000);
// this blocks on the return of DownloadString
ip = new WebClient().DownloadString("http://api.ipify.org");
yield return Async.ToGame; // I've got the result, need to return to unity to set the display
displayText.text = ip;
}
}