-
Notifications
You must be signed in to change notification settings - Fork 119
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cannot use for Editor unit testing, as SyncContextUtil.Install is not called except at runtime #9
Comments
We should probably change it to use the |
Since I'm not sure that runs before awake. But we could use both attributes in that case too |
Unity also doesn't support executing the async in the editor as well, you'll also need a pump. See https://forum.unity.com/threads/async-await-in-editor-script.481276/ |
Here's an modified version of @svermeulen's project that I included in another project. I've been meaning to open pull requests to add this stuff back but I've been pretty busy.
|
@StephenHodgson Thank you for this -- I was unable to find documentation of this after quite a bit of searching Unity's forums. Never occurred to me to just build my project and see that my async worked, and of course it did. The code you shared makes the editor work, too. Can this technique also be used to use the thread pool to run non-UI async tasks that don't need to access the Unity API? Does anyone know if there is official Unity documentation on this anywhere? |
Hi,
I've been experimenting with this a bit and async works as expected.
However, I found it was of limited use since you can't touch any part of
the Unity API, and most of the stuff I wanted to do ended up needing to go
through the API. Depending on what you want it for, I'd recommend looking
at the Unity Job System instead.
Owen
…On Sat, 22 Sep 2018 at 03:21, anticrisis ***@***.***> wrote:
@StephenHodgson <https://github.com/StephenHodgson> Thank you for this --
I was unable to find documentation of this after quite a bit of searching
Unity's forums. Never occurred to me to just build my project and see that
my async worked, and of course it did. The code you shared makes the editor
work, too.
Can this technique also be used to use the thread pool to run non-UI async
tasks that don't need to access the Unity API?
Does anyone know if there is official Unity documentation on this anywhere?
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<https://github.com/svermeulen/Unity3dAsyncAwaitUtil/issues/9#issuecomment-423628689>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAao1NCG0BzPVpVzjNsck0ipWXtmiU75ks5udS4wgaJpZM4WcPzU>
.
|
@jokigenki You should be able to use the unity api with the async approach provided here. Assuming you don't use the multithreading awaiters then it should all remain on the same thread. It should be able to be used in a very similar way to coroutines except a much cleaner syntax and some more features |
As per modesttree/Unity3dAsyncAwaitUtil/modesttree#9, using Unity3dAsyncAwaitUtil in Editor scripts causes an NPE, because `SyncContextUtil.Install` is never called to initialize global variables `SyncContextUtil.UnitySynchronizationContext` and `SyncContextUtil.UnityThreadId`. Fix this by adding the `[InitializeOnLoadMethod()]` attribute to the `SyncContextUtil.Install` method. Note that the existing `[RuntimeInitializeOnLoadMethod]` attribute is still needed in order for `SyncContextUtil.Install` to be invoked when entering Play Mode. Fixes modesttree/Unity3dAsyncAwaitUtil/modesttree#9
As per modesttree/Unity3dAsyncAwaitUtil/modesttree#9, using Unity3dAsyncAwaitUtil in Editor scripts causes an NPE, because `SyncContextUtil.Install` is never called to initialize global variables `SyncContextUtil.UnitySynchronizationContext` and `SyncContextUtil.UnityThreadId`. Fix this by adding the `[InitializeOnLoadMethod()]` attribute to the `SyncContextUtil.Install` method. Note that the existing `[RuntimeInitializeOnLoadMethod]` attribute is still needed in order for `SyncContextUtil.Install` to be invoked when entering Play Mode. Fixes modesttree/Unity3dAsyncAwaitUtil/modesttree#9
As per modesttree/Unity3dAsyncAwaitUtil/modesttree#9, using Unity3dAsyncAwaitUtil in Editor scripts causes an NPE, because `SyncContextUtil.Install` is never called to initialize global variables `SyncContextUtil.UnitySynchronizationContext` and `SyncContextUtil.UnityThreadId`. Fix this by adding the `[InitializeOnLoadMethod()]` attribute to the `SyncContextUtil.Install` method. Note that the existing `[RuntimeInitializeOnLoadMethod]` attribute is still needed in order for `SyncContextUtil.Install` to be invoked when entering Play Mode. Fixes modesttree/Unity3dAsyncAwaitUtil/modesttree#9
As described modesttree#9, using Unity3dAsyncAwaitUtil in Editor scripts causes an NPE, because `SyncContextUtil.Install` is never called to initialize global variables `SyncContextUtil.UnitySynchronizationContext` and `SyncContextUtil.UnityThreadId`. Fix this by adding the `[InitializeOnLoadMethod()]` attribute to the `SyncContextUtil.Install` method. Note that the existing `[RuntimeInitializeOnLoadMethod]` attribute is still needed in order for `SyncContextUtil.Install` to be correctly invoked in Play Mode. Fixes modesttree#9
Using @StephenHodgson's solution from modesttree#9 (comment)
If the package com.unity.editorcoroutine is installed, AsyncCoroutineRunner gains the ability to work at edit time. In Unity 2019.1 and above, no additional setup is needed. Otherwise, the symbol HAVE_EDITOR_COROUTINES must also be manually defined. Includes unit test. Addresses part of modesttree#9
Includes sample unit test showing that "await" works at edit time. Fixes modesttree#9
I hacked this by making SyncContextUtil.Install public and calling it in my tests SetUp method, but I don't know if there's a better way?
The text was updated successfully, but these errors were encountered: