Created
June 20, 2016 23:04
-
-
Save atraver/c008367b0f468843e545daf9f50c15d4 to your computer and use it in GitHub Desktop.
UnityWebRequest Dispose example
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
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); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A few things to note here:
BuildRequestDelegate
is just a normal C# delegate around a call tonew UnityWebRequest()
. We do that just to enable a "Retry" button in an alert popup (shown inDisplayErrorMessage()
) to re-issue the entire request if the initial request fails. The implicit call toUnityWebRequest#Dispose
happens by virtue of wrapping the entire block in theusing
statement, and we invokecallback
with a typed response delegate (e.g., if we're requesting the player's name, theResponseDelegate
used for that callback might be simply a class that has astring
property calledName
).I haven't tried invoking the callback outside of
UnityWebRequest
'susing
block, but maybe there's something to that? I'm not sure.