Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Calling a Javascript function that returns a string from C# in Project Tiny

Discussion in 'Project Tiny' started by AlexisSphero, Nov 24, 2020.

  1. AlexisSphero

    AlexisSphero

    Joined:
    Oct 29, 2020
    Posts:
    12
    I'm trying to call a Javascript function that returns a string from C#. I can do this fine with floats but when I try and get a string back, the value is always null.

    I have added a Javascript file to a ~js folder that looks like this:

    Code (JavaScript):
    1. mergeInto(LibraryManager.library, {
    2.  
    3.   GetFloat: function() {
    4.    return -0.1;
    5.   },
    6.  
    7.   GetString: function() {
    8.     return "This is a string";
    9.   }
    10. });
    In my C# codee I have this:


    Code (CSharp):
    1. [DllImport("__Internal")]
    2.         private static extern float GetFloat();
    3.  
    4.  
    5.         [DllImport("__Internal")]
    6.         private static extern string GetString();
    7.  
    8.         protected override void OnUpdate()
    9.         {
    10.             float javascriptFloat = GetFloat();
    11.             string javascriptString = GetString();
    12.  
    13.             UnityEngine.Debug.Log(string.Format("{0}", javascriptFloat));
    14.             UnityEngine.Debug.Log(javascriptString);
    15.         }
    I recive the float back just fine but the stirng comes back as null, or more specifically:
    null (null message, maybe a format which is unsupported?) 


    When I change javascriptString to be a var and log it's type I get:
    Non-Trivially-Stringable OBJECT logged (Not supported in DOTS C#)


    I also tried converting my Javascript string into a char array but I get the same issue. Is there any way to pass a string like this? My ultimate goal is actually to send some json from the javascript which I assumed I would need to do as a string.
     
  2. TwoorbJan

    TwoorbJan

    Joined:
    Mar 6, 2013
    Posts:
    38
    Azebakh likes this.
  3. AlexisSphero

    AlexisSphero

    Joined:
    Oct 29, 2020
    Posts:
    12