This tutorial is based on a great question Sound Designer and Composer Pablo Sorribes asked in FMOD’s forums: How to change the Max Instances property of FMOD Events for different platforms?
Currently the FMOD API does not allow to adjust the limits of Event Instances during runtime. But as a workaround we can use the FMOD Studio Scripting API to write a script to accomplish the task. In order to solve this problem we will:
- Create a .json file with a list of FMOD Events and the corresponding Max Instances value.
- Manually edit the Max Instances value for each Event.
- Build the banks for a specific platform with the Max Instances values defined in the .json file and revert the values back after the build process.
Creating a .json file containing FMOD Events and the corresponding Max Instances value
Let’s start with creating the .json file. You will need to create the scripts folder within the root directory of your FMOD Studio project to use the Scripting API. After that create a new .js file inside the scripts directory and give it an unique name. Type this code into the JavaScript file:
studio.menu.addMenuItem({
name: "Unity\\ExportMaxInstancesJson",
isEnabled: function() {
var events = studio.window.browserSelection();
return events;
},
execute: function() {
var events = studio.window.browserSelection();
var eventsObj = [];
var event = {};
for (x = 0; x < events.length; x++) {
var newEvent = events[x].getPath();
var newVoices = events[x].automatableProperties.maxVoices
eventsObj.push({
'path': newEvent,
'value': newVoices
})
}
event.eventsObj = event;
var str = JSON.stringify(eventsObj, null, 2);
var fileName = studio.system.getText("File Name", "NewFile");
var projectPath = studio.project.filePath;
projectPath = projectPath.substr(0, projectPath.lastIndexOf("/"));
var filePath = projectPath + "/" + fileName + ".json";
var file = studio.system.getFile(filePath);
file.open(studio.system.openMode.WriteOnly);
file.writeText(str);
file.close();
}});
In FMOD Studio, click on Scripts->Reload. A new menu entry will show up in the scripts menu. Select multiple FMOD Events from the Event Browser and click on the menu entry Scripts->Unity->ExportMaxInstancesJson. Enter a new file name and click on OK. A new .json file will be created in the root directory of your FMOD project. It will look like this:
[
{
"path": "event:/2_Playing FMOD Events/2D Event",
"value": 22
},
{
"path": "event:/2_Playing FMOD Events/3D Event",
"value": 4
},
{
"path": "event:/2_Playing FMOD Events/Loop",
"value": 1
}
]
Path is obviously the path to the FMOD Event and value represents the Max Instances value for that Event. Adjust the values here in this file to your custom Max Instances values, so we can read back the file in FMOD Studio and build the project.
Creating a script to read back the .json file and build the FMOD project
This new script will read the .json file, modify the Max Instances values you specified in the .json file, create the banks for your desired platform and revert the values back to the original state:
studio.menu.addMenuItem({
name: "Unity\\CustomMaxVoicesJSON",
isEnabled: function() {
return 1;
},
execute: function() {
var oldVoiceNumber = [];
var platform = studio.system.getText("Enter the Platform", "Switch");
var fileName = studio.system.getText("File Name", "NewFile");
var projectPath = studio.project.filePath;
projectPath = projectPath.substr(0, projectPath.lastIndexOf("/"));
var filePath = projectPath + "/" + fileName + ".json";
var file = studio.system.getFile(filePath);
file.open(studio.system.openMode.ReadOnly);
var fileSize = file.size();
var events = JSON.parse(file.readText(fileSize));
for (x = 0; x < events.length; x++) {
var event = studio.project.lookup(events[x].path);
var value = events[x].value;
oldVoiceNumber[x] = event.automatableProperties.maxVoices;
event.automatableProperties.maxVoices = value;
}
studio.project.build({
platforms: platform
});
for (x = 0; x < events.length; x++) {
var event = studio.project.lookup(events[x].path);
event.automatableProperties.maxVoices = oldVoiceNumber[x];
}
}});
Simply enter the platform (Switch for example) and the file name of the previously created .json file in the popup dialog when launching the script. FMOD Studio will change the Max Instances values to the values defined in the .json file, it will build the banks for the specified platform and revert the values back to the original state.