163 lines
4.4 KiB
C#
163 lines
4.4 KiB
C#
|
using System;
|
|||
|
using System.Collections;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Linq;
|
|||
|
using UnityEngine;
|
|||
|
using UnityEngine.Audio;
|
|||
|
|
|||
|
public class AudioManager : Singleton<AudioManager>
|
|||
|
{
|
|||
|
public AudioClip PraySound;
|
|||
|
public AudioClip FaithPickSound;
|
|||
|
public AudioMixer AudioMixer;
|
|||
|
public List<AudioClip> BGMs = new();
|
|||
|
public List<AudioClip> ChopWoodSFX = new();
|
|||
|
public List<AudioClip> BlobBornSFX = new();
|
|||
|
public List<AudioClip> KowSFX = new();
|
|||
|
|
|||
|
public List<AudioClip> KowtowSFX = new();
|
|||
|
|
|||
|
private List<float> TargetLayerFrequency = new List<float>();
|
|||
|
|
|||
|
public AudioSource BuildingsLayerAudioSource;
|
|||
|
public AudioSource BattleLayerAudioSource;
|
|||
|
public AudioSource PrayLayerAudioSource;
|
|||
|
public AudioSource ShenPuAudioSource;
|
|||
|
|
|||
|
|
|||
|
public AudioClip ForwardSceneSound;
|
|||
|
public AudioClip BackwardSceneSound;
|
|||
|
|
|||
|
public float AudioCoolDownTime = 0.02f;
|
|||
|
public float CurrentCoolDownTime = 0f;
|
|||
|
|
|||
|
public void PlayBgm(int index)
|
|||
|
{
|
|||
|
GetComponent<AudioSource>().PlayOneShot(BGMs[index]);
|
|||
|
}
|
|||
|
|
|||
|
public void PlayFaithGainSFX(float pitch)
|
|||
|
{
|
|||
|
if (CurrentCoolDownTime >= 0) return;
|
|||
|
|
|||
|
CurrentCoolDownTime = AudioCoolDownTime;
|
|||
|
PrayLayerAudioSource.pitch = pitch;
|
|||
|
PrayLayerAudioSource.Stop();
|
|||
|
PrayLayerAudioSource.PlayOneShot(FaithPickSound);
|
|||
|
}
|
|||
|
|
|||
|
public void PlayKowSFX(AudioSource ac)
|
|||
|
{
|
|||
|
ac.PlayOneShot(KowSFX.RandomPick());
|
|||
|
}
|
|||
|
|
|||
|
public AudioClip holySound;
|
|||
|
public void PlayHoly(AudioSource ac)
|
|||
|
{
|
|||
|
ac.PlayOneShot(holySound);
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
public void PlayChopWoodSFX()
|
|||
|
{
|
|||
|
BuildingsLayerAudioSource.PlayOneShot(ChopWoodSFX.RandomPick());
|
|||
|
}
|
|||
|
|
|||
|
public void PlayEnemyChopWoodSFX()
|
|||
|
{
|
|||
|
BattleLayerAudioSource.pitch = 0.2f;
|
|||
|
BattleLayerAudioSource.PlayOneShot(ChopWoodSFX.RandomPick());
|
|||
|
// BuildingsLayerAudioSource.pitch = 1f;
|
|||
|
}
|
|||
|
|
|||
|
public AudioClip hornSound;
|
|||
|
public void SpawnEnemySFX(AudioSource ac)
|
|||
|
{
|
|||
|
ac.PlayOneShot(hornSound);
|
|||
|
}
|
|||
|
public void PlayBlobBornSFX()
|
|||
|
{
|
|||
|
BuildingsLayerAudioSource.PlayOneShot(BlobBornSFX.RandomPick());
|
|||
|
}
|
|||
|
|
|||
|
public void PlayKowtowSFX(AudioSource ac)
|
|||
|
{
|
|||
|
ac.PlayOneShot(KowtowSFX.RandomPick());
|
|||
|
}
|
|||
|
|
|||
|
private void Start()
|
|||
|
{
|
|||
|
TargetLayerFrequency = (from t in SceneTransitionManager.Instance.TransitionPoints select 5000.0f).ToList();
|
|||
|
}
|
|||
|
|
|||
|
// 调整 LowPass 滤波器的 Cutoff Frequency
|
|||
|
public void SetLowPassCutoff(int layerIndex, float cutoffFrequency)
|
|||
|
{
|
|||
|
AudioMixer.SetFloat($"Layer{layerIndex}LowPass", Mathf.Clamp(cutoffFrequency, 10f, 22000f));
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
public float GetLowPassCutoff(int layerIndex)
|
|||
|
{
|
|||
|
float result = 0f;
|
|||
|
if (!AudioMixer.GetFloat($"Layer{layerIndex}LowPass", out result))
|
|||
|
{
|
|||
|
//Debug.LogError($"Audio Mixer没有这个: Layer{layerIndex}LowPass");
|
|||
|
}
|
|||
|
|
|||
|
return result;
|
|||
|
}
|
|||
|
|
|||
|
public void OnSceneTransition(int transitIndex)
|
|||
|
{
|
|||
|
|
|||
|
for (int i = 0; i < SceneTransitionManager.Instance.TransitionPoints.Count; i++)
|
|||
|
{
|
|||
|
TargetLayerFrequency[i] = transitIndex == i ? 5000f : 1000f / Mathf.Abs(transitIndex - i);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public void PlaySFXOneShot()
|
|||
|
{
|
|||
|
}
|
|||
|
|
|||
|
private void Update()
|
|||
|
{
|
|||
|
CurrentCoolDownTime -= Time.deltaTime;
|
|||
|
for (int i = 0; i < SceneTransitionManager.Instance.TransitionPoints.Count; i++)
|
|||
|
{
|
|||
|
SetLowPassCutoff(i, 0.2f * TargetLayerFrequency[i] + 0.8f * GetLowPassCutoff(i));
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public void PlayAccumulateShot()
|
|||
|
{
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
/*private IEnumerator AdjustPitchOverTime()
|
|||
|
{
|
|||
|
audioSource.pitch = 1.0f; // 初始 pitch
|
|||
|
audioSource.loop = false; // 单次播放,协程中控制循环
|
|||
|
|
|||
|
while (audioSource.pitch < maxPitch)
|
|||
|
{
|
|||
|
audioSource.Play(); // 播放音频
|
|||
|
|
|||
|
// 等待音频播放指定的持续时间
|
|||
|
yield return new WaitForSeconds(playDuration);
|
|||
|
|
|||
|
// 增加 pitch,但不超过 maxPitch
|
|||
|
audioSource.pitch = Mathf.Min(audioSource.pitch + pitchIncrement, maxPitch);
|
|||
|
|
|||
|
// 等待播放间隔
|
|||
|
yield return new WaitForSeconds(delayBetweenPlays);
|
|||
|
}
|
|||
|
|
|||
|
// 到达 maxPitch 后循环播放,不再递增 pitch
|
|||
|
audioSource.pitch = maxPitch;
|
|||
|
audioSource.loop = true;
|
|||
|
audioSource.Play();
|
|||
|
}*/
|
|||
|
}
|