FMOD Unity C# Scripting

tutorialgame-audio-fmodgame-audio-unityscripting

FMOD: Unity C# Scripting

Video Tutorial Script

Scripting Basics

FMOD Namespace

Any script that references FMOD needs to declare that it is using FMOD at the top of the script.

using FMODUnity;

Referencing FMOD Events

Add the following line of code to any script to create a public FMOD event reference variable

cleanshot 2024 04 24 at 14.43.552x

[SerializeField] // Show this variable in Unity's Inspector panel
private EventReference myFmodEvent;  // FMOD Event Reference

Triggering FMOD Events from a script

The following line of code is used to trigger an FMOD event from within a script.

RuntimeManager.PlayOneShot(myFmodEvent);

Triggering FMOD Events via Animations

  1. Add the following script to the desired game object.

    using System.Linq;
    using UnityEngine;
    using FMODUnity;
    
    public class FMODAnimationEventTriggers : MonoBehaviour
    {
        [System.Serializable]
        public struct AnimationEventTriggers
        {
            public string eventName;
            public EventReference fmodEvent;
        }
    
        public AnimationEventTriggers[] animationEventTiggerPairs;
    
        public void FMODAnimationEventTrigger(string eventString)
        {
            AnimationEventTriggers evt = animationEventTiggerPairs.FirstOrDefault(e => e.eventName == eventString);
    
            // If the FMOD event is not null, play the FMOD event
            if (evt.fmodEvent.IsNull == false) // FMOD event is valid, play it
            {
                var instance = RuntimeManager.CreateInstance(evt.fmodEvent);
                instance.set3DAttributes(FMODUnity.RuntimeUtils.To3DAttributes(gameObject));
                instance.start();
                instance.release();
            }
        }
    
    }
  2. Add all FMOD events and assign a unique string to each event.

  3. Add an event to the animation timeline.

  4. Call the FMODAnimationEventTrigger function and pass it the same string as given to the corresponding FMOD event on the script.

  5. Target the game object that is being animated and that the FMOD event should originate from.

Controlling FMOD Parameters via Sliders in Unity

using UnityEngine;
using UnityEngine.UI;
using FMODUnity;

public class FMODSliderParameterControl : MonoBehaviour
{
    // A struct to hold the slider and its corresponding FMOD parameter name.
    [System.Serializable]
    public struct SliderParameterPair
    {
        public Slider soundSlider;
        public string fmodParameterName;
    }

    // Array of SliderParameterPair - each element represents a slider and its corresponding FMOD parameter.
    public SliderParameterPair[] sliderParameterPairs;

    void Start()
    {
        // For each SliderParameterPair in the array, initialize the FMOD parameter and set up the listener for the slider's onValueChanged event.
        foreach (var pair in sliderParameterPairs)
        {
            // Initialize the FMOD parameter with the initial value of the Slider.
            RuntimeManager.StudioSystem.setParameterByName(pair.fmodParameterName, pair.soundSlider.value);

            // Setup the action to be performed whenever the Slider value changes.
            pair.soundSlider.onValueChanged.AddListener((value) => UpdateFMODParameterValue(value, pair.fmodParameterName));
        }
    }

    // A function to update the FMOD parameter value.
    void UpdateFMODParameterValue(float sliderValue, string parameterName)
    {
        RuntimeManager.StudioSystem.setParameterByName(parameterName, sliderValue);
    }
}

Controlling an FMOD Parameter via a Toggle in Unity

using UnityEngine;
using UnityEngine.UI;
using FMODUnity;

public class FMODToggleParameterControl : MonoBehaviour
{
    [System.Serializable]
    public struct ToggleParameterPair
    {
        public Toggle audioToggle;
        public string fmodParameterName;
    }

    public ToggleParameterPair[] toggleParameterPairs;

    void Start()
    {
        foreach (var pair in toggleParameterPairs)
        {
            // Initialize the FMOD parameter with the initial value of the Toggle.
            RuntimeManager.StudioSystem.setParameterByName(pair.fmodParameterName, pair.audioToggle.isOn ? 1.0f : 0.0f);

            // Setup the action to be performed whenever the Toggle value changes.
            pair.audioToggle.onValueChanged.AddListener((value) => UpdateFMODParameterValue(value, pair.fmodParameterName));
        }
    }

    // A function to update the FMOD parameter value.
    void UpdateFMODParameterValue(bool toggleValue, string parameterName)
    {
        // Set FMOD parameter to 1 if Toggle is on, and 0 if off.
        RuntimeManager.StudioSystem.setParameterByName(parameterName, toggleValue ? 1.0f : 0.0f);
    }
}