Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Teardown when Play Mode is stopped while the Autopilot running #109

Merged
merged 3 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
}
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -665,7 +665,15 @@ It can specify the same arguments as when running in the editor.

## Troubleshooting

### Autopilot runs on its own in play mode.
### Autopilot is stopped, but the Run button in AutopilotSettings does not appear

The `AutopilotState.asset` that persists in Anjin's execution state may be in an incorrect state.
Open it in the inspector and click the **Reset** button.

If this does not solve the problem, try deleting `AutopilotState.asset`.


### Autopilot runs on its own in play mode

The `AutopilotState.asset` that persists in Anjin's execution state may be in an incorrect state.
Open it in the inspector and click the **Reset** button.
Expand Down
8 changes: 8 additions & 0 deletions README_ja.md
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,14 @@ $(ROM) -LAUNCH_AUTOPILOT_SETTINGS Path/To/AutopilotSettings

## トラブルシューティング

### オートパイロットは停止しているのに AutopilotSettings の Run ボタンが表示されない

Anjinの実行状態を永続化している `AutopilotState.asset` が不正な状態になっている恐れがあります。
インスペクタで開いて**Reset**ボタンをクリックしてください。

それでも解決しない場合、 `AutopilotState.asset` を削除してみてください。


### プロジェクトを再生モードにすると勝手にオートパイロットが動いてしまう

Anjinの実行状態を永続化している `AutopilotState.asset` が不正な状態になっている恐れがあります。
Expand Down
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
Loading