Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Join us on Thursday, June 8, for a Q&A with Unity's Content Pipeline group here on the forum, and on the Unity Discord, and discuss topics around Content Build, Import Workflows, Asset Database, and Addressables!
    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