private void SetSourceProperties(AudioSource audioSource) { audioSource.playOnAwake = false; switch (audioEvent.spatialization) { case SpatialPositioningType.TwoD: audioSource.spatialBlend = 0f; audioSource.spatialize = false; break; case SpatialPositioningType.ThreeD: audioSource.spatialBlend = 1f; audioSource.spatialize = false; break; case SpatialPositioningType.SpatialSound: audioSource.spatialBlend = 1f; audioSource.spatialize = true; break; default: Debug.LogErrorFormat("Unexpected spatialization type: {0}", audioEvent.spatialization.ToString()); break; } if (audioEvent.spatialization == SpatialPositioningType.SpatialSound) { //audioSource.SetCustomCurve(AudioSourceCurveType.CustomRolloff, ActiveEvent.SpatialRolloff); SpatialSoundSettings.SetRoomSize(audioSource, audioEvent.roomSize); //SpatialSoundSettings.SetMinGain(audioSource, audioEvent.minGain); //SpatialSoundSettings.SetMaxGain(audioSource, audioEvent.maxGain); //SpatialSoundSettings.SetUnityGainDistance(audioSource, audioEvent.unityGainDistance); audioSource.rolloffMode = AudioRolloffMode.Logarithmic; audioSource.maxDistance = 10;// audioEvent.maxDistanceAttenuation3D; audioSource.SetCustomCurve(AudioSourceCurveType.CustomRolloff, audioEvent.attenuationCurve); audioSource.SetCustomCurve(AudioSourceCurveType.SpatialBlend, audioEvent.spatialCurve); audioSource.SetCustomCurve(AudioSourceCurveType.Spread, audioEvent.spreadCurve); audioSource.SetCustomCurve(AudioSourceCurveType.ReverbZoneMix, audioEvent.reverbCurve); } else if (audioEvent.spatialization == SpatialPositioningType.ThreeD) { audioSource.rolloffMode = AudioRolloffMode.Custom; audioSource.maxDistance = audioEvent.maxDistanceAttenuation3D; audioSource.SetCustomCurve(AudioSourceCurveType.CustomRolloff, audioEvent.attenuationCurve); audioSource.SetCustomCurve(AudioSourceCurveType.SpatialBlend, audioEvent.spatialCurve); audioSource.SetCustomCurve(AudioSourceCurveType.Spread, audioEvent.spreadCurve); audioSource.SetCustomCurve(AudioSourceCurveType.ReverbZoneMix, audioEvent.reverbCurve); } else { audioSource.rolloffMode = AudioRolloffMode.Logarithmic; } if (audioEvent.bus != null) { audioSource.outputAudioMixerGroup = audioEvent.bus; } audioSource.pitch = pitchDest; if (audioEvent.fadeInTime > 0) { audioSource.volume = 0f; } else { audioSource.volume = volDest; } audioSource.panStereo = panDest; }
/// <summary> /// Set the volume, spatialization, etc., on our AudioSources to match the settings on the event to play. /// </summary> private void SetSourceProperties() { Action <Action <AudioSource> > forEachSource = (action) => { action(PrimarySource); if (SecondarySource != null) { action(SecondarySource); } }; AudioEvent audioEvent = this.audioEvent; switch (audioEvent.spatialization) { case SpatialPositioningType.TwoD: forEachSource((source) => { source.spatialBlend = 0f; source.spatialize = false; }); break; case SpatialPositioningType.ThreeD: forEachSource((source) => { source.spatialBlend = 1f; source.spatialize = false; }); break; case SpatialPositioningType.SpatialSound: forEachSource((source) => { source.spatialBlend = 1f; source.spatialize = true; }); break; default: Debug.LogErrorFormat("Unexpected spatialization type: {0}", audioEvent.spatialization.ToString()); break; } if (audioEvent.spatialization == SpatialPositioningType.SpatialSound) { CreateFlatSpatialRolloffCurve(); forEachSource((source) => { source.rolloffMode = AudioRolloffMode.Custom; source.SetCustomCurve(AudioSourceCurveType.CustomRolloff, SpatialRolloff); SpatialSoundSettings.SetRoomSize(source, audioEvent.roomSize); SpatialSoundSettings.SetMinGain(source, audioEvent.minGain); SpatialSoundSettings.SetMaxGain(source, audioEvent.maxGain); SpatialSoundSettings.SetUnityGainDistance(source, audioEvent.unityGainDistance); }); } else { forEachSource((source) => { source.rolloffMode = AudioRolloffMode.Logarithmic; }); } if (audioEvent.bus != null) { forEachSource((source) => source.outputAudioMixerGroup = audioEvent.bus); } float pitch = 1f; if (audioEvent.pitchRandomization != 0) { pitch = UnityEngine.Random.Range(audioEvent.pitchCenter - audioEvent.pitchRandomization, audioEvent.pitchCenter + audioEvent.pitchRandomization); } else { pitch = audioEvent.pitchCenter; } forEachSource((source) => source.pitch = pitch); float vol = 1f; if (audioEvent.fadeInTime > 0) { forEachSource((source) => source.volume = 0f); this.currentFade = audioEvent.fadeInTime; if (audioEvent.volumeRandomization != 0) { vol = UnityEngine.Random.Range(audioEvent.volumeCenter - audioEvent.volumeRandomization, audioEvent.volumeCenter + audioEvent.volumeRandomization); } else { vol = audioEvent.volumeCenter; } this.volDest = vol; } else { if (audioEvent.volumeRandomization != 0) { vol = UnityEngine.Random.Range(audioEvent.volumeCenter - audioEvent.volumeRandomization, audioEvent.volumeCenter + audioEvent.volumeRandomization); } else { vol = audioEvent.volumeCenter; } forEachSource((source) => source.volume = vol); } float pan = audioEvent.panCenter; if (audioEvent.panRandomization != 0) { pan = UnityEngine.Random.Range(audioEvent.panCenter - audioEvent.panRandomization, audioEvent.panCenter + audioEvent.panRandomization); } forEachSource((source) => source.panStereo = pan); }
/// <summary> /// Set the volume, spatialization, etc., on our AudioSources to match the settings on the event to play. /// </summary> private void SetSourceProperties() { Action <Action <AudioSource> > forEachSource = (action) => { action(PrimarySource); if (SecondarySource != null) { action(SecondarySource); } }; AudioEvent audioEvent = AudioEvent; switch (audioEvent.Spatialization) { case SpatialPositioningType.TwoD: forEachSource((source) => { source.spatialBlend = 0f; source.spatialize = false; }); break; case SpatialPositioningType.ThreeD: forEachSource((source) => { source.spatialBlend = 1f; source.spatialize = false; }); break; case SpatialPositioningType.SpatialSound: forEachSource((source) => { source.spatialBlend = 1f; source.spatialize = true; }); break; default: Debug.LogErrorFormat("Unexpected spatialization type: {0}", audioEvent.Spatialization.ToString()); break; } if (audioEvent.Spatialization == SpatialPositioningType.SpatialSound) { forEachSource((source) => { SpatialSoundSettings.SetRoomSize(source, audioEvent.RoomSize); source.rolloffMode = AudioRolloffMode.Custom; source.maxDistance = audioEvent.MaxDistanceAttenuation3D; source.SetCustomCurve(AudioSourceCurveType.CustomRolloff, audioEvent.AttenuationCurve); }); } else { forEachSource((source) => { if (audioEvent.Spatialization == SpatialPositioningType.ThreeD) { source.rolloffMode = AudioRolloffMode.Custom; source.maxDistance = audioEvent.MaxDistanceAttenuation3D; source.SetCustomCurve(AudioSourceCurveType.CustomRolloff, audioEvent.AttenuationCurve); source.SetCustomCurve(AudioSourceCurveType.SpatialBlend, audioEvent.SpatialCurve); source.SetCustomCurve(AudioSourceCurveType.Spread, audioEvent.SpreadCurve); source.SetCustomCurve(AudioSourceCurveType.ReverbZoneMix, audioEvent.ReverbCurve); } else { source.rolloffMode = AudioRolloffMode.Logarithmic; } }); } if (audioEvent.AudioBus != null) { forEachSource((source) => source.outputAudioMixerGroup = audioEvent.AudioBus); } float pitch = 1f; if (audioEvent.PitchRandomization != 0) { pitch = UnityEngine.Random.Range(audioEvent.PitchCenter - audioEvent.PitchRandomization, audioEvent.PitchCenter + audioEvent.PitchRandomization); } else { pitch = audioEvent.PitchCenter; } forEachSource((source) => source.pitch = pitch); float vol = 1f; if (audioEvent.FadeInTime > 0) { forEachSource((source) => source.volume = 0f); CurrentFade = audioEvent.FadeInTime; if (audioEvent.VolumeRandomization != 0) { vol = UnityEngine.Random.Range(audioEvent.VolumeCenter - audioEvent.VolumeRandomization, audioEvent.VolumeCenter + audioEvent.VolumeRandomization); } else { vol = audioEvent.VolumeCenter; } VolDest = vol; } else { if (audioEvent.VolumeRandomization != 0) { vol = UnityEngine.Random.Range(audioEvent.VolumeCenter - audioEvent.VolumeRandomization, audioEvent.VolumeCenter + audioEvent.VolumeRandomization); } else { vol = audioEvent.VolumeCenter; } forEachSource((source) => source.volume = vol); } float pan = audioEvent.PanCenter; if (audioEvent.PanRandomization != 0) { pan = UnityEngine.Random.Range(audioEvent.PanCenter - audioEvent.PanRandomization, audioEvent.PanCenter + audioEvent.PanRandomization); } forEachSource((source) => source.panStereo = pan); }