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

How do you call JavaScript functions from Tiny C# Systems?

Discussion in 'Project Tiny' started by Riverdragon, Jun 19, 2019.

  1. Riverdragon

    Riverdragon

    Joined:
    Jun 22, 2018
    Posts:
    6
    I am familiar with how to call Javascript using Typescript Tiny, but the new C# version appears to use a different method. Any help would be much appreciated.
     
  2. Riverdragon

    Riverdragon

    Joined:
    Jun 22, 2018
    Posts:
    6
    Ugh, realized there is a sample with browser interop. You have to include a ~js folder and a ~prejs folder that Unity can not see, reviewing BrowserInterop solved every question I had.
     
  3. Riverdragon

    Riverdragon

    Joined:
    Jun 22, 2018
    Posts:
    6
    Leaving a mini tutorial as people sent me discussion questions.

    This if for Unity Tiny 0.15.3 and may not work in future ones.

    First, in the project folder of your UnityTiny project, place the folder "js~" The tilde makes it invisible to Unity Editor.

    Inside that make your sample file, I will call mine "MultiplyNumbers.js"


    Code (JavaScript):
    1. mergeInto(LibraryManager.library, {
    2.  
    3. MultiplyNumbers: function (p1, p2){
    4.     return p1*p2;
    5. }
    6.  
    7. });
    Then, you can create a C# system.
    Code (CSharp):
    1. using Unity.Entities;
    2. using UnityEngine;
    3. using System.Runtime.InteropServices;
    4.  
    5. public class JavaCallTest : ComponentSystem
    6. {
    7.     [DllImport("__Internal")]
    8.     private static extern int MultiplyNumbers(int x, int y);
    9.  
    10.     protected override void OnStartRunning()
    11.     {
    12.         Debug.Log(MultiplyNumbers(4, 8)); // returns 32
    13.     }
    14.  
    15.     protected override void OnUpdate()
    16.     {
    17.     }
    18. }
    19.  
    Run this and your debug console should spit out 32.
     
    Novack, kristoof, pcthomart and 5 others like this.
  4. gentlegorilla

    gentlegorilla

    Joined:
    Jun 18, 2019
    Posts:
    19
    Trying to use the sample these times (tiny 0.26.0; unity 2019.4.0f1), the sample results in


    Code (CSharp):
    1. error: undefined symbol: MultiplyNumbers
    2. warning: Link with `-s LLD_REPORT_UNDEFINED` to get more information on undefined symbols
    3. warning: To disable errors for undefined symbols use `-s ERROR_ON_UNDEFINED_SYMBOLS=0`
    4. Error: Aborting compilation due to previous errors
    5.  
    Are there any specific build settings you set with your sample last year, or is this probably due to updated packages.
     
  5. Maras

    Maras

    Joined:
    Dec 11, 2012
    Posts:
    131
    Ted_Wikman likes this.
  6. gentlegorilla

    gentlegorilla

    Joined:
    Jun 18, 2019
    Posts:
    19
    That did it, just copied to js~to the scripts folder, which contains the pre-generated asmdef.
    @Maras will now check your code further to understand the js-to c# callback ;). Many thanks.
     
  7. Rangerz132

    Rangerz132

    Joined:
    Feb 3, 2019
    Posts:
    38
    Hi! I have the same issue here. Is it possible to have some screenshots from your hierarchy? Thank you
     
    vitaly_yavorsky likes this.