diff --git a/README.md b/README.md index c1d3c36..fca777c 100644 --- a/README.md +++ b/README.md @@ -846,7 +846,7 @@ git submodule add https://github.com/dena/Anjin.git Packages/com.dena.anjin > [!WARNING] > Required install packages for running tests (when adding to the `testables` in package.json), as follows: > - [Unity Test Framework](https://docs.unity3d.com/Packages/com.unity.test-framework@latest) package v1.3.4 or later -> - [Test Helper](https://github.com/nowsprinting/test-helper) package v0.7.2 or later +> - [Test Helper](https://github.com/nowsprinting/test-helper) package v1.1.1 or later Generate a temporary project and run tests on each Unity version from the command line. diff --git a/README_ja.md b/README_ja.md index 694be35..162348c 100644 --- a/README_ja.md +++ b/README_ja.md @@ -856,7 +856,7 @@ git submodule add https://github.com/dena/Anjin.git Packages/com.dena.anjin > [!WARNING] > Anjinパッケージ内のテストを実行するためには、次のパッケージのインストールが必要です。 > - [Unity Test Framework](https://docs.unity3d.com/Packages/com.unity.test-framework@latest) package v1.3.4 or later -> - [Test Helper](https://github.com/nowsprinting/test-helper) package v0.7.2 or later +> - [Test Helper](https://github.com/nowsprinting/test-helper) package v1.1.1 or later テスト専用のUnityプロジェクトを生成し、Unityバージョンを指定してテストを実行するには、次のコマンドを実行します。 diff --git a/Runtime/AgentDispatcher.cs b/Runtime/AgentDispatcher.cs index 56570a2..c8e1ee0 100644 --- a/Runtime/AgentDispatcher.cs +++ b/Runtime/AgentDispatcher.cs @@ -1,4 +1,4 @@ -// Copyright (c) 2023-2024 DeNA Co., Ltd. +// Copyright (c) 2023-2025 DeNA Co., Ltd. // This software is released under the MIT License. using System; @@ -101,7 +101,7 @@ public bool DispatchByScene(Scene scene, bool fallback = true) return false; } - DispatchAgent(agent); + DispatchAgent(agent, scene); return true; } @@ -110,11 +110,11 @@ public void DispatchSceneCrossingAgents() { _settings.sceneCrossingAgents.ForEach(agent => { - DispatchAgent(agent, true); + DispatchAgent(agent, dontDestroyOnLoad: true); }); } - private void DispatchAgent(AbstractAgent agent, bool dontDestroyOnLoad = false) + private void DispatchAgent(AbstractAgent agent, Scene scene = default, bool dontDestroyOnLoad = false) { var agentName = agent.name; agent.Logger = _logger; @@ -125,6 +125,10 @@ private void DispatchAgent(AbstractAgent agent, bool dontDestroyOnLoad = false) { Object.DontDestroyOnLoad(inspector.gameObject); } + else if (scene != default) + { + SceneManager.MoveGameObjectToScene(inspector.gameObject, scene); + } var token = inspector.gameObject.GetCancellationTokenOnDestroy(); _logger.Log($"Dispatch Agent: {agentName}"); diff --git a/Tests/Runtime/AgentDispatcherTest.cs b/Tests/Runtime/AgentDispatcherTest.cs index f47d588..0ddfdb2 100644 --- a/Tests/Runtime/AgentDispatcherTest.cs +++ b/Tests/Runtime/AgentDispatcherTest.cs @@ -1,6 +1,7 @@ -// Copyright (c) 2023-2024 DeNA Co., Ltd. +// Copyright (c) 2023-2025 DeNA Co., Ltd. // This software is released under the MIT License. +using System.Diagnostics.CodeAnalysis; using System.Text.RegularExpressions; using System.Threading.Tasks; using Cysharp.Threading.Tasks; @@ -16,7 +17,10 @@ namespace DeNA.Anjin { + [TestFixture] [UnityPlatform(RuntimePlatform.OSXEditor, RuntimePlatform.WindowsEditor, RuntimePlatform.LinuxEditor)] + [SuppressMessage("IDisposableAnalyzers.Correctness", "IDISP003:Dispose previous before re-assigning")] + [SuppressMessage("IDisposableAnalyzers.Correctness", "IDISP006:Implement IDisposable")] public class AgentDispatcherTest { private const string TestScenePath = "Packages/com.dena.anjin/Tests/TestScenes/Buttons.unity"; @@ -52,7 +56,7 @@ private void SetUpDispatcher(AutopilotSettings settings) } [Test] - [CreateScene] + [CreateScene(unloadOthers: true)] public async Task DispatchByScene_DispatchAgentBySceneAgentMaps() { const string AgentName = "Mapped Agent"; @@ -71,7 +75,7 @@ public async Task DispatchByScene_DispatchAgentBySceneAgentMaps() } [Test] - [CreateScene] + [CreateScene(unloadOthers: true)] public async Task DispatchByScene_DispatchFallbackAgent() { const string AgentName = "Fallback Agent"; @@ -87,7 +91,7 @@ public async Task DispatchByScene_DispatchFallbackAgent() } [Test] - [CreateScene] + [CreateScene(unloadOthers: true)] public async Task DispatchByScene_NoSceneAgentMapsAndFallbackAgent_AgentIsNotDispatch() { var settings = ScriptableObject.CreateInstance(); @@ -100,7 +104,7 @@ public async Task DispatchByScene_NoSceneAgentMapsAndFallbackAgent_AgentIsNotDis } [Test] - [CreateScene] + [CreateScene(unloadOthers: true)] public async Task DispatchByScene_ReActivateScene_NotCreateDuplicateAgents() { const string AgentName = "Mapped Agent"; @@ -125,7 +129,28 @@ public async Task DispatchByScene_ReActivateScene_NotCreateDuplicateAgents() } [Test] - [CreateScene] + [CreateScene(unloadOthers: true)] + public async Task DispatchByScene_SceneUnloadWithoutBecomingActive_DisposeAgent() + { + const string AgentName = "Mapped Agent"; + var settings = ScriptableObject.CreateInstance(); + settings.sceneAgentMaps.Add(new SceneAgentMap + { + scenePath = TestScenePath, agent = CreateSpyAliveCountAgent(AgentName) + }); + SetUpDispatcher(settings); + + await SceneManagerHelper.LoadSceneAsync(TestScenePath, LoadSceneMode.Additive); + Assume.That(SpyAliveCountAgent.AliveInstances, Is.EqualTo(1)); + + await SceneManager.UnloadSceneAsync(GetSceneName(TestScenePath)); + await UniTask.NextFrame(); // Wait for destroy + + Assert.That(SpyAliveCountAgent.AliveInstances, Is.EqualTo(0)); + } + + [Test] + [CreateScene(unloadOthers: true)] public void DispatchSceneCrossingAgents_DispatchAgent() { const string AgentName = "Scene Crossing Agent"; @@ -141,6 +166,7 @@ public void DispatchSceneCrossingAgents_DispatchAgent() } [Test] + [CreateScene(unloadOthers: true)] public async Task Dispose_DestroyAllRunningAgents() { var settings = ScriptableObject.CreateInstance();