_malloc is not defined in Unity 2017 webGL Player

Hi all,

I’m working on a webGL project and I have a function where I send a JSON array from an external javascript to the unity player. I followed the instructions in the Unity 5.6 manual using lengthBytesUTF8 and _malloc like so:

//my_external_javascript.js

var my_global_array = ["my", "array", "data"];

function UnityGetMarkerArray()
{
    var returnStr = JSON.stringify(my_global_array);
    var buffer = _malloc(lengthBytesUTF8(returnStr) + 1);
    writeStringToMemory(returnStr, buffer);
    return buffer;
}

This worked great.

Then I upgraded to Unity 2017.

Now even though the manual entry is largely unchanged, when I run the same code I now get an error saying _malloc is not defined:

100002-chrome-2017-08-16-11-51-08.png

I can see that now _malloc and lengthBytesUTF8 are no longer defined anywhere, and I can’t seem to get them back.

Is this a bug in the newest version of 2017? Could anyone perhaps provide me with replacement functions so I can keep working? Thanks!!

After hunting around the minified built code I found that there is a temporary workaround, you need to prefix all the previously global Unity functions with “unityPlayerInstance.Module.”, ie:

function UnityGetMarkerArray() {

    var returnStr = JSON.stringify(my_global_array);
    return(UnityAllocateMemoryAndReturnBuffer(returnStr));
}

function UnityAllocateMemoryAndReturnBuffer(str)
{
    if(typeof _malloc != "undefined" && typeof lengthBytesUTF8 != "undefined" && typeof writeStringToMemory != "undefined")
    {
        // The way the manual advises we do it
        var buffer = _malloc(lengthBytesUTF8(str) + 1);
        writeStringToMemory(str, buffer);
        return buffer;
    }
    else
    {
        // Workaround for Unity 2017
        var buffer = unityPlayerInstance.Module._malloc(unityPlayerInstance.Module.lengthBytesUTF8(str) + 1);
        unityPlayerInstance.Module.writeStringToMemory(str, buffer);
        return buffer;
    }
}