Skip to content

Commit

Permalink
Fix exit play mode process for domain reloading is enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
nowsprinting committed Nov 19, 2024
1 parent b6d880f commit 18bfdea
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 44 deletions.
24 changes: 0 additions & 24 deletions Editor/Commandline.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,6 @@ private static void Bootstrap()
// Set first open Scene
EditorSceneManager.playModeStartScene = myWantedStartScene;

// Register event handler for terminate autopilot
EditorApplication.playModeStateChanged += OnChangePlayModeState;

// Activate autopilot and enter play mode
var state = AutopilotState.Instance;
state.launchFrom = LaunchType.Commandline;
Expand Down Expand Up @@ -97,26 +94,5 @@ private static void FocusGameView()
var gameView = assembly.GetType(viewClass);
EditorWindow.GetWindow(gameView, false, null, true);
}

/// <summary>
/// Stop autopilot on play mode exit event when run on Unity editor.
/// Not called when invoked from play mode (not registered in event listener).
/// </summary>
/// <param name="playModeStateChange"></param>
private static void OnChangePlayModeState(PlayModeStateChange playModeStateChange)
{
if (playModeStateChange != PlayModeStateChange.EnteredEditMode)
{
return;
}

EditorApplication.playModeStateChanged -= OnChangePlayModeState;

// Exit Unity when returning from play mode to edit mode.
// Because it may freeze when exiting without going through edit mode.
var exitCode = (int)AutopilotState.Instance.exitCode;
Debug.Log($"Exit Unity-editor by autopilot, exit code: {(int)exitCode}");
EditorApplication.Exit(exitCode);
}
}
}
19 changes: 0 additions & 19 deletions Editor/UI/Settings/AutopilotSettingsEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ private static void DrawHeader(string label)
// ReSharper disable once MemberCanBeMadeStatic.Global
internal void Stop()
{
EditorApplication.playModeStateChanged -= OnChangePlayModeState;
Autopilot.Instance.TerminateAsync(ExitCode.Normally, reporting: false).Forget();
}

Expand All @@ -131,24 +130,6 @@ internal void Launch()
state.launchFrom = LaunchType.EditMode;
EditorApplication.isPlaying = true;
}

EditorApplication.playModeStateChanged += OnChangePlayModeState;
}

/// <summary>
/// Teardown when Play Mode is stopped while the Autopilot is running.
/// </summary>
private static void OnChangePlayModeState(PlayModeStateChange playModeStateChange)
{
if (playModeStateChange != PlayModeStateChange.EnteredEditMode)
{
return;
}

EditorApplication.playModeStateChanged -= OnChangePlayModeState;

Debug.LogWarning("Play Mode is stopped while the Autopilot is running");
AutopilotState.Instance.Reset();
}
}
}
60 changes: 60 additions & 0 deletions Runtime/Autopilot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
using JetBrains.Annotations;
using UnityEngine;
using UnityEngine.SceneManagement;
#if UNITY_EDITOR
using UnityEditor;
#endif

namespace DeNA.Anjin
{
Expand Down Expand Up @@ -71,6 +74,17 @@ private void Start()
throw new InvalidOperationException("Autopilot is not running");
}

#if UNITY_EDITOR
if (_state.launchFrom == LaunchType.Commandline)
{
EditorApplication.playModeStateChanged += OnExitPlayModeToTerminateEditor;
}
else
{
EditorApplication.playModeStateChanged += OnExitPlayModeToTeardown;
}
#endif

_logger = _settings.LoggerAsset.Logger;
// Note: Set a default logger if no logger settings. see: AutopilotSettings.Initialize method.

Expand Down Expand Up @@ -148,6 +162,13 @@ public async UniTask TerminateAsync(ExitCode exitCode, string message = null, st

_isTerminating = true;

#if UNITY_EDITOR
if (_state.launchFrom != LaunchType.Commandline)
{
EditorApplication.playModeStateChanged -= OnExitPlayModeToTeardown;
}
#endif

if (reporting && _state.IsRunning && _settings.Reporter != null)
{
await _settings.Reporter.PostReportAsync(message, stackTrace, exitCode, token);
Expand All @@ -164,5 +185,44 @@ public void Terminate(ExitCode exitCode, string logString = null, string stackTr
{
TerminateAsync(exitCode, logString, stackTrace).Forget();
}

#if UNITY_EDITOR
/// <summary>
/// Stop autopilot on play mode exit event when run on Unity editor.
/// Not called when invoked from play mode (not registered in event listener).
/// </summary>
private static void OnExitPlayModeToTerminateEditor(PlayModeStateChange playModeStateChange)
{
if (playModeStateChange != PlayModeStateChange.EnteredEditMode)
{
return;
}

EditorApplication.playModeStateChanged -= OnExitPlayModeToTerminateEditor;

// Exit Unity when returning from play mode to edit mode.
// Because it may freeze when exiting without going through edit mode.
var exitCode = (int)AutopilotState.Instance.exitCode;
Debug.Log($"Exit Unity-editor by autopilot, exit code: {exitCode}");
EditorApplication.Exit(exitCode);
}

/// <summary>
/// Teardown when Play Mode is stopped while the Autopilot is running.
/// </summary>
private static void OnExitPlayModeToTeardown(PlayModeStateChange playModeStateChange)
{
if (playModeStateChange != PlayModeStateChange.EnteredEditMode)
{
return;
}

EditorApplication.playModeStateChanged -= OnExitPlayModeToTeardown;

// Teardown when Play Mode is stopped while the Autopilot is running.
Debug.LogWarning("Play Mode is stopped while the Autopilot is running");
AutopilotState.Instance.Reset();
}
#endif
}
}
2 changes: 1 addition & 1 deletion Runtime/Launcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ internal static async UniTaskVoid TeardownLaunchAutopilotAsync(AutopilotState st
await UniTask.NextFrame();
#if UNITY_EDITOR
EditorApplication.isPlaying = false;
// Note: If launched from the command line, `DeNA.Anjin.Editor.Commandline.OnChangePlayModeState()` will be called, and the Unity editor will be terminated.
// Note: If launched from the command line, `DeNA.Anjin.Autopilot.OnExitPlayModeToTerminateEditor()` will be called, and the Unity editor will be terminated.
#endif
}
else
Expand Down

0 comments on commit 18bfdea

Please sign in to comment.