Skip to content

Commit 17849f8

Browse files
committed
Added a delay to the script recompilation
1 parent acad201 commit 17849f8

File tree

3 files changed

+60
-2
lines changed

3 files changed

+60
-2
lines changed

Editor/GenericTypesAnalyzer/GenericTypesAnalyzer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ private static void AnalyzeGenericTypes()
4444
catch (ApplicationException)
4545
{
4646
Debug.LogWarning("Editor could not load some of the scriptable objects from plugin's resources. It will try on next assembly reload.");
47-
CompilationHelper.RecompileOnce();
47+
EditorCoroutineHelper.Delay(CompilationHelper.RecompileOnce, 1f);
4848
return;
4949
}
5050

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
namespace GenericUnityObjects.Editor.Util
2+
{
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Diagnostics;
6+
using UnityEditor;
7+
using Debug = UnityEngine.Debug;
8+
9+
public static class EditorCoroutineHelper
10+
{
11+
private static readonly List<(Action Action, float Time)> _currentActions = new List<(Action Action, float Time)>();
12+
13+
private static readonly Stopwatch _stopwatch;
14+
private static long _elapsedTime;
15+
16+
static EditorCoroutineHelper()
17+
{
18+
_stopwatch = new Stopwatch();
19+
_stopwatch.Start();
20+
EditorApplication.update += Update;
21+
}
22+
23+
public static void Delay(Action action, float timeInSeconds)
24+
{
25+
if (timeInSeconds <= 0f)
26+
{
27+
action.Invoke();
28+
return;
29+
}
30+
31+
_currentActions.Add((action, timeInSeconds));
32+
}
33+
34+
private static void Update()
35+
{
36+
float timeSinceLastFrameMilliseconds = _stopwatch.ElapsedMilliseconds - _elapsedTime;
37+
_elapsedTime = _stopwatch.ElapsedMilliseconds;
38+
39+
int i = 0;
40+
41+
while (i < _currentActions.Count)
42+
{
43+
(Action action, float time) = _currentActions[i];
44+
time -= timeSinceLastFrameMilliseconds / 1000f;
45+
46+
if (time > 0f)
47+
{
48+
_currentActions[i] = (action, time);
49+
i++;
50+
continue;
51+
}
52+
53+
action.Invoke();
54+
_currentActions.RemoveAt(i);
55+
}
56+
}
57+
}
58+
}

Editor/Util/IconSetter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ private static void SetIcons()
5858
catch (ApplicationException)
5959
{
6060
Debug.LogWarning("Editor could not load some of the scriptable objects from plugin's resources. It will try on next assembly reload.");
61-
CompilationHelper.RecompileOnce();
61+
EditorCoroutineHelper.Delay(CompilationHelper.RecompileOnce, 1f);
6262
return;
6363
}
6464

0 commit comments

Comments
 (0)