Jump to content
  • Advertisement
Sign in to follow this  
sk84z

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

This topic is 864 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.
Edited by sk84z

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!