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. Let us know your feedback about the Global Illumination changes in the 2023.2 beta.
    Dismiss Notice
  3. Dismiss Notice

Passing variables to jslib : WebGL [B14]

Discussion in 'Unity 5 Pre-order Beta' started by d12frosted, Dec 3, 2014.

  1. d12frosted

    d12frosted

    Joined:
    Feb 3, 2014
    Posts:
    43
    Hi everyone!

    I wanted to use new cool jslib stuff. And created file in `Assets/Plugins/WebGL/MyPlugin.jslib` with content:

    Code (JavaScript):
    1. var MyPlugin = {
    2.   LogId: function(id) {
    3.     console.log("app id is: " + id);
    4.   }
    5. }
    6.  
    7. mergeInto(LibraryManager.library, MyPlugin);

    Then I went to class that should call this `LogId` function and did this:

    Code (csharp):
    1. #if UNITY_WEBGL
    2.     [DllImport("__Internal")]
    3.     private static extern void LogId(string id);
    4. #endif
    5.  
    6. ...
    7.  
    8. void SomeMethod(string id) {
    9.    // stuff to do before log
    10.    LogId(id);
    11.    // stuff to do after log
    12. }

    So I was expecting to see, for example, '918090405799497', but instead I got some junk: '34XXXX64'. This value is always the same for the same id. So I thought that `id` variable is changing somewhere in code (but that's really weird, it should not), so I tried to call LogId like

    Code (csharp):
    1. LogId(string.Copy(id));
    And anyway I got that junk value.
    Then I tried to use old fashioned way to call functions in browser - by Application.ExternalCall.

    Code (csharp):
    1. Application.ExternalCall("LogId", id);
    So I made WebGL build with this change and added script to generated index.html:

    Code (JavaScript):
    1. function FBWebGLInit(id) {
    2.   console.log("app id is: " + id);
    3. }
    4.  

    And after I run index.html I saw in console 'app id is: 918090405799497'. And that's what I was expecting.

    Have anyone faced such problem?
     
    Last edited: Dec 3, 2014
  2. jonas-echterhoff

    jonas-echterhoff

    Unity Technologies

    Joined:
    Aug 18, 2005
    Posts:
    1,666
    It's not quite as easy: you need to handle string marshalling between C# and JS. Passing without marshalling will only work for simple, numeric types.

    For an example which uses string marshalling in a jslib, check:
    http://files.unity3d.com/jonas/WebSockets.unitypackage

    The above works right now.

    You can expect this to change a little, still: We are adding support for string marshalling to IL2Cpp, so you will be able to simply pass a string in on the C# side in the future (like in your current code). You will still need to load the string from memory in JavaScript, however, like in the example from my link.
     
    made-on-jupiter likes this.
  3. d12frosted

    d12frosted

    Joined:
    Feb 3, 2014
    Posts:
    43
    @Jonas
    Thank you very much for your reply. I will look into the package you provided.
    And I hope that in future sending strings from C# to JS will be as easy as calling a function :)