Just to document my ‘progress’ (if you can call it that), and because I can’t find anything else on this in the forums…
the snippet of code above was born out of necessity, as I had to import functions from a bundle, and the function declarations had void pointers in them. Reading from this thread:
http://answers.unity3d.com/questions/2053/tell-me-how-to-use-unsafe-with-c
brought me (actually, Martin Roth working with me) to use IntPtr to try to wrap the dll function. We also read this thread from a little while back:
http://forum.unity3d.com/viewtopic.php?t=6153
But, our implementation so far seems to fail with dll not found again. Here’s the complete c# script calling the bundle (which we’ve checked contains all the symbols it should):
using UnityEngine;
using System;
using System.Runtime.InteropServices;
public class ZGUnity : MonoBehaviour
{
// This tells unity to look up the function FooPluginFunction inside the plugin named "PluginName"
[DllImport ("ZGUnity")]
private static extern IntPtr zg_new_context(int numInputChannels, int numOutputChannels, int blockSize, float sampleRate,
long function, long userData); // void (*function)(int function, void *userData, void *ptr)
[DllImport ("ZGUnity")]
private static extern IntPtr zg_new_graph(IntPtr context, string directory, string patch);
[DllImport ("ZGUnity")]
private static extern void zg_attach_graph(IntPtr context, IntPtr graph);
[DllImport ("ZGUnity")]
private static extern void zg_process(IntPtr context, float[] inputBuffer, float[] outputBuffer);
[DllImport ("ZGUnity")]
private static extern void zg_delete_context(IntPtr context);
void Awake ()
{
int numInputChannels = 2;
int numOutputChannels = 2;
int blockSize = 256;
float sampleRate = 44100.0f;
float[] inputBuffer = new float[blockSize * numInputChannels];
float[] outputBuffer = new float[blockSize * numOutputChannels];
IntPtr context = zg_new_context(numInputChannels, numOutputChannels, blockSize, sampleRate, 0, 0);
IntPtr graph = zg_new_graph(context, "/Users/dpc/GamesDevelopment/zengardengit/zengardenunityNormal/Assets/Plugins", "test.pd");
Debug.Log("Patch:" + graph);
zg_attach_graph(context, graph);
while (true) {
zg_process(context, inputBuffer, outputBuffer);
}
zg_delete_context(context);
}
}
What are we doing wrong? Am I not correct in thinking that we can’t use ‘unsafe’ as there’s no way to invoke the unsafe compiler flag in Unity, and in any case it would be best to marshal?