Hello, I am trying to write a callback to pass values from c to c# (in iOS). I followed the guide from [here][1]
public delegate void meshDataUpdated(MeshData data);
public static event meshDataUpdated meshDataUpdatedEvent;
[DllImport("__Internal")]
private static extern void onMeshUpdated(MeshUpdateCallback callback);
[MonoPInvokeCallback(typeof(MeshUpdateCallback))]
static void _mesh_updated(MeshData dataPtr)
{
if (MeshUpdateEvent != null)
{
}
}
public MeshData {
// marshal C objects to c# objects in the constructor here using the Ptr from C
}
// C code
typedef struct
{
float *vertices;
int *triangleIndices;
} MeshData;
typedef void (*Mesh_Data_Callback)(MeshData* meshData);
extern "C" {
void onMeshUpdated(Mesh_Data_Callback callback){
// contruct mesh data ptr
MeshData* data;
callback(data);
}
}
// Usage
void Start(){
meshDataUpdatedEvent += updateMesh;
}
public void updateMesh(MeshData data){
// do something with data
}
Does this flow work? Or I would like to know if there are any mistakes beforehand?
How can I call the C function whenever my mesh is updated? How do I hold the reference to the callback function in C?
[1]: https://answers.unity.com/questions/1229036/callbacks-from-c-to-c-are-not-working-in-540f3.html
↧