Last active
February 5, 2016 21:10
-
-
Save reidblomquist/4d9bc4f307ac81e53d0c to your computer and use it in GitHub Desktop.
U3D Async Json Serialization to Object
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 UnityEngine; | |
| using System; | |
| public class ApiBackgroundColor : MonoBehaviour { | |
| private Color backgroundColor; | |
| private ApiClient.Rgba rgba; | |
| private Camera camera; | |
| void Awake() | |
| { | |
| camera = GetComponent<Camera>(); | |
| } | |
| void LateUpdate () { | |
| try | |
| { | |
| rgba = ApiClient.instance.currentRgba; | |
| backgroundColor = new Color(rgba.R/255, rgba.G/255, rgba.B/255, rgba.A); | |
| camera.backgroundColor = backgroundColor; | |
| } | |
| catch (Exception e) | |
| { | |
| print(e); | |
| } | |
| } | |
| } |
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 UnityEngine; | |
| using System; | |
| using System.Collections; | |
| public class ApiClient : MonoBehaviour | |
| { | |
| #region Persistance | |
| public static ApiClient instance = null; | |
| void Awake() | |
| { | |
| if (instance == null) | |
| instance = this; | |
| else if (instance != this) | |
| Destroy(gameObject); | |
| DontDestroyOnLoad(gameObject); | |
| } | |
| #endregion | |
| // Create Rgba struct to serialize api request into | |
| // Run route requests via coroutine to ensure we don't block | |
| // the application's main thread. | |
| #region Rgba | |
| [System.Serializable] | |
| public class Rgba | |
| { | |
| public float R; | |
| public float G; | |
| public float B; | |
| public float A; | |
| } | |
| public Rgba currentRgba; | |
| private string rgbaUrl = "http://reidblomquist.com:6969/rgba"; | |
| private bool freshRgba = false; | |
| IEnumerator RgbaCoroutine() | |
| { | |
| while (freshRgba) | |
| { | |
| WWW www = new WWW(rgbaUrl); | |
| yield return www; | |
| if (www.error == null) | |
| { | |
| currentRgba = JsonUtility.FromJson<Rgba>(www.text); | |
| } | |
| else | |
| { | |
| print("failed json request moar info: " + www.text); | |
| } | |
| } | |
| } | |
| public void StopRgbaCoroutine() | |
| { | |
| freshRgba = false; | |
| try | |
| { | |
| StopCoroutine(RgbaCoroutine()); | |
| } | |
| catch (Exception e) | |
| { | |
| print(e); | |
| } | |
| } | |
| #endregion | |
| void Start() | |
| { | |
| if (freshRgba == false) | |
| { | |
| try | |
| { | |
| freshRgba = true; | |
| StartCoroutine(RgbaCoroutine()); | |
| } | |
| catch (Exception e) | |
| { | |
| print(e); | |
| } | |
| } | |
| } | |
| void OnApplicationQuit() | |
| { | |
| StopRgbaCoroutine(); | |
| } | |
| } |
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 UnityEngine; | |
| using System.Collections; | |
| public class Loader : MonoBehaviour { | |
| public GameObject apiClient; | |
| void Awake () { | |
| if (apiClient && ApiClient.instance == null) | |
| Instantiate(apiClient); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment