Skip to content

Instantly share code, notes, and snippets.

@made-indrayana
Last active August 6, 2021 14:13
Show Gist options
  • Save made-indrayana/da62dc79e66791d4783fdf93668d65a2 to your computer and use it in GitHub Desktop.
Save made-indrayana/da62dc79e66791d4783fdf93668d65a2 to your computer and use it in GitHub Desktop.
FMOD Programming Crash Course
// FMOD Crash Course for Programmer
// written by Made Indrayana
// MIT License
using UnityEngine;
public class FMODCrashCourse : MonoBehaviour
{
// String as reference with FMOD Picker
[FMODUnity.EventRef] public string eventName;
public GameObject objectToAttachSoundTo;
FMOD.Studio.EventInstance instance;
FMODUnity.StudioEventEmitter eventEmitter;
public void PlayFromEvent(string eventName)
{
// PlayOneShot, would normally use this for non 3D audio
FMODUnity.RuntimeManager.PlayOneShot(eventName);
// PlayOneShotAttached, to make sure the sound is attached to GameObject so localization will be correct for 3D Audio
FMODUnity.RuntimeManager.PlayOneShotAttached(eventName, objectToAttachSoundTo);
//This method will create and release the resources needed for playing automatically, so this is almost always the way to play sounds that are not looped.
}
// To play looping sound that you can reference, you have to create an instance manually.
public void CreateInstanceFromEvent(string eventName)
{
instance = FMODUnity.RuntimeManager.CreateInstance(eventName);
/* If this sound needs to be attached to a GameObject, you will need to set the 3D Attributes
instance.set3DAttributes(FMODUnity.RuntimeUtils.To3DAttributes(objectToAttachSoundTo.transform));
The method above is unreliable because it sets the position only once and actually does not "attach" itself to the GameObject.
What we can do is to actually run another method directly **after** starting the instance.
(If you insist) the quick and dirty way would be to run
instance.set3DAttributes(FMODUnity.RuntimeUtils.To3DAttributes(objectToAttachSoundTo.transform)); in Update(). */
}
//...then start the instance
public void StartInstance(FMOD.Studio.EventInstance instance)
{
instance.start();
// If this sound needs to be attached to a GameObject, you will need to call this method **AFTER** starting the instance
FMODUnity.RuntimeManager.AttachInstanceToGameObject(instance, this.gameObject.transform, this.GetComponent<Rigidbody>())
// If the GameObject does not contain a Rigidbody or Rigidbody2D, you can use GetComponent like this, and it doesn't matter if it returns null.
}
//and stop it if the time has come
public void StopInstance(FMOD.Studio.EventInstance instance)
{
instance.stop(FMOD.Studio.STOP_MODE.ALLOWFADEOUT); // STOP_MODE could also be immediate, that means it will always stop without any fade out.
instance.release(); // Very important to release resources after the sound has stopped! This call is async so it will automatically wait until the sound has stopped and then free resources.
}
// if you prefer to trigger everything through FMOD'S Studio Event Emitter, here you go:
public void StartEmitter(FMODUnity.StudioEventEmitter emitter)
{
emitter.Play();
}
// also to stop it. You do not have to call release if you're using the StudioEventEmitter
public void StopEmitter(FMODUnity.StudioEventEmitter emitter)
{
emitter.Stop();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment