Created
March 23, 2018 09:33
-
-
Save rohansen/6396003f213131a642b2a92511419fb7 to your computer and use it in GitHub Desktop.
Calling web services with Coroutines
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Assets; | |
using Newtonsoft.Json; | |
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class Trivia : MonoBehaviour { | |
public GameObject plane; | |
// Use this for initialization | |
void Start () { | |
} | |
// Update is called once per frame | |
void Update () { | |
if (Input.GetKeyDown(KeyCode.Space)) | |
{ | |
StartCoroutine(GetCage()); | |
} | |
} | |
float prevScale = 1f; | |
IEnumerator DoOverTime() | |
{ | |
while (prevScale > 0) | |
{ | |
prevScale -= 0.01f; | |
plane.transform.localScale = new Vector3(prevScale, prevScale, prevScale); | |
yield return new WaitForSeconds(0.1f); | |
} | |
} | |
IEnumerator GetData() | |
{ | |
WWW www = new WWW("https://opentdb.com/api.php?amount=10&type=multiple"); | |
yield return www; | |
print(www.text); | |
var test = JsonConvert.DeserializeObject<RootObject>(www.text); | |
var a = www.texture; | |
var b = www.progress; | |
} | |
IEnumerator GetCage() | |
{ | |
var randomDivensionx = Random.Range(200, 1600); | |
var randomDivensiony = Random.Range(200, 1600); | |
WWW www = new WWW("https://www.placecage.com/"+randomDivensionx+"/"+randomDivensiony); | |
yield return www; | |
plane.GetComponent<Renderer>().material.SetTexture("_MainTex", www.texture); | |
} | |
IEnumerator Example() | |
{ | |
yield return new WaitForSeconds(2); | |
yield return null; // skip frame, continue execution next frame (eg loop once per frame) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment