- Home /
_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:
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!!
Answer by bbeckford · Aug 16, 2017 at 01:58 PM
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;
}
}
Your answer
Follow this Question
Related Questions
How to get the game data to the Web (webgl or webplayer) 1 Answer
GL build won't run on Chrome 1 Answer
Unity3d Webgl build and angularjs 1 Answer
Running one instance of webgl build at a time in a html page when loading webgl builds sequencially. 0 Answers
Install unity web player from flash 2 Answers