Home > other >  How to detect Unity AudioSource volume change?
How to detect Unity AudioSource volume change?

Time:10-20

I am using Unity AudioSource. It is easy to get and change the volume by accessing AudioSource.volume as explained here https://docs.unity3d.com/ScriptReference/AudioSource-volume.html.

The cases that I want to cover

  • If there is a volume change from the Editor Inspector volume slider
  • If there is a volume change from 3rd party library that has access to AudioSource gameobject

How do I detect the volume change? Is there any callback or event that I can use?

1 thing that I can do is by checking the volume continuously inside Update() , but it looks overkill.

CodePudding user response:

If there is a volume change from the Editor Inspector volume slider How do I detect the volume change? Is there any callback or event that I can use?

*I don't remember any callback or event in there*

Hello! If you´re just looking towards getting the amount that has changed, you can simply do the next

define a float changeAmmount, and get 2 extra float variables lastVolume, actualVolume, to establish and move the values.

lastVolume will not move until you made your calculations, but the actualVolume should be always attached to the Audiosources Volume.

You can run on Update or fixedUpdate or an InvokeRepeating to get this answer the number of times you wanted per second

if you move the slider run

changeAmmount = Actualvolume - lastVolume; 

therefore giving you the result directly of how much your volume has changed. from the last position

Then you can already set your last volume to the actual volume so the threshold goes back to 0.

From pure memory, maybe something is wrong, but it should work.

CodePudding user response:

Unfortunately most of Unity's properties do not have any callbacks by default - and by default means except you use some really dirty hacks ;)

Audiosource.volume is one of those.

You could of course simply attach a second component which only purpose is to track any changes in the volume

// Makes Unity messages be called by both edit mode and play mode
[ExecuteAlways]

[RequireComponent(typeof(AudioSource))]
public class AudioSourceVolumeTrigger : MonoBehaviour
{
    [SerializeField]
    private AudioSource _audioSrouce;

    // In the Inspector make sure to set the event to "Editor And Runtime"
    // Default is "Runtime Only"
    public UnityEvent<float,float> OnVolumeChanged;

    private float _lastVolume;

    private void Awake()
    {
        if (!_audioSrouce) _audioSrouce = GetComponent<AudioSource>();

        _lastVolume = _audioSrouce.volume;
    }

    // PlayMode - called every frame
    // EditMode - called after every change to scene or assets
    private void Update()
    {
        // Has the volume changed?
        if (!Mathf.Approximately(_audioSrouce.volume, _lastVolume))
        {
            // invoke the event 
            OnVolumeChanged?.Invoke(_lastVolume, _audioSrouce.volume);

            // and store the new volume to compare later again
            _lastVolume = _audioSrouce.volume;
        }
    }
}
  • Related