2019-05-19 23:14:47 +08:00
|
|
|
|
#if CSHARP_7_OR_LATER || (UNITY_2018_3_OR_NEWER && (NET_STANDARD_2_0 || NET_4_6))
|
|
|
|
|
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
using UniRx.Async.Internal;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
|
|
|
|
|
#if UNITY_2019_3_OR_NEWER
|
|
|
|
|
using UnityEngine.LowLevel;
|
|
|
|
|
#else
|
|
|
|
|
using UnityEngine.Experimental.LowLevel;
|
|
|
|
|
#endif
|
|
|
|
|
|
2019-09-09 19:07:56 +08:00
|
|
|
|
#if UNITY_EDITOR
|
|
|
|
|
using UnityEditor;
|
|
|
|
|
#endif
|
|
|
|
|
|
2019-05-19 23:14:47 +08:00
|
|
|
|
namespace UniRx.Async
|
|
|
|
|
{
|
|
|
|
|
public static class UniTaskLoopRunners
|
|
|
|
|
{
|
|
|
|
|
public struct UniTaskLoopRunnerInitialization { };
|
|
|
|
|
public struct UniTaskLoopRunnerEarlyUpdate { };
|
|
|
|
|
public struct UniTaskLoopRunnerFixedUpdate { };
|
|
|
|
|
public struct UniTaskLoopRunnerPreUpdate { };
|
|
|
|
|
public struct UniTaskLoopRunnerUpdate { };
|
|
|
|
|
public struct UniTaskLoopRunnerPreLateUpdate { };
|
|
|
|
|
public struct UniTaskLoopRunnerPostLateUpdate { };
|
|
|
|
|
|
2020-05-05 04:22:49 +08:00
|
|
|
|
// Last
|
|
|
|
|
|
|
|
|
|
public struct UniTaskLoopRunnerLastInitialization { };
|
|
|
|
|
public struct UniTaskLoopRunnerLastEarlyUpdate { };
|
|
|
|
|
public struct UniTaskLoopRunnerLastFixedUpdate { };
|
|
|
|
|
public struct UniTaskLoopRunnerLastPreUpdate { };
|
|
|
|
|
public struct UniTaskLoopRunnerLastUpdate { };
|
|
|
|
|
public struct UniTaskLoopRunnerLastPreLateUpdate { };
|
|
|
|
|
public struct UniTaskLoopRunnerLastPostLateUpdate { };
|
|
|
|
|
|
2019-05-19 23:14:47 +08:00
|
|
|
|
// Yield
|
|
|
|
|
|
|
|
|
|
public struct UniTaskLoopRunnerYieldInitialization { };
|
|
|
|
|
public struct UniTaskLoopRunnerYieldEarlyUpdate { };
|
|
|
|
|
public struct UniTaskLoopRunnerYieldFixedUpdate { };
|
|
|
|
|
public struct UniTaskLoopRunnerYieldPreUpdate { };
|
|
|
|
|
public struct UniTaskLoopRunnerYieldUpdate { };
|
|
|
|
|
public struct UniTaskLoopRunnerYieldPreLateUpdate { };
|
|
|
|
|
public struct UniTaskLoopRunnerYieldPostLateUpdate { };
|
2020-05-05 04:22:49 +08:00
|
|
|
|
|
|
|
|
|
// Yield Last
|
|
|
|
|
|
|
|
|
|
public struct UniTaskLoopRunnerLastYieldInitialization { };
|
|
|
|
|
public struct UniTaskLoopRunnerLastYieldEarlyUpdate { };
|
|
|
|
|
public struct UniTaskLoopRunnerLastYieldFixedUpdate { };
|
|
|
|
|
public struct UniTaskLoopRunnerLastYieldPreUpdate { };
|
|
|
|
|
public struct UniTaskLoopRunnerLastYieldUpdate { };
|
|
|
|
|
public struct UniTaskLoopRunnerLastYieldPreLateUpdate { };
|
|
|
|
|
public struct UniTaskLoopRunnerLastYieldPostLateUpdate { };
|
2019-05-19 23:14:47 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public enum PlayerLoopTiming
|
|
|
|
|
{
|
|
|
|
|
Initialization = 0,
|
2020-05-05 04:22:49 +08:00
|
|
|
|
LastInitialization = 1,
|
|
|
|
|
|
|
|
|
|
EarlyUpdate = 2,
|
|
|
|
|
LastEarlyUpdate = 3,
|
|
|
|
|
|
|
|
|
|
FixedUpdate = 4,
|
|
|
|
|
LastFixedUpdate = 5,
|
|
|
|
|
|
|
|
|
|
PreUpdate = 6,
|
|
|
|
|
LastPreUpdate = 7,
|
|
|
|
|
|
|
|
|
|
Update = 8,
|
|
|
|
|
LastUpdate = 9,
|
|
|
|
|
|
|
|
|
|
PreLateUpdate = 10,
|
|
|
|
|
LastPreLateUpdate = 11,
|
|
|
|
|
|
|
|
|
|
PostLateUpdate = 12,
|
|
|
|
|
LastPostLateUpdate = 13
|
2019-05-19 23:14:47 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public interface IPlayerLoopItem
|
|
|
|
|
{
|
|
|
|
|
bool MoveNext();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static class PlayerLoopHelper
|
|
|
|
|
{
|
|
|
|
|
public static SynchronizationContext UnitySynchronizationContext => unitySynchronizationContetext;
|
|
|
|
|
public static int MainThreadId => mainThreadId;
|
|
|
|
|
|
|
|
|
|
static int mainThreadId;
|
|
|
|
|
static SynchronizationContext unitySynchronizationContetext;
|
|
|
|
|
static ContinuationQueue[] yielders;
|
|
|
|
|
static PlayerLoopRunner[] runners;
|
|
|
|
|
|
2020-05-05 04:22:49 +08:00
|
|
|
|
static PlayerLoopSystem[] InsertRunner(PlayerLoopSystem loopSystem,
|
|
|
|
|
Type loopRunnerYieldType, ContinuationQueue cq, Type lastLoopRunnerYieldType, ContinuationQueue lastCq,
|
|
|
|
|
Type loopRunnerType, PlayerLoopRunner runner, Type lastLoopRunnerType, PlayerLoopRunner lastRunner)
|
2019-05-19 23:14:47 +08:00
|
|
|
|
{
|
2020-05-05 04:22:49 +08:00
|
|
|
|
|
2020-02-28 02:14:02 +08:00
|
|
|
|
#if UNITY_EDITOR
|
2019-09-09 19:07:56 +08:00
|
|
|
|
EditorApplication.playModeStateChanged += (state) =>
|
|
|
|
|
{
|
2020-05-05 04:22:49 +08:00
|
|
|
|
if (state == PlayModeStateChange.EnteredEditMode || state == PlayModeStateChange.EnteredPlayMode)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-02-28 02:14:02 +08:00
|
|
|
|
|
2019-09-09 19:07:56 +08:00
|
|
|
|
if (runner != null)
|
2020-05-05 04:22:49 +08:00
|
|
|
|
{
|
2019-09-09 19:07:56 +08:00
|
|
|
|
runner.Clear();
|
2020-05-05 04:22:49 +08:00
|
|
|
|
}
|
|
|
|
|
if (lastRunner != null)
|
|
|
|
|
{
|
|
|
|
|
lastRunner.Clear();
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-09 19:07:56 +08:00
|
|
|
|
if (cq != null)
|
2020-05-05 04:22:49 +08:00
|
|
|
|
{
|
2019-09-09 19:07:56 +08:00
|
|
|
|
cq.Clear();
|
2020-05-05 04:22:49 +08:00
|
|
|
|
}
|
|
|
|
|
if (lastCq != null)
|
|
|
|
|
{
|
|
|
|
|
lastCq.Clear();
|
|
|
|
|
}
|
2019-09-09 19:07:56 +08:00
|
|
|
|
};
|
2020-02-28 02:14:02 +08:00
|
|
|
|
#endif
|
2020-05-05 04:22:49 +08:00
|
|
|
|
|
2019-05-19 23:14:47 +08:00
|
|
|
|
var yieldLoop = new PlayerLoopSystem
|
|
|
|
|
{
|
|
|
|
|
type = loopRunnerYieldType,
|
|
|
|
|
updateDelegate = cq.Run
|
|
|
|
|
};
|
|
|
|
|
|
2020-05-05 04:22:49 +08:00
|
|
|
|
var lastYieldLoop = new PlayerLoopSystem
|
|
|
|
|
{
|
|
|
|
|
type = lastLoopRunnerYieldType,
|
|
|
|
|
updateDelegate = lastCq.Run
|
|
|
|
|
};
|
|
|
|
|
|
2019-05-19 23:14:47 +08:00
|
|
|
|
var runnerLoop = new PlayerLoopSystem
|
|
|
|
|
{
|
|
|
|
|
type = loopRunnerType,
|
|
|
|
|
updateDelegate = runner.Run
|
|
|
|
|
};
|
|
|
|
|
|
2020-05-05 04:22:49 +08:00
|
|
|
|
var lastRunnerLoop = new PlayerLoopSystem
|
|
|
|
|
{
|
|
|
|
|
type = lastLoopRunnerType,
|
|
|
|
|
updateDelegate = lastRunner.Run
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Remove items from previous initializations.
|
|
|
|
|
var source = loopSystem.subSystemList
|
|
|
|
|
.Where(ls => ls.type != loopRunnerYieldType && ls.type != loopRunnerType && ls.type != lastLoopRunnerYieldType && ls.type != lastLoopRunnerType)
|
|
|
|
|
.ToArray();
|
|
|
|
|
|
|
|
|
|
var dest = new PlayerLoopSystem[source.Length + 4];
|
|
|
|
|
|
2020-02-18 22:06:19 +08:00
|
|
|
|
Array.Copy(source, 0, dest, 2, source.Length);
|
2019-05-19 23:14:47 +08:00
|
|
|
|
dest[0] = yieldLoop;
|
|
|
|
|
dest[1] = runnerLoop;
|
2020-05-05 04:22:49 +08:00
|
|
|
|
dest[dest.Length - 2] = lastYieldLoop;
|
|
|
|
|
dest[dest.Length - 1] = lastRunnerLoop;
|
|
|
|
|
|
2019-05-19 23:14:47 +08:00
|
|
|
|
return dest;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
|
2020-02-18 22:19:10 +08:00
|
|
|
|
static void Init()
|
2019-05-19 23:14:47 +08:00
|
|
|
|
{
|
|
|
|
|
// capture default(unity) sync-context.
|
|
|
|
|
unitySynchronizationContetext = SynchronizationContext.Current;
|
|
|
|
|
mainThreadId = Thread.CurrentThread.ManagedThreadId;
|
|
|
|
|
|
2020-02-18 22:06:19 +08:00
|
|
|
|
#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
|
2019-05-19 23:14:47 +08:00
|
|
|
|
if (runners != null) return; // already initialized
|
2020-02-18 22:06:19 +08:00
|
|
|
|
#endif
|
2019-05-19 23:14:47 +08:00
|
|
|
|
|
|
|
|
|
var playerLoop =
|
|
|
|
|
#if UNITY_2019_3_OR_NEWER
|
|
|
|
|
PlayerLoop.GetCurrentPlayerLoop();
|
|
|
|
|
#else
|
|
|
|
|
PlayerLoop.GetDefaultPlayerLoop();
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
Initialize(ref playerLoop);
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-09 19:07:56 +08:00
|
|
|
|
|
|
|
|
|
#if UNITY_EDITOR
|
|
|
|
|
[InitializeOnLoadMethod]
|
|
|
|
|
static void InitOnEditor()
|
|
|
|
|
{
|
2019-09-09 19:34:13 +08:00
|
|
|
|
//Execute the play mode init method
|
2019-09-09 19:07:56 +08:00
|
|
|
|
Init();
|
|
|
|
|
|
2019-09-09 19:34:13 +08:00
|
|
|
|
//register an Editor update delegate, used to forcing playerLoop update
|
2019-09-09 19:07:56 +08:00
|
|
|
|
EditorApplication.update += ForceEditorPlayerLoopUpdate;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void ForceEditorPlayerLoopUpdate()
|
|
|
|
|
{
|
|
|
|
|
if (EditorApplication.isPlayingOrWillChangePlaymode || EditorApplication.isCompiling ||
|
|
|
|
|
EditorApplication.isUpdating)
|
|
|
|
|
{
|
|
|
|
|
// Not in Edit mode, don't interfere
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-09 19:34:13 +08:00
|
|
|
|
//force unity to update PlayerLoop callbacks
|
2019-09-09 19:07:56 +08:00
|
|
|
|
EditorApplication.QueuePlayerLoopUpdate();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
2019-05-19 23:14:47 +08:00
|
|
|
|
public static void Initialize(ref PlayerLoopSystem playerLoop)
|
|
|
|
|
{
|
2020-05-05 04:22:49 +08:00
|
|
|
|
yielders = new ContinuationQueue[14];
|
|
|
|
|
runners = new PlayerLoopRunner[14];
|
2019-05-19 23:14:47 +08:00
|
|
|
|
|
|
|
|
|
var copyList = playerLoop.subSystemList.ToArray();
|
|
|
|
|
|
2020-05-05 04:22:49 +08:00
|
|
|
|
// Initialization
|
|
|
|
|
copyList[0].subSystemList = InsertRunner(copyList[0], typeof(UniTaskLoopRunners.UniTaskLoopRunnerYieldInitialization), yielders[0] = new ContinuationQueue(),
|
|
|
|
|
typeof(UniTaskLoopRunners.UniTaskLoopRunnerLastYieldInitialization), yielders[1] = new ContinuationQueue(),
|
|
|
|
|
typeof(UniTaskLoopRunners.UniTaskLoopRunnerInitialization), runners[1] = new PlayerLoopRunner(),
|
|
|
|
|
typeof(UniTaskLoopRunners.UniTaskLoopRunnerLastInitialization), runners[1] = new PlayerLoopRunner());
|
|
|
|
|
// EarlyUpdate
|
|
|
|
|
copyList[1].subSystemList = InsertRunner(copyList[1], typeof(UniTaskLoopRunners.UniTaskLoopRunnerYieldEarlyUpdate), yielders[2] = new ContinuationQueue(),
|
|
|
|
|
typeof(UniTaskLoopRunners.UniTaskLoopRunnerLastYieldEarlyUpdate), yielders[3] = new ContinuationQueue(),
|
|
|
|
|
typeof(UniTaskLoopRunners.UniTaskLoopRunnerEarlyUpdate), runners[2] = new PlayerLoopRunner(),
|
|
|
|
|
typeof(UniTaskLoopRunners.UniTaskLoopRunnerLastEarlyUpdate), runners[3] = new PlayerLoopRunner());
|
|
|
|
|
// FixedUpdate
|
|
|
|
|
copyList[2].subSystemList = InsertRunner(copyList[2], typeof(UniTaskLoopRunners.UniTaskLoopRunnerYieldFixedUpdate), yielders[4] = new ContinuationQueue(),
|
|
|
|
|
typeof(UniTaskLoopRunners.UniTaskLoopRunnerLastYieldFixedUpdate), yielders[5] = new ContinuationQueue(),
|
|
|
|
|
typeof(UniTaskLoopRunners.UniTaskLoopRunnerFixedUpdate), runners[4] = new PlayerLoopRunner(),
|
|
|
|
|
typeof(UniTaskLoopRunners.UniTaskLoopRunnerLastFixedUpdate), runners[5] = new PlayerLoopRunner());
|
|
|
|
|
// PreUpdate
|
|
|
|
|
copyList[3].subSystemList = InsertRunner(copyList[3], typeof(UniTaskLoopRunners.UniTaskLoopRunnerYieldPreUpdate), yielders[6] = new ContinuationQueue(),
|
|
|
|
|
typeof(UniTaskLoopRunners.UniTaskLoopRunnerLastYieldPreUpdate), yielders[7] = new ContinuationQueue(),
|
|
|
|
|
typeof(UniTaskLoopRunners.UniTaskLoopRunnerPreUpdate), runners[6] = new PlayerLoopRunner(),
|
|
|
|
|
typeof(UniTaskLoopRunners.UniTaskLoopRunnerLastPreUpdate), runners[7] = new PlayerLoopRunner());
|
|
|
|
|
// Update
|
|
|
|
|
copyList[4].subSystemList = InsertRunner(copyList[4], typeof(UniTaskLoopRunners.UniTaskLoopRunnerYieldUpdate), yielders[8] = new ContinuationQueue(),
|
|
|
|
|
typeof(UniTaskLoopRunners.UniTaskLoopRunnerLastYieldUpdate), yielders[9] = new ContinuationQueue(),
|
|
|
|
|
typeof(UniTaskLoopRunners.UniTaskLoopRunnerUpdate), runners[8] = new PlayerLoopRunner(),
|
|
|
|
|
typeof(UniTaskLoopRunners.UniTaskLoopRunnerLastUpdate), runners[9] = new PlayerLoopRunner());
|
|
|
|
|
// PreLateUpdate
|
|
|
|
|
copyList[5].subSystemList = InsertRunner(copyList[5], typeof(UniTaskLoopRunners.UniTaskLoopRunnerYieldPreLateUpdate), yielders[10] = new ContinuationQueue(),
|
|
|
|
|
typeof(UniTaskLoopRunners.UniTaskLoopRunnerLastYieldPreLateUpdate), yielders[11] = new ContinuationQueue(),
|
|
|
|
|
typeof(UniTaskLoopRunners.UniTaskLoopRunnerPreLateUpdate), runners[10] = new PlayerLoopRunner(),
|
|
|
|
|
typeof(UniTaskLoopRunners.UniTaskLoopRunnerLastPreLateUpdate), runners[11] = new PlayerLoopRunner());
|
|
|
|
|
// PostLateUpdate
|
|
|
|
|
copyList[6].subSystemList = InsertRunner(copyList[6], typeof(UniTaskLoopRunners.UniTaskLoopRunnerYieldPostLateUpdate), yielders[12] = new ContinuationQueue(),
|
|
|
|
|
typeof(UniTaskLoopRunners.UniTaskLoopRunnerLastYieldPostLateUpdate), yielders[13] = new ContinuationQueue(),
|
|
|
|
|
typeof(UniTaskLoopRunners.UniTaskLoopRunnerPostLateUpdate), runners[12] = new PlayerLoopRunner(),
|
|
|
|
|
typeof(UniTaskLoopRunners.UniTaskLoopRunnerLastPostLateUpdate), runners[13] = new PlayerLoopRunner());
|
2019-05-19 23:14:47 +08:00
|
|
|
|
|
|
|
|
|
playerLoop.subSystemList = copyList;
|
|
|
|
|
PlayerLoop.SetPlayerLoop(playerLoop);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void AddAction(PlayerLoopTiming timing, IPlayerLoopItem action)
|
|
|
|
|
{
|
|
|
|
|
runners[(int)timing].AddAction(action);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void AddContinuation(PlayerLoopTiming timing, Action continuation)
|
|
|
|
|
{
|
|
|
|
|
yielders[(int)timing].Enqueue(continuation);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif
|