- New occlusion opt-in on SoundEffect: spatialized instances get a SteamAudioSource with raycast occlusion + frequency-dependent transmission (walls muffle instead of hard-mute) - Replace SoundEffectDistanceRange asset indirection with an explicit _3d bool (user refactor); rolloff/distance now left to defaults - Hey.asset: occlusion enabled Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
70 lines
1.9 KiB
C#
70 lines
1.9 KiB
C#
using System.Collections;
|
|
|
|
using UnityEngine;
|
|
|
|
namespace Prototype
|
|
{
|
|
[AddComponentMenu("/")]
|
|
public class SoundEffectInstance : MonoBehaviour
|
|
{
|
|
public AudioClip clip { get; set; }
|
|
public SoundEffectOptions options { get; set; }
|
|
|
|
AudioSource audioSource;
|
|
|
|
void Awake()
|
|
{
|
|
audioSource = gameObject.AddComponent<AudioSource>();
|
|
{
|
|
audioSource.playOnAwake = false;
|
|
audioSource.dopplerLevel = 0;
|
|
audioSource.outputAudioMixerGroup = options.audioMixerGroup;
|
|
audioSource.clip = clip;
|
|
audioSource.volume = options.GetVolume();
|
|
audioSource.pitch = options.GetPitch();
|
|
|
|
audioSource.spatialBlend = options.Get3D() ? 1 : 0;
|
|
audioSource.spatialize = options.GetSpatialize() && audioSource.spatialBlend > 0;
|
|
audioSource.loop = options.GetLoop();
|
|
}
|
|
|
|
if (audioSource.spatialize && options.GetOcclusion())
|
|
{
|
|
var steamAudioSource = gameObject.AddComponent<SteamAudio.SteamAudioSource>();
|
|
steamAudioSource.occlusion = true;
|
|
steamAudioSource.transmission = true;
|
|
steamAudioSource.transmissionType = SteamAudio.TransmissionType.FrequencyDependent;
|
|
}
|
|
|
|
if (!options.ShouldDestroyOnLoad())
|
|
{
|
|
DontDestroyOnLoad(this);
|
|
}
|
|
}
|
|
|
|
IEnumerator Start()
|
|
{
|
|
var delay = options.GetDelay();
|
|
if (delay > 0)
|
|
{
|
|
yield return new WaitForSeconds(delay);
|
|
}
|
|
|
|
audioSource.Play();
|
|
|
|
if (audioSource.loop)
|
|
{
|
|
yield break;
|
|
}
|
|
|
|
yield return new WaitForSecondsRealtime(audioSource.clip.length + .1f);
|
|
Destroy(gameObject);
|
|
}
|
|
|
|
public void Stop()
|
|
{
|
|
Destroy(gameObject);
|
|
}
|
|
}
|
|
}
|