Skip to content

Instantly share code, notes, and snippets.

@atraver
Created June 20, 2016 23:04
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save atraver/c008367b0f468843e545daf9f50c15d4 to your computer and use it in GitHub Desktop.
UnityWebRequest Dispose example
private IEnumerator ExecuteRequest<T>(BuildRequestDelegate builder, ResponseDelegate<T> callback,
Action cancelCallback, RequestOptions requestOptions)
{
using(UnityWebRequest serverRequest = builder())
{
bool showActivityIndicator = ((requestOptions & RequestOptions.ShowActivityIndicator) == RequestOptions.ShowActivityIndicator);
bool forceRetry = ((requestOptions & RequestOptions.ForceRetry) == RequestOptions.ForceRetry);
float startTime = Time.realtimeSinceStartup;
yield return serverRequest.Send();
this.LastResponseTime = (int)((Time.realtimeSinceStartup - startTime) * 1000);
if(serverRequest.isError)
{
LogError(serverRequest.error);
DisplayErrorMessage("Communication Error", serverRequest.error, true, () => {
StartCoroutine(ExecuteRequest(builder, callback, null, requestOptions));
}, null);
yield break;
}
else
{
string responseString = serverRequest.downloadHandler.text;
if((requestOptions & RequestOptions.SuppressLogging) != RequestOptions.SuppressLogging)
{
Log("API response: {0}", responseString);
}
try
{
Dictionary<string, JToken> response = JsonConvert.DeserializeObject<Dictionary<string, JToken>>(responseString);
if(response.ContainsKey("error"))
{
LogErrorWithoutFormatting(string.Join("\n", response["error"].Values<string>().ToArray()));
DisplayErrorMessage("Server Error", response["message"].ToString(), forceRetry, () => {
StartCoroutine(ExecuteRequest(builder, callback, cancelCallback, requestOptions));
}, cancelCallback);
}
else
{
T result = response["data"].ToObject<T>();
if(callback != null)
{
callback(result);
}
}
}
catch(JsonReaderException e)
{
LogError("Error reading response data. Possibly malformed data: {0}", e.Message);
DisplayErrorMessage("Server Error", e.Message, true, () => {
StartCoroutine(ExecuteRequest(builder, callback, cancelCallback, requestOptions));
}, cancelCallback);
}
}
}
}
@atraver
Copy link
Author

atraver commented Jun 20, 2016

A few things to note here: BuildRequestDelegate is just a normal C# delegate around a call to new UnityWebRequest(). We do that just to enable a "Retry" button in an alert popup (shown in DisplayErrorMessage()) to re-issue the entire request if the initial request fails. The implicit call to UnityWebRequest#Dispose happens by virtue of wrapping the entire block in the using statement, and we invoke callback with a typed response delegate (e.g., if we're requesting the player's name, the ResponseDelegate used for that callback might be simply a class that has a string property called Name).

I haven't tried invoking the callback outside of UnityWebRequest's using block, but maybe there's something to that? I'm not sure.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment