Is It Possible To Set A Javascript Callback Method To A C# Method Using Marshalling?

Started by
-1 comments, last by sk84z 7 years, 8 months ago
I am coding in Unity using C# and developing for WebGL. I need to use browser JavaScript so have written a .jslib from which I call methods in C# using Marshalling.
Is it possible for me to set a JavaScript callback to a C# Action, Func, or Event Handler like so:
// Javascript in .jslib called from C# using static extern
// action being the C# method that I want to call
// On callback I get -> TypeError: action is not a function
SetOnOpen: function (action) {
$jsobj.onopen = function () {
action();
};
},
I've tried each of these, but don't appear to be doing it properly. I know that C++ can call C# functions, but JavaScript must communicate differently somehow.
Attempts:
[DllImport("__Internal")]
private static extern void SetOnOpen(ref OnOpenHandler handler);
[DllImport("__Internal")]
private static extern void SetOnOpen(ref Action action);
[DllImport("__Internal")]
private static extern void SetOnOpen(IntPtr handler);
No matter what I try, I get a TypeError stating that the object that I'm passing over to the JavaScript isn't a function. I've tried:
Using Action and Event Delegates
Using ref, IntPtr, and neither
Anyone know if this is even possible? Like I said, I successfully called a C# method in a C++ .dll before, so figured it would be possible to do so from a .jslib.
[size=2]

This topic is closed to new replies.

Advertisement