Overall, I’m pretty happy with the UnityWebRequest API for sending and receiving RESTful HTTP requests. One issue I’m running into, however, is using it to access a web API at edit-time (say, in a custom editor tool).
For my use case, we want to convert some ScriptableObject assets into JSON and send them to our server through a REST API using POST.
UnityWebRequest.Send() returns an AsyncOperation, which you can “yield on” in a Coroutine to asynchronously retrieve the result. However, there’s no way to run Coroutines in an editor tool, correct? So, would I just need to block until the AsyncOperation returned from Send() says it’s done? But would it ever say it’s done if I blocked like that? Do I need to send the request from a separate thread in this case?
It seems like the UnityWebRequest could easily support an asynchronous callback mechanism (perhaps the Send() call has an override that takes a callback, which is called when the request completes). But barring that, I’m not aware of an easy/effective way to make use of an AsyncOperation API in the editor. Any suggestions?
As an alternative, I’m looking at the C# HttpWebRequest class, buuuut it would be cool if the UnityWebRequest could work.
There is a bug introduced in Unity 5.5 where UnityWebRequests calls will not work on Editor (except if the editor is on Play mode, as in, Playing an scene or game).
Is reported as (Case 858932) UnityWebRequest on Editor only works on Play Mode.
However more than a week passed since the report, and no news from Unity yet.
@erich202 they have merged the issue with another one and closed my report. Is supossedly being worked. I have been updating on this issue here: Google Sheets For Unity
I have a webrequest attached to a button in an EditorWindow, it doesnt log errors but always return an empty string.
If I go int play mode before pressing the button, then it works fine.
Update:
If you put the attribute [UnityEngine.ExecuteInEditMode] on top of your EditorWindow class it will make it work. I’m just not sure if I want to have my script run in edit mode all the time.
@tcz8 heya, thanks for the link to the report. I have long moved to later Unity releases. Right now using 2018.3.2f1 and it works right on the editor using the EditorApplication.update method described in the blog.
I’ve tried the two methods above, and I recommand Editor Corutines Utility. Because somtimes the first method doesn’t work. And I can’t find why ? that’s sad.