Skip to content

Commit d0003c8

Browse files
author
Stewart Miles
committed
Fixed RunOnMainThread behavior in batch mode vs. executeMethod mode.
When the Unity editor is launched in batch mode, EditorApplication.update is called periodically just like in interactive mode. The EditorApplication.update is not called if the editor is launched with the -executeMethod argument. This changes RunOnMainThread to only execute jobs in serial if the editor is launched with the -executeMethod flag. This makes it possible to get notifications of an asset database refresh in batch mode without -executeMethod set. Bug: 150471207 Change-Id: I5878e1f5ff6dc3ee6a8bafd998c54b100ab9f2c5
1 parent d20d6f2 commit d0003c8

File tree

2 files changed

+28
-14
lines changed

2 files changed

+28
-14
lines changed

source/VersionHandlerImpl/src/ExecutionEnvironment.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,21 @@ public static bool InBatchMode {
3232
get { return Environment.CommandLine.Contains("-batchmode"); }
3333
}
3434

35+
/// <summary>
36+
/// Whether the editor was started with a method to executed.
37+
/// </summary>
38+
public static bool ExecuteMethodEnabled {
39+
get { return Environment.CommandLine.Contains("-executeMethod"); }
40+
}
41+
3542
/// <summary>
3643
/// Whether the UI should be treated as interactive.
3744
/// </summary>
3845
internal static bool InteractiveMode {
39-
get { return !Environment.CommandLine.Contains("-gvh_noninteractive"); }
46+
get {
47+
return !(Environment.CommandLine.Contains("-gvh_noninteractive") ||
48+
ExecutionEnvironment.InBatchMode);
49+
}
4050
}
4151

4252
/// <summary>

source/VersionHandlerImpl/src/RunOnMainThread.cs

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public static int Schedule(Action job, double delayInMilliseconds) {
7878
scheduledJob = new ScheduledJob {
7979
Job = job,
8080
JobId = nextJobId,
81-
DelayInMilliseconds = ExecutionEnvironment.InBatchMode ? 0.0 :
81+
DelayInMilliseconds = ExecutionEnvironment.ExecuteMethodEnabled ? 0.0 :
8282
delayInMilliseconds
8383
};
8484
scheduledJobs[nextJobId++] = scheduledJob;
@@ -208,9 +208,9 @@ private static bool OnMainThread {
208208
}
209209

210210
/// <summary>
211-
/// Set when the current thread is running ExecuteAll().
211+
/// Number of times ExecuteAll() has been called on the current thread.
212212
/// </summary>
213-
private static bool runningExecuteAll = false;
213+
private static int runningExecuteAllCount = 0;
214214

215215
/// <summary>
216216
/// Flag which indicates whether any jobs are running on the main thread.
@@ -223,7 +223,10 @@ private static bool OnMainThread {
223223
/// This property is reset to its' default value after each set of jobs is dispatched.
224224
/// </summary>
225225
public static bool ExecuteNow {
226-
get { return ExecutionEnvironment.InBatchMode && !runningJobs && !runningExecuteAll; }
226+
get {
227+
return ExecutionEnvironment.ExecuteMethodEnabled && !runningJobs &&
228+
runningExecuteAllCount == 0;
229+
}
227230
}
228231

229232
/// <summary>
@@ -234,7 +237,7 @@ static RunOnMainThread() {
234237
mainThreadId = System.Threading.Thread.CurrentThread.ManagedThreadId;
235238
// NOTE: This hooks ExecuteAll on the main thread here and never unregisters as we can't
236239
// register event handlers on any thread except for the main thread.
237-
if (!ExecutionEnvironment.InBatchMode) OnUpdate += ExecuteAll;
240+
OnUpdate += ExecuteAll;
238241
}
239242

240243
/// <summary>
@@ -261,9 +264,9 @@ private static void AddOnUpdateCallback(EditorApplication.CallbackFunction callb
261264
Run(() => {
262265
EditorApplication.update -= callback;
263266
EditorApplication.update += callback;
264-
// If we're in batch mode, execute the callback now as EditorApplication.update
265-
// will not be signaled if Unity was launched to execute a single method.
266-
if (ExecutionEnvironment.InBatchMode) callback();
267+
// If we're in running a single method, execute the callback now as
268+
// EditorApplication.update will not be signaled.
269+
if (ExecutionEnvironment.ExecuteMethodEnabled) callback();
267270
});
268271
}
269272

@@ -470,10 +473,10 @@ private static void ExecuteAllUnnested(bool allowNested) {
470473

471474
// Don't nest job execution on the main thread, return to the last stack frame
472475
// running ExecuteAll().
473-
if (runningExecuteAll && !allowNested) return;
476+
if (runningExecuteAllCount > 0 && !allowNested) return;
474477

475478
RunAction(() => {
476-
runningExecuteAll = true;
479+
runningExecuteAllCount ++;
477480
bool jobsRemaining = true;
478481
while (jobsRemaining) {
479482
jobsRemaining = false;
@@ -484,12 +487,13 @@ private static void ExecuteAllUnnested(bool allowNested) {
484487

485488
// Execute polling jobs.
486489
int remainingJobs = ExecutePollingJobs();
487-
// If we're in batch mode, keep on executing until no polling jobs remain.
488-
if (ExecutionEnvironment.InBatchMode && remainingJobs > 0) {
490+
// If we're in running a single method, keep on executing until no polling jobs
491+
// remain.
492+
if (ExecutionEnvironment.ExecuteMethodEnabled && remainingJobs > 0) {
489493
jobsRemaining = true;
490494
}
491495
}
492-
runningExecuteAll = false;
496+
runningExecuteAllCount --;
493497
});
494498
}
495499
}

0 commit comments

Comments
 (0)