From 5cc7c3c38fd3b66ec9e5fdfb0006c1ca8641ff30 Mon Sep 17 00:00:00 2001 From: Awesomesauce Labs Date: Sun, 3 Nov 2019 14:12:48 -0600 Subject: [PATCH] Make Unity3dAsyncAwaitUtil usable in Editor As per modesttree/Unity3dAsyncAwaitUtil/#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/#9 --- .../AsyncAwaitUtil/Source/Internal/SyncContextUtil.cs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/UnityProject/Assets/Plugins/AsyncAwaitUtil/Source/Internal/SyncContextUtil.cs b/UnityProject/Assets/Plugins/AsyncAwaitUtil/Source/Internal/SyncContextUtil.cs index 3d505a4..22bf8cd 100644 --- a/UnityProject/Assets/Plugins/AsyncAwaitUtil/Source/Internal/SyncContextUtil.cs +++ b/UnityProject/Assets/Plugins/AsyncAwaitUtil/Source/Internal/SyncContextUtil.cs @@ -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() {