support disable domain reload

When domain reload is disabled, re-initialization is required when entering play mode; otherwise, pending tasks will leak between play mode sessions.
master
Artyom Sovetnikov 2020-02-18 17:06:19 +03:00
parent 50ba93f951
commit ecd678117e
1 changed files with 13 additions and 3 deletions

View File

@ -96,21 +96,31 @@ namespace UniRx.Async
#endif #endif
}; };
var dest = new PlayerLoopSystem[loopSystem.subSystemList.Length + 2]; var source = loopSystem.subSystemList // Remove items form previous initializations.
Array.Copy(loopSystem.subSystemList, 0, dest, 2, loopSystem.subSystemList.Length); .Where(ls => ls.type != loopRunnerYieldType && ls.type != loopRunnerType).ToArray();
var dest = new PlayerLoopSystem[source.Length + 2];
Array.Copy(source, 0, dest, 2, source.Length);
dest[0] = yieldLoop; dest[0] = yieldLoop;
dest[1] = runnerLoop; dest[1] = runnerLoop;
return dest; return dest;
} }
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)] [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
static void Init() static void Init ()
{ {
// capture default(unity) sync-context. // capture default(unity) sync-context.
unitySynchronizationContetext = SynchronizationContext.Current; unitySynchronizationContetext = SynchronizationContext.Current;
mainThreadId = Thread.CurrentThread.ManagedThreadId; mainThreadId = Thread.CurrentThread.ManagedThreadId;
#if UNITY_EDITOR && UNITY_2019_3_OR_NEWER
// When domain reload is disabled, re-initialization is required when entering play mode;
// otherwise, pending tasks will leak between play mode sessions.
var domainReloadDisabled = UnityEditor.EditorSettings.enterPlayModeOptionsEnabled &&
UnityEditor.EditorSettings.enterPlayModeOptions.HasFlag(UnityEditor.EnterPlayModeOptions.DisableDomainReload);
if (!domainReloadDisabled && runners != null) return;
#else
if (runners != null) return; // already initialized if (runners != null) return; // already initialized
#endif
var playerLoop = var playerLoop =
#if UNITY_2019_3_OR_NEWER #if UNITY_2019_3_OR_NEWER