Search Unity

  1. Click here to see what's on sale for the "Best of Super Sale" on the Asset Store
    Dismiss Notice
  2. We are looking for feedback on the naming of a new user research platform that we are working on.
    Dismiss Notice
  3. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

How to call C# methods from JavaScript?

Discussion in 'Project Tiny' started by SINePrime, Mar 14, 2020.

  1. SINePrime

    SINePrime

    Joined:
    Jan 24, 2019
    Posts:
    43
    Sorry if this is considered a duplicate of this thread, I didn't want to hijack/ necro that thread.

    I'm trying to invoke C# methods from JavaScript, and having trouble accomplishing this.

    Are we meant to use `ccall` and `cwrap`, as outlined here? When I tried to add `EXTRA_EXPORTED_RUNTIME_METHODS['cwrap']` to the arguments of my Emscripten build settings, the build tools threw an error:
    >error: Emscripten LinkerSettings contains setting for which a dedicated parameter exists: EXTRA_EXPORTED_RUNTIME_METHODS (System.InvalidOperationException)

    Is there somewhere I am meant to configure this outside the Emscripten settings?

    Another method I see is using `dynCall` methods, but documentation on these methods is scarce, and I've had no luck with all previous examples.

    I'm at a bit of a loss on how to proceed. Any ideas or documentation?
     
  2. SINePrime

    SINePrime

    Joined:
    Jan 24, 2019
    Posts:
    43
    With a bit of digging, I figured out there were multiple dynCall variants; the only variants that worked for me was the dynCall_* variant:
    Code (CSharp):
    1. dynCall_v(callback); // this works
    2. //dynCall('v', callback); // ReferenceError: dynCall is not defined
    3. //Module['dynCall_v'](callback); // TypeError: Module.dynCall_v is not a function
    I'll be sharing an example Project Tiny repo with bidirectional C# <-> JavaScript shortly.
     
    facundo_unity961 likes this.
  3. SINePrime

    SINePrime

    Joined:
    Jan 24, 2019
    Posts:
    43
    I made an example project with interoperability between C# and JavaScript here. The JavaScript to C# examples require you to pass the function you want to call as a delegate.

    If anyone has info on how to call an arbitrary static C# function from JavaScript in Unity, please let me know!
     
    fantasicle0 and facundo_unity961 like this.
  4. Savign

    Savign

    Joined:
    Nov 15, 2013
    Posts:
    3
    Hey Sine, thanks for putting up the example - I'm at a bit of a loss to how to call the callback methods from the javascript console though.

    If you could offer any advice that'd be greatly appreciated!
     
unityunity