Hello!
I’m using FMOD for Unity and FMOD Studio, both version 2.02.19!
Please see the sample code below:
using UnityEngine;
using System;
using System.Runtime.InteropServices;
public class Audio_Test : MonoBehaviour
{
public FMOD.Studio.EVENT_CALLBACK callBack;
//public FMODUnity.EventReference eventReference;
void Start()
{
callBack = new FMOD.Studio.EVENT_CALLBACK(CallBackFunction);
PlayAudioFile(@"D:\UnityRepos\Test\1.flac");
}
public void PlayAudioFile(string location)
{
var eventInstance = FMODUnity.RuntimeManager.CreateInstance(FMODUnity.RuntimeManager.PathToGUID(location)); // Here
// ...
}
private static FMOD.RESULT CallBackFunction(FMOD.Studio.EVENT_CALLBACK_TYPE type, IntPtr instancePtr, IntPtr parameterPtr)
{
FMOD.Studio.EventInstance instance = new FMOD.Studio.EventInstance(instancePtr);
if (instance.isValid() == false)
{
Debug.Log("Failed to create instance, instance invalid");
return FMOD.RESULT.ERR_EVENT_NOTFOUND;
}
IntPtr stringPtr;
instance.getUserData(out stringPtr);
GCHandle stringHandle = GCHandle.FromIntPtr(stringPtr);
string location = stringHandle.Target as string;
switch (type)
{
// ...
}
}
In the commented line of code, an error is reported at runtime:
EventNotFoundException: [FMOD] Event not found: ‘D:\UnityRepos\Test\1.flac’
I realized that its asking to pass in the path to the event that was still created in FMOD Studio.
In the original reply, it states in the opening position that it needs to instantiate an FMODUnity.EventReference object:
Create an variable which will be used to instantiate your FMODUnity.EventReferenceProgrammer Instrument from the FMOD Project.
I created an empty event (2D Timeline) in FMOD Studio, generated it and sent it to Unity.
Modified the code as below:
public class Audio_Test : MonoBehaviour
{
public FMOD.Studio.EVENT_CALLBACK callBack;
public FMODUnity.EventReference eventReference;
void Start()
{
callBack = new FMOD.Studio.EVENT_CALLBACK(CallBackFunction);
PlayAudioFile(@"D:\UnityRepos\Test\1.flac");
}
public void PlayAudioFile(string location)
{
var eventInstance = FMODUnity.RuntimeManager.CreateInstance(eventReference);
//var eventInstance = FMODUnity.RuntimeManager.CreateInstance(FMODUnity.RuntimeManager.PathToGUID(location));
FMOD.RESULT result;
if (!eventInstance.isValid())
{
Debug.Log("Failed to create instance, instance invalid");
return;
}
GCHandle stringLocation = GCHandle.Alloc(location);
result = eventInstance.setUserData(GCHandle.ToIntPtr(stringLocation));
if (result != FMOD.RESULT.OK)
{
Debug.Log("Failed to create event instance with a result of " + result);
return;
}
result = eventInstance.setCallback(callBack);
result = eventInstance.start();
}
// The CallBackFunction function is the same as the code above.
}
Mounted the script to an empty object and set up the events that were just generated in FMOD Studio.
After running it, Unity doesn’t have any output messages and the audio doesn’t play.
I think the original answer may not accomplish what I need.
Also, due to my poor English, I used translation software to translate most of the English documents, so I may have misunderstood, so I am very sorry if I am wrong about the original answer.