You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
136 lines
4.8 KiB
C#
136 lines
4.8 KiB
C#
//Shady
|
|
using UnityEngine;
|
|
using Newtonsoft.Json;
|
|
using UnityEngine.Networking;
|
|
using System.Threading.Tasks;
|
|
using System.Collections.Generic;
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
public static class APIRequestHandler
|
|
{
|
|
/// <summary>
|
|
/// Method requests given URL and returns as Object of given Type T.
|
|
/// </summary>
|
|
/// <param name="uri">The URL to request.</param>
|
|
/// <returns>T</returns>
|
|
public static async Task<T> GetRequest<T>(string uri)
|
|
{
|
|
using UnityWebRequest webRequest = UnityWebRequest.Get(uri);
|
|
Debug.Log($"Requesting URL {uri}");
|
|
webRequest.SendWebRequest();
|
|
|
|
while(!webRequest.isDone)
|
|
{
|
|
if(!Application.isPlaying)
|
|
{
|
|
Debug.LogWarning("Request sent while Application is not playing!");
|
|
return default;
|
|
}//if end
|
|
|
|
await Task.Yield();
|
|
}//loop end
|
|
|
|
string[] pages = uri.Split('/');
|
|
int page = pages.Length - 1;
|
|
|
|
switch (webRequest.result)
|
|
{
|
|
case UnityWebRequest.Result.ConnectionError:
|
|
case UnityWebRequest.Result.DataProcessingError:
|
|
Debug.LogError(pages[page] + ": Error: " + webRequest.error);
|
|
break;
|
|
case UnityWebRequest.Result.ProtocolError:
|
|
Debug.LogError(pages[page] + ": HTTP Error: " + webRequest.error);
|
|
break;
|
|
case UnityWebRequest.Result.Success:
|
|
T temp = default;
|
|
Debug.Log(pages[page] + ":\nReceived: " + webRequest.downloadHandler.text);
|
|
return temp = JsonConvert.DeserializeObject<T>(webRequest.downloadHandler.text);
|
|
}//switch end
|
|
return default;
|
|
}//GetRequest() end
|
|
|
|
/// <summary>
|
|
/// Method requests given URL and returns as a List Objects of given Type T.
|
|
/// </summary>
|
|
/// <param name="uri">The URL to request.</param>
|
|
/// <returns>List<T></returns>
|
|
public static async Task<List<T>> GetRequestList<T>(string uri)
|
|
{
|
|
using UnityWebRequest webRequest = UnityWebRequest.Get(uri);
|
|
Debug.Log($"Requesting URL {uri}");
|
|
webRequest.SendWebRequest();
|
|
|
|
while(!webRequest.isDone)
|
|
{
|
|
if(!Application.isPlaying)
|
|
{
|
|
Debug.LogWarning("Request sent while Application is not playing!");
|
|
return null;
|
|
}//if end
|
|
|
|
await Task.Yield();
|
|
}//loop end
|
|
|
|
string[] pages = uri.Split('/');
|
|
int page = pages.Length - 1;
|
|
|
|
switch (webRequest.result)
|
|
{
|
|
case UnityWebRequest.Result.ConnectionError:
|
|
case UnityWebRequest.Result.DataProcessingError:
|
|
Debug.LogError(pages[page] + ": Error: " + webRequest.error);
|
|
return null;
|
|
case UnityWebRequest.Result.ProtocolError:
|
|
Debug.LogError(pages[page] + ": HTTP Error: " + webRequest.error);
|
|
break;
|
|
case UnityWebRequest.Result.Success:
|
|
Debug.Log(pages[page] + ":\nReceived: " + webRequest.downloadHandler.text);
|
|
|
|
// SerializableList<T> dataLoader = JsonUtility.FromJson<SerializableList<T>>(webRequest.downloadHandler.text);
|
|
SerializableList<T> dataLoader = JsonConvert.DeserializeObject<SerializableList<T>>(webRequest.downloadHandler.text);
|
|
return dataLoader.list;
|
|
}//switch end
|
|
return null;
|
|
}//GetRequestList() end
|
|
|
|
/// <summary>
|
|
/// Method requests given URL and return downloaded media as Sprite.
|
|
/// </summary>
|
|
/// <param name="uri">The URL to get media from.</param>
|
|
/// <returns>Sprite</returns>
|
|
public static async Task<Sprite> GetMedia(string uri)
|
|
{
|
|
Debug.Log($"Requesting URL {uri}");
|
|
UnityWebRequest webRequest = UnityWebRequestTexture.GetTexture(uri);
|
|
webRequest.SendWebRequest();
|
|
|
|
while(!webRequest.isDone)
|
|
{
|
|
if(!Application.isPlaying)
|
|
{
|
|
Debug.LogWarning("Request sent while Application is not playing!");
|
|
return null;
|
|
}//if end
|
|
|
|
await Task.Yield();
|
|
}//loop end
|
|
|
|
switch (webRequest.result)
|
|
{
|
|
case UnityWebRequest.Result.ConnectionError:
|
|
case UnityWebRequest.Result.DataProcessingError:
|
|
Debug.LogError(": Error: " + webRequest.error);
|
|
break;
|
|
case UnityWebRequest.Result.ProtocolError:
|
|
Debug.LogError(": HTTP Error: " + webRequest.error);
|
|
break;
|
|
case UnityWebRequest.Result.Success:
|
|
Texture2D tex = ((DownloadHandlerTexture)webRequest.downloadHandler).texture;
|
|
return Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), Vector2.zero);
|
|
}//switch end
|
|
return null;
|
|
}//GetMedia() end
|
|
|
|
}//class end
|