public void PlaySmooth(SoundSettings settings, float smoothTime) { if (_isPlaying) { return; } Play(settings); var targetVolume = _audioSource.volume; Timer.Timer.Register(smoothTime, delegate { }, x => { var roundTime = x / smoothTime; roundTime = roundTime.Remap(0, 1, 0, targetVolume); _audioSource.volume = roundTime; }); }
public void Play(SoundSettings settings) { if (_isPlaying) { return; } _audioSource.clip = settings.ClipsToPlay[Random.Range(0, settings.ClipsToPlay.Length - 1)]; _audioSource.pitch = settings.Pitch.Random(); _audioSource.volume = settings.Volume.Random(); _audioSource.outputAudioMixerGroup = settings.AudioMixerGroup; _audioSource.spatialBlend = settings.Is3DSound ? 1f : 0f; _audioSource.spread = settings.Is3DSound ? -360f : 0f; _audioSource.minDistance = settings.Distance3D.x; _audioSource.maxDistance = settings.Distance3D.y; _audioSource.SetCustomCurve(AudioSourceCurveType.CustomRolloff, settings.VolumeChangeByDistance); _audioSource.Play(); DisableOnSoundEnded(); _isPlaying = true; }