使用DoTween重构淡变效果
This commit is contained in:
@@ -49,7 +49,7 @@ MonoBehaviour:
|
|||||||
tk2DEnabled: 0
|
tk2DEnabled: 0
|
||||||
deAudioEnabled: 0
|
deAudioEnabled: 0
|
||||||
deUnityExtendedEnabled: 0
|
deUnityExtendedEnabled: 0
|
||||||
epoOutlineEnabled: 1
|
epoOutlineEnabled: 0
|
||||||
createASMDEF: 1
|
createASMDEF: 1
|
||||||
showPlayingTweens: 0
|
showPlayingTweens: 0
|
||||||
showPausedTweens: 0
|
showPausedTweens: 0
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ using System;
|
|||||||
using System.Collections;
|
using System.Collections;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
using DG.Tweening;
|
||||||
|
|
||||||
namespace OCES.Audio
|
namespace OCES.Audio
|
||||||
{
|
{
|
||||||
@@ -106,39 +107,64 @@ namespace OCES.Audio
|
|||||||
{
|
{
|
||||||
if (handle.Cancelled) break;
|
if (handle.Cancelled) break;
|
||||||
elapsed += Time.deltaTime;
|
elapsed += Time.deltaTime;
|
||||||
float t = Mathf.Clamp01(elapsed / duration);
|
// 为每个 source 创建 DOFade(仅创建一次)
|
||||||
float vol = Mathf.Lerp(fromVolume, 0f, t);
|
sources.Clear();
|
||||||
|
handle.CollectActiveSources(sources);
|
||||||
|
|
||||||
foreach (AudioSource src in sources)
|
foreach (AudioSource src in sources)
|
||||||
if (src) src.volume = vol;
|
{
|
||||||
|
if (!src) continue;
|
||||||
|
|
||||||
|
if (DOTween.IsTweening(src))
|
||||||
|
continue;
|
||||||
|
|
||||||
|
src.volume = fromVolume;
|
||||||
|
src.DOFade(0f, duration - elapsed).SetEase(Ease.Linear);
|
||||||
|
}
|
||||||
yield return null;
|
yield return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 确保最终状态
|
||||||
|
sources.Clear();
|
||||||
|
handle.CollectActiveSources(sources);
|
||||||
|
foreach (AudioSource src in sources)
|
||||||
|
if (src) src.volume = 0f;
|
||||||
|
|
||||||
StopHandle(handle);
|
StopHandle(handle);
|
||||||
}
|
}
|
||||||
|
|
||||||
IEnumerator FadeIn(ContainerPlayHandle handle, float duration)
|
IEnumerator FadeIn(ContainerPlayHandle handle, float duration)
|
||||||
{
|
{
|
||||||
//Debug.Log($"Fading In {duration} seconds.");
|
|
||||||
if (handle == null || handle.Cancelled) yield break;
|
if (handle == null || handle.Cancelled) yield break;
|
||||||
|
|
||||||
float elapsed = 0f;
|
|
||||||
List<AudioSource> sources = new();
|
List<AudioSource> sources = new();
|
||||||
|
float elapsed = 0f;
|
||||||
|
|
||||||
while (elapsed < duration)
|
while (elapsed < duration)
|
||||||
{
|
{
|
||||||
if (handle.Cancelled) yield break;
|
if (handle.Cancelled) yield break;
|
||||||
elapsed += Time.deltaTime;
|
|
||||||
float t = Mathf.Clamp01(elapsed / duration);
|
|
||||||
|
|
||||||
// 每帧重新收集(Blend 模式下新 source 可能中途加入)
|
elapsed += Time.deltaTime;
|
||||||
|
|
||||||
|
// 每帧收集当前活跃的 sources,并为新加入的 source 创建 tween
|
||||||
sources.Clear();
|
sources.Clear();
|
||||||
handle.CollectActiveSources(sources);
|
handle.CollectActiveSources(sources);
|
||||||
|
|
||||||
foreach (AudioSource src in sources)
|
foreach (AudioSource src in sources)
|
||||||
if (src) src.volume = Mathf.Lerp(0f, 1f, t);
|
{
|
||||||
|
if (!src) continue;
|
||||||
|
|
||||||
|
// 如果这个 source 还没被 tween 过,则创建一个 DOFade
|
||||||
|
if (DOTween.IsTweening(src))
|
||||||
|
continue;
|
||||||
|
src.volume = 0f;
|
||||||
|
src.DOFade(1f, duration - elapsed).SetEase(Ease.Linear);
|
||||||
|
}
|
||||||
|
|
||||||
yield return null;
|
yield return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 确保最终音量准确
|
// 确保最终音量
|
||||||
sources.Clear();
|
sources.Clear();
|
||||||
handle.CollectActiveSources(sources);
|
handle.CollectActiveSources(sources);
|
||||||
foreach (AudioSource src in sources)
|
foreach (AudioSource src in sources)
|
||||||
|
|||||||
@@ -20,7 +20,6 @@ namespace OCES.Audio
|
|||||||
// 当前正在播放的句柄
|
// 当前正在播放的句柄
|
||||||
ContainerPlayHandle m_currentHandle;
|
ContainerPlayHandle m_currentHandle;
|
||||||
uint m_currentContainerId;
|
uint m_currentContainerId;
|
||||||
float m_currentVolume = 1f;
|
|
||||||
|
|
||||||
// 当前播放的 Container(用于读取 bpm/timeSig 做节拍对齐)
|
// 当前播放的 Container(用于读取 bpm/timeSig 做节拍对齐)
|
||||||
MusicContainer m_currentContainer;
|
MusicContainer m_currentContainer;
|
||||||
|
|||||||
@@ -760,23 +760,23 @@ PlayerSettings:
|
|||||||
webGLMemoryGeometricGrowthCap: 96
|
webGLMemoryGeometricGrowthCap: 96
|
||||||
webGLPowerPreference: 2
|
webGLPowerPreference: 2
|
||||||
scriptingDefineSymbols:
|
scriptingDefineSymbols:
|
||||||
Android: DOTWEEN;DOTWEEN_EPO
|
Android: DOTWEEN
|
||||||
EmbeddedLinux: DOTWEEN;DOTWEEN_EPO
|
EmbeddedLinux: DOTWEEN
|
||||||
GameCoreScarlett: DOTWEEN;DOTWEEN_EPO
|
GameCoreScarlett: DOTWEEN
|
||||||
GameCoreXboxOne: DOTWEEN;DOTWEEN_EPO
|
GameCoreXboxOne: DOTWEEN
|
||||||
LinuxHeadlessSimulation: DOTWEEN;DOTWEEN_EPO
|
LinuxHeadlessSimulation: DOTWEEN
|
||||||
Nintendo Switch: DOTWEEN;DOTWEEN_EPO
|
Nintendo Switch: DOTWEEN
|
||||||
PS4: DOTWEEN;DOTWEEN_EPO
|
PS4: DOTWEEN
|
||||||
PS5: DOTWEEN;DOTWEEN_EPO
|
PS5: DOTWEEN
|
||||||
QNX: DOTWEEN;DOTWEEN_EPO
|
QNX: DOTWEEN
|
||||||
Stadia: DOTWEEN;DOTWEEN_EPO
|
Stadia: DOTWEEN
|
||||||
Standalone: DOTWEEN;DOTWEEN_EPO
|
Standalone: DOTWEEN
|
||||||
VisionOS: DOTWEEN;DOTWEEN_EPO
|
VisionOS: DOTWEEN
|
||||||
WebGL: DOTWEEN;DOTWEEN_EPO
|
WebGL: DOTWEEN
|
||||||
Windows Store Apps: DOTWEEN;DOTWEEN_EPO
|
Windows Store Apps: DOTWEEN
|
||||||
XboxOne: DOTWEEN;DOTWEEN_EPO
|
XboxOne: DOTWEEN
|
||||||
iPhone: DOTWEEN;DOTWEEN_EPO
|
iPhone: DOTWEEN
|
||||||
tvOS: DOTWEEN;DOTWEEN_EPO
|
tvOS: DOTWEEN
|
||||||
additionalCompilerArguments: {}
|
additionalCompilerArguments: {}
|
||||||
platformArchitecture: {}
|
platformArchitecture: {}
|
||||||
scriptingBackend: {}
|
scriptingBackend: {}
|
||||||
|
|||||||
Reference in New Issue
Block a user