I’m trying to build a toy synth in unity using a juce plugin. Generally I’ve got the plugin running so I can call my juce library from my game objects with no problem.
However I’m trying to call some custom functions from my CustomAudioProcessor.cs from my unity scripts. This works if I set up my extern interfaces on the generated juce_UnityWrapper.cpp but in that class the plugin instance is set of type AudioProcessor. Do I need to change that to my CustomAudioProcessor so I can access my public functions? I’m not sure if that’s the intended way of accessing them.
Apologies if my question was a bit confusing! Let me try to explain what I’m trying to accomplish.
Basically I’ve got the Unity game running my JUCE synth plugin from the unity audio mixer and I’m able to control my synth parameters at runtime from my gameobject scripts which is great!
However I would also like to load a sample from unity into my juce plugin. Usually I would do this through the plugin editor if this was a VST and pass that sample into my audio processor. I have my plugin editor disabled for this unity plugin so I need to find a way to directly upload that sample into my plugin. Anyone had any experience doing something similar?
I can see the generated juce_Unity_Wrapper.cpp already has implementations of the extern interface functions (like process callback etc which calls the processBlock on the audio processor) but if I define let’s say a new extern load_sample(float* data), on the implementation side (juce_Unity_Wrapper.cpp) I see no way of accessing my audio processor subclass from the pluginInstance (pluginInstance->loadSample(data)) because the pluginInstance is defined as a AudioProcessor object.
Hey, unfortunately I haven’t managed to do it yet with the JUCE unity plugin export. Instead I temporarily mirrored some of the DSP functionality I needed in Unity and doing the audio processing with OnAudioFilterRead which works fine now but it’s not a good practice as it can cause audio drops due to the garbage collection. I’m planning to get back to it soon though, it would be great if someone else has done something similar and get some hints on how to do so. Would be awesome to be able to use all the JUCE goodies in Unity!
check out this little tutorial I wrote a while back:
This is definitely not the ideal way to do things, since it involves doctoring some juce files, but it certainly is a start and it works This will expose juce functions to unity and let you call them from within your unity code
Just as a side note as I notices this while scanning over the topic: Changes getting overwritten are no real issue if you are using git.
Having your own fork of JUCE and modifying it makes it super easy to pull recent JUCE updates into your fork without breaking your customisations. Furthermore, you are more likely to notice if your changes clash with some changes in the upstream JUCE repository.
We do this quite successfully for some longer time now and I have seen other companies here with their own forks as well…
Hey @benediktadams! Thanks a lot for sharing your workaround.
I was trying to patch it using your steps but I am facing an issue where on pluginInstance.reset (createPluginFilterOfType (MyAudioProcessor::wrapperType_Unity)); , its throwing an error that it can accept only juce::AudioProcessor type. I have subclassed MyAudioProcessor correctly and I can’t seem to figure out how to get around it? Would appreciate your help!
Sorry to the above posters, i never saw these replies… Hope you got it figured out. Just reviving this, because it came to my attention, that the above posted gist link has expired (github handle changed, and i can’t edit the comment). Here’s a new link to the same content… i didn’t implement the fix mentioned above by @clffrd because I didn’t test it, and haven’t touched any of this since I made the gist in 2020… anything in there is “use at your own risk” anyways