Skip to content

Commit 50b1a09

Browse files
committed
EditorApplication.delayCall invocation interval is throttled making UI sluggish. Use update instead.
1 parent 0eeb2d0 commit 50b1a09

File tree

1 file changed

+29
-4
lines changed

1 file changed

+29
-4
lines changed

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

Lines changed: 29 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.Generic;
1010
using UnityEditor;
1111

1212
public class MainThreadSynchronizationContext: SynchronizationContext, IMainThreadSynchronizationContext
1313
{
14-
public MainThreadSynchronizationContext(CancellationToken token = default) { }
14+
readonly Queue<Action> m_Callbacks = new Queue<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,25 @@ public override void Post(SendOrPostCallback d, object state)
2434
if (d == null)
2535
return;
2636

27-
EditorApplication.delayCall += () => d(state);
37+
lock (m_Callbacks)
38+
m_Callbacks.Enqueue(() => d(state));
2839
}
2940

30-
public void Dispose() {}
41+
void Update()
42+
{
43+
Queue<Action> callbacks;
44+
45+
lock (m_Callbacks)
46+
{
47+
if (m_Callbacks.Count == 0)
48+
return;
49+
callbacks = new Queue<Action>(m_Callbacks);
50+
m_Callbacks.Clear();
51+
}
52+
53+
foreach (var callback in callbacks)
54+
callback();
55+
}
3156
}
3257
#else
3358
public class MainThreadSynchronizationContext : ThreadSynchronizationContext, IMainThreadSynchronizationContext

0 commit comments

Comments
 (0)