Hi,
This can certainly be done using a Programmer Instrument. We have a scripting example: Unity Integration | Scripting Examples - Programmer Sound which outlines using Audio Tables with a programmer sound. However, since you already have a sound, rather than using a table or a file you’d just pass the sound to the programmer instrument.
Here is the updated switch
statement:
Switch
{
case FMOD.Studio.EVENT_CALLBACK_TYPE.CREATE_PROGRAMMER_SOUND:
{
// We can remove all this since we already have the sound
//FMOD.MODE soundMode = FMOD.MODE.LOOP_NORMAL | FMOD.MODE.CREATECOMPRESSEDSAMPLE | FMOD.MODE.NONBLOCKING;
// We need to keep this
var parameter = (FMOD.Studio.PROGRAMMER_SOUND_PROPERTIES)Marshal.PtrToStructure(parameterPtr, typeof(FMOD.Studio.PROGRAMMER_SOUND_PROPERTIES));
// Remove this
// if (key.Contains("."))
// {
// FMOD.Sound dialogueSound;
// var soundResult = FMODUnity.RuntimeManager.CoreSystem.createSound(Application.streamingAssetsPath + "/" + key, soundMode, out dialogueSound);
// if (soundResult == FMOD.RESULT.OK)
// {
// parameter.sound = dialogueSound.handle;
// parameter.subsoundIndex = -1;
// Marshal.StructureToPtr(parameter, parameterPtr, false);
// }
// }
// else
// {
// FMOD.Studio.SOUND_INFO dialogueSoundInfo;
// var keyResult = FMODUnity.RuntimeManager.StudioSystem.getSoundInfo(key, out dialogueSoundInfo);
// if (keyResult != FMOD.RESULT.OK)
// {
// break;
// }
// FMOD.Sound dialogueSound;
// var soundResult = FMODUnity.RuntimeManager.CoreSystem.createSound(dialogueSoundInfo.name_or_data, soundMode | dialogueSoundInfo.mode, ref dialogueSoundInfo.exinfo, out dialogueSound);
// if (soundResult == FMOD.RESULT.OK)
// {
// parameter.sound = dialogueSound.handle;
// parameter.subsoundIndex = dialogueSoundInfo.subsoundindex;
// Marshal.StructureToPtr(parameter, parameterPtr, false);
// }
// }
// We now need to pass the sound to the programmer instrument
parameter.sound = dialogueSound.handle;
parameter.subsoundIndex = dialogueSoundInfo.subsoundindex;
Marshal.StructureToPtr(parameter, parameterPtr, false);
// Thats it!
break;
}
case FMOD.Studio.EVENT_CALLBACK_TYPE.DESTROY_PROGRAMMER_SOUND:
{
var parameter = (FMOD.Studio.PROGRAMMER_SOUND_PROPERTIES)Marshal.PtrToStructure(parameterPtr, typeof(FMOD.Studio.PROGRAMMER_SOUND_PROPERTIES));
var sound = new FMOD.Sound(parameter.sound);
sound.release();
break;
}
case FMOD.Studio.EVENT_CALLBACK_TYPE.DESTROYED:
{
// Now the event has been destroyed, unpin the string memory so it can be garbage collected
stringHandle.Free();
break;
}
}
Remember to pass in the sound as the event instances User Data . To do this I create a simple class to hold the sound for me:
public class UserDataClass
{
public FMOD.Sound sound;
}
But you may wish to do this another way.
Hope this helps!