Last active
December 11, 2017 11:04
-
-
Save grifdail/1ef645a05b95995aeb4c127f47140b5e to your computer and use it in GitHub Desktop.
Unity Deferred
This file contains 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
public class AsyncError { | |
public string Message { get; private set; } | |
public AsyncError(string _message) { | |
Message = _message; | |
} | |
} |
This file contains 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 System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class ExempleDeferred : MonoBehaviour { | |
public int Pokemon1 = 1; | |
public int Pokemon2 = 2; | |
private void OnGUI() { | |
if (GUI.Button(new Rect(200,0,200,100),"calc biggest pokemon")) { | |
StartCoroutine(GetBiggestPokemon(Pokemon1, Pokemon2)); | |
} | |
} | |
IEnumerator GetBiggestPokemon(int id1, int id2) { | |
Debug.Log("hello"); | |
Deferred<PokemonData> firstPokemon = new Deferred<PokemonData>(); | |
yield return GetPokemon(id1, firstPokemon); | |
if (firstPokemon.Error != null) { | |
Debug.LogError(firstPokemon.Error.Message); | |
yield break; | |
} | |
Debug.Log("First pokemon is " + firstPokemon.Value.name); | |
Deferred<PokemonData> secondPokemon = new Deferred<PokemonData>(); | |
yield return GetPokemon(id2, secondPokemon); | |
if (secondPokemon.Error != null) { | |
Debug.LogError(secondPokemon.Error.Message); | |
yield break; | |
} | |
Debug.Log("Second pokemon is " + secondPokemon.Value.name); | |
PokemonData biggest = firstPokemon.Value.weight > secondPokemon.Value.weight ? firstPokemon.Value : secondPokemon.Value; | |
Debug.Log("Biggest pokemon is " + biggest.name); | |
} | |
// Make a request to the pokemon api, parse it and resolve with a PokemonData | |
IEnumerator GetPokemon(int id, Deferred<PokemonData> _pokemonDefered) { | |
Deferred<string> apiRequest = new Deferred<string>(); | |
yield return RequestCoroutine("http://pokeapi.co/api/v2/pokemon/"+id, apiRequest); | |
if (apiRequest.Error != null) { | |
_pokemonDefered.Catch(apiRequest.Error); | |
yield break; | |
} | |
_pokemonDefered.Resolve(JsonUtility.FromJson<PokemonData>(apiRequest.Value)); | |
} | |
IEnumerator RequestCoroutine(string url, Deferred<string> _deferred) { | |
WWW request = new WWW(url); | |
yield return request; | |
if (request.isDone && request.error == null) { | |
_deferred.Resolve(request.text); | |
} else { | |
_deferred.Catch(request.error); | |
} | |
} | |
class Deferred<T> { | |
public T Value { get; private set; } | |
public bool IsCompleted { get; private set; } | |
public AsyncError Error { get; private set; } | |
public void Resolve(T _value) { | |
Value = _value; | |
IsCompleted = true; | |
} | |
public void Catch(AsyncError _err) { | |
Error = _err; | |
IsCompleted = true; | |
} | |
public void Catch(string _message) { | |
Catch(new AsyncError(_message)); | |
} | |
} | |
} |
This file contains 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
[System.Serializable] | |
public class PokemonData { | |
public string name; | |
public int height; | |
public float weight; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment