From c0163d4d1c6e5e1e671c3f59003e5b2e43d11817 Mon Sep 17 00:00:00 2001 From: "JSD\\13999" <1399945104@qq.com> Date: Tue, 19 May 2026 11:22:07 +0800 Subject: [PATCH] =?UTF-8?q?=E7=A8=B3=E5=AE=9A=20Audio=20PlayMode=20?= =?UTF-8?q?=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Tests/PlayMode/Audio/AudioServiceTests.cs | 39 +++++++++++++++++-- 1 file changed, 35 insertions(+), 4 deletions(-) diff --git a/My project/Assets/FlowScope/Tests/PlayMode/Audio/AudioServiceTests.cs b/My project/Assets/FlowScope/Tests/PlayMode/Audio/AudioServiceTests.cs index 4712cd5..95ed4df 100644 --- a/My project/Assets/FlowScope/Tests/PlayMode/Audio/AudioServiceTests.cs +++ b/My project/Assets/FlowScope/Tests/PlayMode/Audio/AudioServiceTests.cs @@ -17,6 +17,7 @@ namespace FlowScope.Tests.PlayMode.Audio private AudioClip bgmA; private AudioClip bgmB; private AudioClip sfx; + private GameObject listenerObject; [SetUp] public void SetUp() @@ -29,6 +30,12 @@ namespace FlowScope.Tests.PlayMode.Audio resources.Add("bgm-a", bgmA); resources.Add("bgm-b", bgmB); resources.Add("sfx", sfx); + + if (Object.FindObjectOfType() == null) + { + listenerObject = new GameObject("AudioServiceTestListener"); + listenerObject.AddComponent(); + } } [TearDown] @@ -38,6 +45,11 @@ namespace FlowScope.Tests.PlayMode.Audio { Object.Destroy(service.gameObject); } + + if (listenerObject != null) + { + Object.DestroyImmediate(listenerObject); + } } [UnityTest] @@ -129,10 +141,15 @@ namespace FlowScope.Tests.PlayMode.Audio handle.Stop(0.1f); - yield return new WaitForSeconds(0.15f); - Assert.That(handle.IsPlaying, Is.False); - Assert.That(bgmSource.isPlaying, Is.False); - Assert.That(bgmSource.volume, Is.EqualTo(0f).Within(0.01f)); + yield return WaitUntilOrFail( + () => bgmSource == null || (!bgmSource.isPlaying && bgmSource.volume <= 0.01f), + 1f, + "BGM fade-out did not stop and reach zero volume."); + if (bgmSource != null) + { + Assert.That(bgmSource.isPlaying, Is.False); + Assert.That(bgmSource.volume, Is.EqualTo(0f).Within(0.01f)); + } } [Test] @@ -151,6 +168,20 @@ namespace FlowScope.Tests.PlayMode.Audio return gameObject.AddComponent().Initialize(resources, config); } + private static System.Collections.IEnumerator WaitUntilOrFail( + Func condition, + float timeoutSeconds, + string message) + { + var deadline = Time.realtimeSinceStartup + timeoutSeconds; + while (!condition() && Time.realtimeSinceStartup < deadline) + { + yield return null; + } + + Assert.That(condition(), Is.True, message); + } + private static AudioClip CreateClip(string name, float lengthSeconds) { var sampleCount = Mathf.CeilToInt(44100 * lengthSeconds);