Search Unity

Passing Uint8Array from javascript to Unity C#

Discussion in 'Scripting' started by akshay_shah, Mar 15, 2018.

  1. akshay_shah

    akshay_shah

    Joined:
    May 19, 2017
    Posts:
    16
    I am creating a WebGL build.
    In my javascript file, I have a variable with data of type Uint8Array.
    I want to pass that value to Unity C# as byte[] or float[].
    I tried a lot but could not find a way.
    Is it possible to do what I am trying to achieve?

    Any help would be appreciated.
    Thanks!
     
  2. unitynoob24

    unitynoob24

    Joined:
    Dec 27, 2014
    Posts:
    398
    I believe communicating across C# and .JS can be quite tricky. I would look into PlayerPrefsX - which acts just like Player Prefs but allows you to store arrays as well.

    http://wiki.unity3d.com/index.php/ArrayPrefs2

    So basically you can assign your values to a new PlayerPrefsX array in .JS then pull them down in any C# script from PlayerPrefsX and assigning them to an empty array in C#.

    Hope this helps!
     
  3. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,537
    Well communicating between your webgl game and the browser doesn't require PlayerPrefs. You can directly communicate:
    https://docs.unity3d.com/Manual/webgl-interactingwithbrowserscripting.html

    As for the Uint8Array, I don't believe you can directly translate that across. Instead you have to pass the ArrayBuffer instead.

    This thread seems to have some people chatting about doing this specifically:
    https://forum.unity.com/threads/try...etween-c-and-webgl-browser-javascript.394861/

    If you have a hard time getting it done (sorry, I never really set this up, so I can't directly help you with it)... you could convert it to a B64 string and send that. But that would certainly increase memory usage by a lot since you have to convert to and from for every message. And I don't know how big of data you're sending...