I am trying to show a simple image on a Unity Editor window, it worked for a while and then I guess I made some changes that broke it and I keep getting a NullReferenceException. Here is my simple example:
public class ShowImagePreview : EditorWindow
{
private Texture2D texture;
private async Task<Texture2D> GetHTTPTextureAsync(string imageUrl)
{
UnityWebRequest request = UnityWebRequestTexture.GetTexture(imageUrl);
await request.SendWebRequest();
if (request.isNetworkError || request.isHttpError)
{
Debug.Log("Something went wrong!");
Debug.Log(request.error);
return null;
}
return ((DownloadHandlerTexture)request.downloadHandler).texture;
}
private async Task OnGUI()
{
GUILayout.BeginHorizontal("box");
if (texture != null)
{
GUILayout.Label("Texture is not null!");
GUILayout.Box(texture, GUILayout.Width(140), GUILayout.Height(100));
}
else
{
GUILayout.Label("Texture is NULL!");
try
{
// This is what's causing the issue and keeping the texture value to null
texture = await GetHTTPTextureAsync("https://nikkorpon.files.wordpress.com/2011/02/random-shots-059.jpg");
}
catch (Exception e)
{
Debug.Log(e);
}
}
GUILayout.EndHorizontal();
}
}
This is an extract from a larger script. The image urls are actually dynamic but for the sake of this example I just grabbed a random image url. I am using the Async/Await plugin which works as expected with any other request. Any idea about what I might be doing wrong ?
Here is the error message in full:
System.NullReferenceException: Object reference not set to an instance of an object
at IEnumeratorAwaitExtensions.RunOnUnityScheduler (System.Action action) [0x00042] in /Users/user_name/Projects/project_name/Assets/Plugins/AsyncAwaitUtil/Source/IEnumeratorAwaitExtensions.cs:128
at IEnumeratorAwaitExtensions.GetAwaiterReturnSelf[T] (T instruction) [0x00025] in /Users/user_name/Projects/project_name/Assets/Plugins/AsyncAwaitUtil/Source/IEnumeratorAwaitExtensions.cs:115
at IEnumeratorAwaitExtensions.GetAwaiter (UnityEngine.AsyncOperation instruction) [0x00002] in /Users/user_name/Projects/project_name/Assets/Plugins/AsyncAwaitUtil/Source/IEnumeratorAwaitExtensions.cs:55
at ShowImagePreview+<GetHTTPTextureAsync>c__async0.MoveNext () [0x00033] in /Users/user_name/Projects/project_name/Assets/Resources/Scripts/Import.cs:18
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in <f2e6809acb14476a81f399aeb800f8f2>:0
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x0003e] in <f2e6809acb14476a81f399aeb800f8f2>:0
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x00028] in <f2e6809acb14476a81f399aeb800f8f2>:0
at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x00008] in <f2e6809acb14476a81f399aeb800f8f2>:0
at System.Runtime.CompilerServices.TaskAwaiter`1[TResult].GetResult () [0x00000] in <f2e6809acb14476a81f399aeb800f8f2>:0
at ShowImagePreview+<OnGUI>c__async1.MoveNext () [0x0010b] in /Users/user_name/Projects/project_name/Assets/Resources/Scripts/Import.cs:43
UnityEngine.Debug:Log(Object)
<OnGUI>c__async1:MoveNext() (at Assets/Resources/Scripts/Import.cs:47)
System.Runtime.CompilerServices.AsyncTaskMethodBuilder:Start(<OnGUI>c__async1&)
ShowImagePreview:OnGUI()
UnityEngine.GUIUtility:ProcessEvent(Int32, IntPtr)