Skip to content

Commit 6354c31

Browse files
authored
Merge pull request #10 from Unity-Technologies/delaycall-fix
Tasks with UI affinity are now invoked faster.
2 parents 0eeb2d0 + e171567 commit 6354c31

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed

extras/com.unity.editor.tasks/Tests/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"description": "Tests - Unity Editor Tasks",
55
"type": "tests",
66
"version": "0.0.0-placeholder",
7+
"unity": "2018.4",
78
"license" : "MIT",
89
"author": {
910
"name": "Andreia Gaita",

src/com.unity.editor.tasks/Editor/Schedulers/MainThreadSynchronizationContext.cs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,22 @@ namespace Unity.Editor.Tasks
66
using Helpers;
77

88
#if UNITY_EDITOR
9-
9+
using System.Collections.Concurrent;
1010
using UnityEditor;
1111

1212
public class MainThreadSynchronizationContext: SynchronizationContext, IMainThreadSynchronizationContext
1313
{
14-
public MainThreadSynchronizationContext(CancellationToken token = default) { }
14+
private readonly ConcurrentQueue<Action> m_Callbacks = new ConcurrentQueue<Action>();
15+
16+
public MainThreadSynchronizationContext(CancellationToken token = default)
17+
{
18+
EditorApplication.update += Update;
19+
}
20+
21+
public void Dispose()
22+
{
23+
EditorApplication.update -= Update;
24+
}
1525

1626
public void Schedule(Action action)
1727
{
@@ -24,10 +34,14 @@ public override void Post(SendOrPostCallback d, object state)
2434
if (d == null)
2535
return;
2636

27-
EditorApplication.delayCall += () => d(state);
37+
m_Callbacks.Enqueue(() => d(state));
2838
}
2939

30-
public void Dispose() {}
40+
void Update()
41+
{
42+
while (m_Callbacks.TryDequeue(out var callback))
43+
callback();
44+
}
3145
}
3246
#else
3347
public class MainThreadSynchronizationContext : ThreadSynchronizationContext, IMainThreadSynchronizationContext

src/com.unity.editor.tasks/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"displayName": "Unity Editor Tasks",
44
"description": "A friendly threaded task system for the Unity Editor.",
55
"version": "0.0.0-placeholder",
6+
"unity": "2018.4",
67
"license" : "MIT",
78
"author": {
89
"name": "Andreia Gaita",

0 commit comments

Comments
 (0)