TouhouLS/Assets/Code/AudioProvider.cs

44 lines
1.3 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using EntityNetwork;
public class AudioProvider : EntityBase, IAutoRegister
{
public static AudioProvider Instance;
public override void Awake() {
base.Awake();
Instance = this;
bgmSrc = GetComponent<AudioSource>();
}
public float musicSpeed = 1f;
AudioSource bgmSrc;
public void Update() {
// TODO - This is WIP help
//bgmSrc.outputAudioMixerGroup.
}
int lastAudioFrame = 0;
public static void PlaySFX(AudioClip sfx, AudioHelper.AudioCategory category = AudioHelper.AudioCategory.Fx, float volume = 1.0f, bool sendRemote = true) {
if (sfx == null) return;
if (sendRemote)
Instance.RaiseEvent('s', true, AudioHelper.ByClip(sfx), (byte)category, volume);
else
Instance.InternallyPlaySFXEvent(AudioHelper.ByClip(sfx), (byte)category, volume);
}
[NetEvent('s')]
public void InternallyPlaySFXEvent(int sfxID, byte category, float volume) {
// Don't allow multiple audio sources to trigger at once
if (Time.frameCount == lastAudioFrame) return;
lastAudioFrame = Time.frameCount;
var clip = AudioHelper.ByID(sfxID);
AudioSource.PlayClipAtPoint(clip, Camera.main.transform.position, AudioHelper.VolumeLevel((AudioHelper.AudioCategory)category) * volume);
}
}