Skip to content

Commit 7aa10e2

Browse files
Improve play mode test detection by preventing RuntimeInitializeOnLoadMethod registration
The previous fix checked TestRunnerCallbacks.IsRunningTests at runtime, but there was a timing issue - RuntimeInitializeOnLoadMethod executes before TestRunnerCallbacks.RunStarted is called, so the flag was not set yet. This commit uses the cleaner approach recommended by Unity: prevent the method from being registered at all during tests by applying #if !UNITY_INCLUDE_TESTS to the attribute itself. This eliminates the runtime check and TestRunnerCallbacks dependency entirely from ChangeScene.cs, making the code simpler and more reliable. References: - Unity Discussions: RuntimeInitializeOnLoadMethod runs during playmode unit tests https://discussions.unity.com/t/methods-with-runtimeinitializeonloadmethod-attribute-run-during-playmode-unit-tests/949876 Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
1 parent 32a7722 commit 7aa10e2

1 file changed

Lines changed: 3 additions & 8 deletions

File tree

  • UnityProject/Packages/com.jasonxudeveloper.jengine.core/Editor/Misc

UnityProject/Packages/com.jasonxudeveloper.jengine.core/Editor/Misc/ChangeScene.cs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,12 @@ namespace JEngine.Core.Editor.Misc
99
/// </summary>
1010
internal static class ChangeScene
1111
{
12+
// Don't auto-change scenes during Play Mode tests
13+
#if !UNITY_INCLUDE_TESTS
1214
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
15+
#endif
1316
private static void DoChange()
1417
{
15-
#if UNITY_INCLUDE_TESTS
16-
// Skip scene change when running Play Mode tests
17-
if (TestRunnerCallbacks.IsRunningTests)
18-
{
19-
return;
20-
}
21-
#endif
22-
2318
var jump = Settings.Instance.jumpStartUp;
2419
if (!jump) return;
2520
var scene = SceneManager.GetActiveScene();

0 commit comments

Comments
 (0)