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