Advertisement
napland

WWW basic request helper example

Aug 9th, 2017
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.32 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.Events;
  3. using System.Collections;
  4.  
  5. public class WWWHelper : MonoBehaviour // Must inherit from Monobehaviour to run coroutines
  6. {
  7.     private static WWWHelper _instance; // there must be an instance of the class.
  8.                                         // since it derives from MonoBehaviour it must also be added to a gameObjct as a component.
  9.  
  10.     private const float TIMEOUT = 5f; // a timeout if we need it.
  11.  
  12.     public const string TIMEOUT_RESPONSE = "TIMEOUT";
  13.     public const string ERROR_PREFIX = "ERROR - ";
  14.  
  15.     // convenience method for easy access
  16.     public static void MakeRequest(string url, UnityAction<string> callback)
  17.     {
  18.         if (_instance == null)
  19.         {
  20.             // create a game object to put this script on
  21.             GameObject go = new GameObject("WWWHelper");
  22.  
  23.             // add this script to it and store it in _instance
  24.             _instance = go.AddComponent<WWWHelper>();
  25.         }
  26.  
  27.         _instance.StartCoroutine(_instance.RequestCoroutine(url, callback));
  28.     }
  29.  
  30.     // Since coroutines cannot return a value, we usually use callbacks / delegates / actions
  31.     private IEnumerator RequestCoroutine(string url, UnityAction<string> callback)
  32.     {
  33.         WWW request = new WWW(url);
  34.  
  35.         float timer = 0;
  36.  
  37.         while (timer < TIMEOUT)
  38.         {
  39.             timer += Time.deltaTime; // increment our timer every frame
  40.  
  41.             if (request.isDone) // if the request is done, no need to wait
  42.                 break;
  43.  
  44.             yield return new WaitForEndOfFrame(); // wait for the frame to complete before continuing this loop
  45.         }
  46.  
  47.         string response = TIMEOUT_RESPONSE;
  48.  
  49.         if (request.isDone)
  50.         {
  51.             if (!string.IsNullOrEmpty(request.error))
  52.                 response = request.error;
  53.             else
  54.                 response = request.text;
  55.         }
  56.  
  57.         if (callback != null)
  58.             callback.Invoke(response); // finally call the callback so we can hear about the response.
  59.     }
  60. }
  61.  
  62. public class Example : MonoBehaviour
  63. {
  64.     private void Start()
  65.     {
  66.         WWWHelper.MakeRequest("www.naplandgames.com", HandleResponse);
  67.     }
  68.  
  69.     private void HandleResponse(string response)
  70.     {
  71.         if (response == WWWHelper.TIMEOUT_RESPONSE)
  72.         {
  73.             Debug.LogError("Hey user, the response timed out, your internet sucks");
  74.         }
  75.         else if (response.Contains(WWWHelper.ERROR_PREFIX))
  76.         {
  77.             Debug.LogError("Hey developer, your requested URL stunk, here's the error: " + response);
  78.         }
  79.         else
  80.         {
  81.             Debug.Log("Hey user, here's the response: " + response);
  82.         }
  83.     }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement