Skip to content

Commit

Permalink
Merge pull request #137 from nowsprinting/fix/unload_scene
Browse files Browse the repository at this point in the history
Fix to Agent is not destroyed
  • Loading branch information
asurato authored Jan 10, 2025
2 parents b91d362 + a9ffa63 commit ff89ed3
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 12 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
2 changes: 1 addition & 1 deletion README_ja.md
Original file line number Diff line number Diff line change
Expand Up @@ -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バージョンを指定してテストを実行するには、次のコマンドを実行します。

Expand Down
12 changes: 8 additions & 4 deletions Runtime/AgentDispatcher.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -101,7 +101,7 @@ public bool DispatchByScene(Scene scene, bool fallback = true)
return false;
}

DispatchAgent(agent);
DispatchAgent(agent, scene);
return true;
}

Expand All @@ -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;
Expand All @@ -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}");
Expand Down
38 changes: 32 additions & 6 deletions Tests/Runtime/AgentDispatcherTest.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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";
Expand Down Expand Up @@ -52,7 +56,7 @@ private void SetUpDispatcher(AutopilotSettings settings)
}

[Test]
[CreateScene]
[CreateScene(unloadOthers: true)]
public async Task DispatchByScene_DispatchAgentBySceneAgentMaps()
{
const string AgentName = "Mapped Agent";
Expand All @@ -71,7 +75,7 @@ public async Task DispatchByScene_DispatchAgentBySceneAgentMaps()
}

[Test]
[CreateScene]
[CreateScene(unloadOthers: true)]
public async Task DispatchByScene_DispatchFallbackAgent()
{
const string AgentName = "Fallback Agent";
Expand All @@ -87,7 +91,7 @@ public async Task DispatchByScene_DispatchFallbackAgent()
}

[Test]
[CreateScene]
[CreateScene(unloadOthers: true)]
public async Task DispatchByScene_NoSceneAgentMapsAndFallbackAgent_AgentIsNotDispatch()
{
var settings = ScriptableObject.CreateInstance<AutopilotSettings>();
Expand All @@ -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";
Expand All @@ -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<AutopilotSettings>();
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";
Expand All @@ -141,6 +166,7 @@ public void DispatchSceneCrossingAgents_DispatchAgent()
}

[Test]
[CreateScene(unloadOthers: true)]
public async Task Dispose_DestroyAllRunningAgents()
{
var settings = ScriptableObject.CreateInstance<AutopilotSettings>();
Expand Down

0 comments on commit ff89ed3

Please sign in to comment.