c++ - two way communication between unmanaged code and unity3d code -
i have 2 apps. 1 of them written in visual c++ , other unity app, both running on windows. in scenario, want call unity function , draw object whenever user presses on button in c++ app. far, have tried load unity executable same address space c++ app calling mono_domain_assembly_open
. however, returns null , not able call mono_jit_exec
run unity app. possible maintain 2 way communication between 2 applications using mono? in advance!
here old example have, based off of this post. want pass c# delegate c++ function pointer. can store function pointer use button, or whatever else you'd like.
c++ dll:
typedef int ( __stdcall *unitycallback )( int ); static unitycallback gcallback; extern "c" __declspec( dllexport ) inline int callbackexample( unitycallback unityfunctionpointer, int n ) { gcallback = unityfunctionpointer; if( gcallback ) { return gcallback( n ); } return 0; }
c# caller:
using unityengine; using system; using system.runtime.interopservices; public class callback : monobehaviour { public delegate int callbackdelegate( int n ); [dllimport ("unityplugincallback")] private static extern int callbackexample(callbackdelegate fp, int n); void awake() { int result = callbackexample(new callbackdelegate(this.callbacktest), 42); debug.log("result callback, should 43: " + result); } int callbacktest( int n ) { debug.log("received: " + n + " c++ dll"); return n+1; } }
in example, c++ dll calls c# callback value of 42. c#'s callback increments value 1 , returns c++ in turn returns c# @ callbackexample
call site.
unity doesn't when try access engine outside of main thread i'm not sure happens if c++ dll has asynchronous calls c#. in example calls starts in main unity thread there aren't issues. suggest not allow unity specific functionality in c# callback, instead use callback set boolean(or other mechanism) used update
implement whatever want unity engine.
Comments
Post a Comment