Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice
  2. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  3. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

[Solved] Is there a way to call JS functions from C#

Discussion in 'Project Tiny' started by egonaraujo, Oct 10, 2019.

  1. egonaraujo

    egonaraujo

    Joined:
    Jan 21, 2015
    Posts:
    18
    Lots of tutorials and old videos explain how to do this from Typescript..
    Do we have the same ability with C#?

    I've seen lost in the forum a "PInvoke" reference, but I only found this library being used with .dll or .so libraries, and you have to specify which file you are getting the function from. If this is the way, can you maybe post an example? thanks!


    Edit for easier solution:

     
    Last edited: Oct 17, 2019
  2. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,683
  3. egonaraujo

    egonaraujo

    Joined:
    Jan 21, 2015
    Posts:
    18
    I just tested and, unfortunately, that did not work :/
    Tiny do not find the function I created in the .jslib file


    Yet, I do have a kind of work around solution, but working:
    Call
    Code (CSharp):
    1. UnityEngine.Debug.Log("callFunctionName");
    2.  
    On Code, then go to your build folder (NameProject-platform-version) and find "NameProject.js"
    [ platform being wasm/asmjs; version being release/develop/debug]
    Inside "NameProject.js" find
    Code (JavaScript):
    1. function out(text) {
    2.   console.log(text);
    3. }
    And change to:
    Code (JavaScript):
    1. function out(text) {
    2.   if(text == "callFunctionName"){
    3.     FunctionName();
    4.     return;
    5.   }
    6.   console.log(text);
    7. }
    I am not proud of this specific piece of junk, but it worked and was a good solution for me.
     
    SomeGuy22 likes this.
  4. egonaraujo

    egonaraujo

    Joined:
    Jan 21, 2015
    Posts:
    18
  5. yossi_horowitz_artie

    yossi_horowitz_artie

    Joined:
    Jan 30, 2019
    Posts:
    87
    I think it's actually quite brilliant.
     
    egonaraujo likes this.