3
\$\begingroup\$

I am trying to create a WebGL plugin and I read in the docs here: http://docs.unity3d.com/Manual/webgl-interactingwithbrowserscripting.html

But I'm not sure I understand.

I created a file in Assets/Plugins/WebGL called DomPlugin.jslib:

var DomPlugin = {
    ReadFromDom: function()
    {
        var mapStr = document.getElementById('my-elm').innerText;
        var buffer = _malloc(mapStr.length + 1);
        writeStringToMemory(mapStr, buffer);
        return buffer;
    }
};
mergeInto(LibraryManager.library, DomPlugin);

Within one of my C# scripts, I created a method declaration like so:

[System.Runtime.InteropServices.DllImport("__Internal")]
private static extern string ReadFromDom();

Then I call this external method like so:

private string ReadData()
{
    if (Application.platform != RuntimePlatform.WebGLPlayer)
        return testData.text;

    return ReadFromDom();
}

My WebGL Template page has an element with ID my-elm defined above %UNITY_WEBGL_LOADER_GLUE%

When I deploy I find this error:

Invalid function pointer called with signature 'ii'. Perhaps this is an invalid value (e.g. caused by calling a virtual method on a NULL pointer)? Or calling a function with an incorrect type, which will fail? (it is worth building your source files with -Werror (warnings are errors), as warnings can indicate undefined behavior which can cause this)

\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

Fixed by changing my C# script:

private delegate string ReadData();
private ReadData dataReader;

#if UNITY_WEBGL && !UNITY_EDITOR

[System.Runtime.InteropServices.DllImport("__Internal")]
private static extern string ReadFromDom();

#else

public TextAsset testData;
private string ReadTestData()
{
    return testData.text;
}

#endif

void Awake()
{
#if UNITY_WEBGL && !UNITY_EDITOR
    dataReader = ReadFromDom;
#else
    dataReader = ReadTestData;
#endif
}
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .