Search Unity

Call C# from Javascript in Tiny

Discussion in 'Project Tiny' started by Zadirion, Dec 28, 2019.

  1. Zadirion

    Zadirion

    Joined:
    Mar 16, 2015
    Posts:
    88
    So I have a method defined in a js~ as I've seen instructed on the forums, in order to call some javascript from my C#.
    Now, that method does an XmlHttpRequest and that request finishes at a later time, and calls a callback.

    So my question is, how can I call back into C# with the result of that request? How do you, in general, call C# from javascript in Tiny?

    Thanks
     
    Last edited: Dec 28, 2019
  2. Zadirion

    Zadirion

    Joined:
    Mar 16, 2015
    Posts:
    88
    Discovered dynCall_* set of methods in emscripten that can be used to call back into c# from what I understand, but when I use that to call my static c# method, it gives me.
    Code (CSharp):
    1. No reverse pinvoke wrapper exists for method: 'System.Void LMLib.Gameplay.DragAndDropSystem::CallbackTest(System.Int32)' because it does not have the [MonoPInvokeCallback] attribute.
    I can't add MonoPInvokeCallback attribute on my method because that's part of UnityEngine.Core.

    So, how do I call back into C# from a wasm build in Tiny?
     
  3. elliotc-unity

    elliotc-unity

    Unity Technologies

    Joined:
    Nov 5, 2015
    Posts:
    33
    Try creating MonoPInvokeCallbackAttribute just from scratch in your code and adding it. IIRC the code that looks for it just looks for the name of the attribute anyway, and not where it comes from.
     
    Maras and Zadirion like this.
  4. kristoof

    kristoof

    Joined:
    Aug 26, 2013
    Posts:
    19

    How did you get it to build? I made a js~ folderinside my Assets, put my js file:
    Code (CSharp):
    1. mergeInto(LibraryManager.library, {
    2.     MultiplyNumbers: function (p1, p2){
    3.         alert("CALLED");
    4.         return p1*p2;
    5.     }
    6.    
    7.     });
    Then in my system i have
    Code (CSharp):
    1. using Unity.Entities;
    2. using UnityEngine;
    3. using System.Runtime.InteropServices;
    4. public class JavaCallTest : ComponentSystem
    5. {
    6.     [DllImport("__Internal")]
    7.     private static extern int MultiplyNumbers(int x, int y);
    8.     protected override void OnStartRunning()
    9.     {
    10.         Debug.Log(MultiplyNumbers(4, 8)); // returns 32
    11.     }
    12.     protected override void OnUpdate()
    13.     {
    14.     }
    15. }
    16.  
    But every time i try to build with the [DllImport("__Internal")] it fails.
    (Build Wasm-Tiny3D failed in BuildStepRunBee after 27.03s.
    warning: unexpected return type i8* in call to 'SetTempSliceHandle', should be void)

    Also about calling from js to unity:
    Does this mean that SendMessage is no longer an option?
     
  5. Zadirion

    Zadirion

    Joined:
    Mar 16, 2015
    Posts:
    88
    I think it needs to be in the same folder with your main asmdef, if I remember correctly.
     
    tonialatalo likes this.
  6. tonialatalo

    tonialatalo

    Joined:
    Apr 23, 2015
    Posts:
    27
    I also got it working yesterday by putting in the same dir as where the asmdef is. Tested with Tiny3D and a little test system that have put there, for getting data over http to Tiny in a web build. Thanks for the helps!
     
    Zadirion likes this.
  7. ckucukha

    ckucukha

    Joined:
    Sep 18, 2018
    Posts:
    3
    So is the main asmdef Tiny3D.asmdef file? because I placed my jslib file there, sadly it didn't compile. Anyone has any ideas?
     
  8. Maras

    Maras

    Joined:
    Dec 11, 2012
    Posts:
    88
    Needs to be .js , not .jslib like in regular Unity
     
    tonialatalo likes this.
  9. ckucukha

    ckucukha

    Joined:
    Sep 18, 2018
    Posts:
    3
    Thanks! Got it to work that way.
     
unityunity