Skip to content

Commit

Permalink
Make Unity3dAsyncAwaitUtil usable in Editor
Browse files Browse the repository at this point in the history
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
  • Loading branch information
AwesomesauceLabs committed Nov 3, 2019
1 parent 5f948fd commit 5cc7c3c
Showing 1 changed file with 7 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,17 @@
using System.Threading.Tasks;
using UnityEngine;

#if UNITY_EDITOR
using UnityEditor;
#endif

namespace UnityAsyncAwaitUtil
{
public static class SyncContextUtil
{
#if UNITY_EDITOR
[InitializeOnLoadMethod()]
#endif
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
static void Install()
{
Expand Down

0 comments on commit 5cc7c3c

Please sign in to comment.