FMOD功能迁移
This commit is contained in:
@@ -1,306 +0,0 @@
|
||||
using System.Collections;
|
||||
using AibisDream.Kit;
|
||||
using UnityEngine;
|
||||
using Yarn.Unity;
|
||||
|
||||
namespace AibisDream
|
||||
{
|
||||
public class AudioManager : Singleton<AudioManager>
|
||||
{
|
||||
private const string LoopSource = "Loop";
|
||||
private const string LoopSource2 = "Loop2";
|
||||
private const string LoopSource3 = "Loop3";
|
||||
private const string InteractionSource = "Interaction";
|
||||
private const string VoiceSource = "Voice";
|
||||
private const string MemorySource = "Memery";
|
||||
private const string Amb1Source = "Amb1";
|
||||
private const string Amb2Source = "Amb2";
|
||||
|
||||
private AudioSource _loopSource;
|
||||
private AudioSource _loopSource2;
|
||||
private AudioSource _loopSource3;
|
||||
private AudioSource _interactionSource;
|
||||
private AudioSource _voiceSource;
|
||||
private AudioSource _memorySource;
|
||||
private AudioSource _amb1Source;
|
||||
private AudioSource _amb2Source;
|
||||
|
||||
public float fadeTime;
|
||||
|
||||
public override void OnSingletonInit()
|
||||
{
|
||||
_loopSource = transform.Find(LoopSource).GetComponent<AudioSource>();
|
||||
_loopSource2 = transform.Find(LoopSource2).GetComponent<AudioSource>();
|
||||
_loopSource3 = transform.Find(LoopSource3).GetComponent<AudioSource>();
|
||||
_interactionSource = transform.Find(InteractionSource).GetComponent<AudioSource>();
|
||||
_voiceSource = transform.Find(VoiceSource).GetComponent<AudioSource>();
|
||||
_memorySource = transform.Find(MemorySource).GetComponent<AudioSource>();
|
||||
_amb1Source = transform.Find(Amb1Source).GetComponent<AudioSource>();
|
||||
_amb2Source = transform.Find(Amb2Source).GetComponent<AudioSource>();
|
||||
}
|
||||
|
||||
#region 随机播放
|
||||
|
||||
public void RandomPlayVoice(string voicePath)
|
||||
{
|
||||
AudioClip clip = RandomLoadAudioClip(3, $"Audio/{voicePath}");
|
||||
_voiceSource.clip = clip;
|
||||
_voiceSource.Play();
|
||||
}
|
||||
[YarnCommand("mute")]
|
||||
public static void MuteInteract(bool isMute)
|
||||
{
|
||||
Instance._interactionSource.mute=isMute;
|
||||
}
|
||||
|
||||
[YarnCommand("random_play_interaction")]
|
||||
public static void RandomPlayInteraction(string interactionName)
|
||||
{
|
||||
AudioClip clip = Instance.RandomLoadAudioClip(3, $"Audio/{interactionName}");
|
||||
if (clip == null)
|
||||
{
|
||||
clip = Resources.Load<AudioClip>($"Audio/{interactionName}");
|
||||
}
|
||||
|
||||
Instance._interactionSource.clip = clip;
|
||||
Instance._interactionSource.Play();
|
||||
}
|
||||
|
||||
[YarnCommand("play_interaction")]
|
||||
public static void PlayInteraction(string name)
|
||||
{
|
||||
var clip = Resources.Load<AudioClip>($"Audio/{name}");
|
||||
Instance._interactionSource.clip = clip;
|
||||
Instance._interactionSource.Play();
|
||||
}
|
||||
|
||||
public void StopVoice()
|
||||
{
|
||||
if (_voiceSource.isPlaying)
|
||||
{
|
||||
_voiceSource.Stop();
|
||||
}
|
||||
}
|
||||
|
||||
[YarnCommand("stop_interaction")]
|
||||
public static void StopInteraction()
|
||||
{
|
||||
Instance._interactionSource.Stop();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 循环播放
|
||||
|
||||
[YarnCommand("play_loop_audio")]
|
||||
public static void PlayLoopAudio(string audioName)
|
||||
{
|
||||
AudioClip clip = Resources.Load<AudioClip>($"Audio/{audioName}");
|
||||
if (clip == null)
|
||||
{
|
||||
clip = Instance.RandomLoadAudioClip(3, $"Audio/{audioName}");
|
||||
}
|
||||
|
||||
Instance._loopSource.clip = clip;
|
||||
Instance._loopSource.Play();
|
||||
}
|
||||
|
||||
[YarnCommand("pause_loop_audio")]
|
||||
public static void PauseLoopAudio()
|
||||
{
|
||||
Instance._loopSource.Pause();
|
||||
}
|
||||
|
||||
[YarnCommand("play_loop2_audio")]
|
||||
public static void PlayLoop2Audio(string audioName)
|
||||
{
|
||||
AudioClip clip = Resources.Load<AudioClip>($"Audio/{audioName}");
|
||||
if (clip == null)
|
||||
{
|
||||
clip = Instance.RandomLoadAudioClip(3, $"Audio/{audioName}");
|
||||
}
|
||||
|
||||
Instance._loopSource2.clip = clip;
|
||||
Instance._loopSource2.Play();
|
||||
}
|
||||
|
||||
[YarnCommand("pause_loop2_audio")]
|
||||
public static void PauseLoop2Audio()
|
||||
{
|
||||
Instance._loopSource2.Pause();
|
||||
}
|
||||
|
||||
[YarnCommand("play_loop3_audio")]
|
||||
public static void PlayLoop3Audio(string audioName)
|
||||
{
|
||||
AudioClip clip = Resources.Load<AudioClip>($"Audio/{audioName}");
|
||||
if (clip == null)
|
||||
{
|
||||
clip = Instance.RandomLoadAudioClip(3, $"Audio/{audioName}");
|
||||
}
|
||||
|
||||
Instance._loopSource3.clip = clip;
|
||||
Instance._loopSource3.Play();
|
||||
}
|
||||
|
||||
[YarnCommand("pause_loop3_audio")]
|
||||
public static void PauseLoop3Audio()
|
||||
{
|
||||
Instance._loopSource3.Pause();
|
||||
}
|
||||
|
||||
|
||||
public void ContinueLoopAudio()
|
||||
{
|
||||
_loopSource.UnPause();
|
||||
}
|
||||
|
||||
public bool IsLoopPlaying()
|
||||
{
|
||||
return _loopSource.isPlaying;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 背景音循环播放
|
||||
|
||||
[YarnCommand("play_amb1_audio")]
|
||||
public static void PlayAmb1Audio(string audioName)
|
||||
{
|
||||
Instance._amb1Source.Stop();
|
||||
AudioClip clip = Resources.Load<AudioClip>($"Audio/{audioName}");
|
||||
Instance._amb1Source.clip = clip;
|
||||
Instance._amb1Source.volume = 0f;
|
||||
Instance._amb1Source.Play();
|
||||
Instance.StartCoroutine(Instance.FadeTo(1.0f, Instance.fadeTime, Instance._amb1Source));
|
||||
}
|
||||
|
||||
[YarnCommand("pause_amb1_audio")]
|
||||
public static void PauseAmb1Audio()
|
||||
{
|
||||
Instance._amb1Source.Pause();
|
||||
}
|
||||
|
||||
[YarnCommand("fade_out_amb1_audio")]
|
||||
public static void FadeOutAmb1Audio()
|
||||
{
|
||||
Instance.StartCoroutine(Instance.FadeTo(0.0f, Instance.fadeTime, Instance._amb1Source));
|
||||
}
|
||||
|
||||
public void ContinueAmb1Audio()
|
||||
{
|
||||
_amb1Source.volume = 0.0f;
|
||||
_amb1Source.UnPause();
|
||||
StartCoroutine(FadeTo(1.0f, fadeTime, _amb1Source));
|
||||
}
|
||||
|
||||
[YarnCommand("play_amb2_audio")]
|
||||
public static void PlayAmb2Audio(string audioName)
|
||||
{
|
||||
Instance._amb2Source.Stop();
|
||||
AudioClip clip = Resources.Load<AudioClip>($"Audio/{audioName}");
|
||||
Instance._amb2Source.clip = clip;
|
||||
Instance._amb2Source.volume = 0f;
|
||||
Instance._amb2Source.Play();
|
||||
Instance.StartCoroutine(Instance.FadeTo(1.0f, Instance.fadeTime, Instance._amb2Source));
|
||||
}
|
||||
|
||||
[YarnCommand("pause_amb2_audio")]
|
||||
public static void PauseAmb2Audio()
|
||||
{
|
||||
Instance._amb2Source.Pause();
|
||||
}
|
||||
|
||||
[YarnCommand("fade_out_amb2_audio")]
|
||||
public static void FadeOutAmb2Audio()
|
||||
{
|
||||
Instance.StartCoroutine(Instance.FadeTo(0.0f, Instance.fadeTime, Instance._amb2Source));
|
||||
}
|
||||
|
||||
public void ContinueAmb2Audio()
|
||||
{
|
||||
_amb2Source.volume = 0.0f;
|
||||
_amb2Source.UnPause();
|
||||
StartCoroutine(FadeTo(1.0f, fadeTime, _amb2Source));
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 播放记忆
|
||||
|
||||
public static void PlayMemoryAudio(string memoryName)
|
||||
{
|
||||
// Instance.StartCoroutine(AsyncPlayMemoryAudio(memoryName));
|
||||
AudioClip loop = Resources.Load<AudioClip>($"Audio/{memoryName}");
|
||||
Instance._memorySource.clip = loop;
|
||||
Instance._memorySource.loop = true;
|
||||
Instance._memorySource.Play();
|
||||
}
|
||||
|
||||
public static void PauseMemoryAudio()
|
||||
{
|
||||
Instance._memorySource.Pause();
|
||||
}
|
||||
|
||||
public void ContinueMemoryAudio()
|
||||
{
|
||||
_memorySource.volume = 0.0f;
|
||||
_memorySource.Play();
|
||||
StartCoroutine(FadeTo(1.0f, fadeTime, _memorySource));
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
public void FadeInLoopAudio(string audioName)
|
||||
{
|
||||
AudioClip clip = Resources.Load<AudioClip>(audioName);
|
||||
_loopSource.clip = clip;
|
||||
_loopSource.volume = 0.0f;
|
||||
_loopSource.Play();
|
||||
StartCoroutine(FadeTo(1.0f, 5f, _loopSource));
|
||||
}
|
||||
|
||||
private IEnumerator FadeTo(float targetVolume, float duration, AudioSource audioSource)
|
||||
{
|
||||
float startVolume = audioSource.volume;
|
||||
float timeElapsed = 0f;
|
||||
|
||||
while (timeElapsed < duration)
|
||||
{
|
||||
timeElapsed += Time.deltaTime;
|
||||
audioSource.volume = Mathf.Lerp(startVolume, targetVolume, timeElapsed / duration);
|
||||
yield return null;
|
||||
}
|
||||
|
||||
audioSource.volume = targetVolume;
|
||||
}
|
||||
|
||||
private AudioClip RandomLoadAudioClip(int audioSize, string path)
|
||||
{
|
||||
int randomIndex = Random.Range(0, audioSize) + 1;
|
||||
return Resources.Load<AudioClip>($"{path}_{randomIndex}");
|
||||
}
|
||||
|
||||
private static IEnumerator AsyncPlayMemoryAudio(string memoryName)
|
||||
{
|
||||
AudioClip intro = Resources.Load<AudioClip>($"Audio/mem_play");
|
||||
AudioClip loop = Resources.Load<AudioClip>($"Audio/{memoryName}");
|
||||
Instance._memorySource.clip = intro;
|
||||
Instance._memorySource.loop = false;
|
||||
Instance._memorySource.Play();
|
||||
yield return new WaitForSeconds(intro.length);
|
||||
Instance._memorySource.clip = loop;
|
||||
Instance._memorySource.loop = true;
|
||||
Instance._memorySource.Play();
|
||||
}
|
||||
|
||||
public static void SetLoopSourceVolume(float num)
|
||||
{
|
||||
Instance._loopSource.volume=num;
|
||||
}
|
||||
public static void SetinteractionSourceVolume(float num)
|
||||
{
|
||||
Instance._interactionSource.volume=num;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d02d13e0ef27eb544b62d5a8d296f2ee
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -9,9 +9,9 @@ namespace AibisDream.Kit
|
||||
private static IAudioManager AudioManager => FmodManager.Instance;
|
||||
|
||||
[YarnCommand("play_music")]
|
||||
public static void PlayMusic(string eventName)
|
||||
public static void PlayMusic(string eventName, string audioKey = null)
|
||||
{
|
||||
AudioManager.PlayMusic(eventName);
|
||||
AudioManager.PlayMusic(eventName, audioKey);
|
||||
}
|
||||
|
||||
[YarnCommand("stop_music")]
|
||||
@@ -34,9 +34,9 @@ namespace AibisDream.Kit
|
||||
}
|
||||
|
||||
[YarnCommand("play_sfx")]
|
||||
public static void PlaySfx(string eventName)
|
||||
public static void PlaySfx(string eventName, string audioKey = null)
|
||||
{
|
||||
AudioManager.PlaySfx(eventName);
|
||||
AudioManager.PlaySfx(eventName, audioKey);
|
||||
}
|
||||
|
||||
[YarnCommand("stop_sfx")]
|
||||
|
||||
@@ -89,36 +89,36 @@ namespace AibisDream.Kit
|
||||
|
||||
#region 音乐相关
|
||||
|
||||
public void PlayMusic(string eventName)
|
||||
public void PlayMusic(string eventName, string audioKey = null)
|
||||
{
|
||||
var eventPath = FormatEventPath(eventName);
|
||||
if (_musicDict.TryGetValue(eventPath, out var oldMusic))
|
||||
|
||||
var key = string.IsNullOrEmpty(audioKey) ? eventName : audioKey;
|
||||
if (_musicDict.TryGetValue(key, out var oldMusic))
|
||||
{
|
||||
oldMusic.start();
|
||||
}
|
||||
else
|
||||
{
|
||||
var newMusic = RuntimeManager.CreateInstance(eventPath);
|
||||
_musicDict[eventPath] = newMusic;
|
||||
newMusic.start();
|
||||
oldMusic.stop(STOP_MODE.IMMEDIATE);
|
||||
oldMusic.release();
|
||||
_musicDict.Remove(key);
|
||||
}
|
||||
|
||||
var newMusic = RuntimeManager.CreateInstance(eventPath);
|
||||
_musicDict[key] = newMusic;
|
||||
newMusic.start();
|
||||
}
|
||||
|
||||
public void StopMusic(string eventName, STOP_MODE stopMode)
|
||||
public void StopMusic(string key, STOP_MODE stopMode)
|
||||
{
|
||||
var eventPath = FormatEventPath(eventName);
|
||||
if (_musicDict.TryGetValue(eventPath, out var oldMusic))
|
||||
if (_musicDict.TryGetValue(key, out var oldMusic))
|
||||
{
|
||||
oldMusic.stop(stopMode);
|
||||
oldMusic.release();
|
||||
_musicDict.Remove(eventPath);
|
||||
_musicDict.Remove(key);
|
||||
}
|
||||
}
|
||||
|
||||
public void SetMusicParam(string eventName, string param, float paramValue)
|
||||
public void SetMusicParam(string key, string param, float paramValue)
|
||||
{
|
||||
var eventPath = FormatEventPath(eventName);
|
||||
if (_musicDict.TryGetValue(eventPath, out var music))
|
||||
if (_musicDict.TryGetValue(key, out var music))
|
||||
{
|
||||
music.setParameterByName(param, paramValue);
|
||||
}
|
||||
@@ -152,40 +152,39 @@ namespace AibisDream.Kit
|
||||
}
|
||||
}
|
||||
|
||||
public void PlaySfx(string eventName)
|
||||
public void PlaySfx(string eventName, string audioKey = null)
|
||||
{
|
||||
var eventPath = FormatEventPath(eventName);
|
||||
if (_sfxDict.TryGetValue(eventPath, out var oldSfx))
|
||||
var key = string.IsNullOrEmpty(audioKey) ? eventName : audioKey;
|
||||
if (_sfxDict.TryGetValue(key, out var oldSfx))
|
||||
{
|
||||
oldSfx.start();
|
||||
oldSfx.stop(STOP_MODE.IMMEDIATE);
|
||||
oldSfx.release();
|
||||
_sfxDict.Remove(key);
|
||||
}
|
||||
else
|
||||
|
||||
var newSfx = RuntimeManager.CreateInstance(eventPath);
|
||||
if (newSfx.GetDescription().isOneshot(out var isOneshot) != RESULT.OK || !isOneshot)
|
||||
{
|
||||
var newSfx = RuntimeManager.CreateInstance(eventPath);
|
||||
if (newSfx.GetDescription().isOneshot(out var isOneshot) != RESULT.OK || !isOneshot)
|
||||
{
|
||||
_sfxDict[eventPath] = newSfx;
|
||||
}
|
||||
_sfxDict[key] = newSfx;
|
||||
}
|
||||
|
||||
newSfx.start();
|
||||
}
|
||||
newSfx.start();
|
||||
}
|
||||
|
||||
public void StopSfx(string eventName, STOP_MODE stopMode)
|
||||
public void StopSfx(string key, STOP_MODE stopMode)
|
||||
{
|
||||
var eventPath = FormatEventPath(eventName);
|
||||
if (_sfxDict.TryGetValue(eventPath, out var oldSfx))
|
||||
if (_sfxDict.TryGetValue(key, out var oldSfx))
|
||||
{
|
||||
oldSfx.stop(stopMode);
|
||||
oldSfx.release();
|
||||
_musicDict.Remove(eventPath);
|
||||
_musicDict.Remove(key);
|
||||
}
|
||||
}
|
||||
|
||||
public void SetSfxParam(string eventName, string paramName, float paramValue)
|
||||
public void SetSfxParam(string key, string paramName, float paramValue)
|
||||
{
|
||||
var eventPath = FormatEventPath(eventName);
|
||||
if (_sfxDict.TryGetValue(eventPath, out var sfx))
|
||||
if (_sfxDict.TryGetValue(key, out var sfx))
|
||||
{
|
||||
sfx.setParameterByName(paramName, paramValue);
|
||||
}
|
||||
@@ -195,7 +194,7 @@ namespace AibisDream.Kit
|
||||
|
||||
private static string FormatEventPath(string eventName)
|
||||
{
|
||||
return $"event:/{eventName}";
|
||||
return eventName.StartsWith("event:/") ? eventName : $"event:/{eventName}";
|
||||
}
|
||||
|
||||
private static AmbState MatchAmbState(string key)
|
||||
@@ -234,7 +233,7 @@ namespace AibisDream.Kit
|
||||
public interface IAudioManager
|
||||
{
|
||||
// Yarn音频接口
|
||||
public void PlayMusic(string eventName);
|
||||
public void PlayMusic(string eventName, string audioKey);
|
||||
public void StopMusic(string eventName, STOP_MODE stopMode);
|
||||
public void SetMusicParam(string eventName, string paramName, float paramValue);
|
||||
|
||||
@@ -247,7 +246,7 @@ namespace AibisDream.Kit
|
||||
public void OnLineShown();
|
||||
|
||||
// 交互相关
|
||||
public void PlaySfx(string eventName);
|
||||
public void PlaySfx(string eventName, string audioKey);
|
||||
public void StopSfx(string eventName, STOP_MODE stopMode);
|
||||
public void SetSfxParam(string eventName, string paramName, float paramValue);
|
||||
}
|
||||
|
||||
@@ -51,7 +51,7 @@ namespace AibisDream.Kit
|
||||
{
|
||||
get
|
||||
{
|
||||
if (!voicePath.StartsWith("event:/"))
|
||||
if (!string.IsNullOrEmpty(voicePath) && !voicePath.StartsWith("event:/"))
|
||||
{
|
||||
return "event:/" + voicePath;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user