Browser to Unity WebGL communication

How do we pass messages into Unity WebGL apps from browser javascript? I know the syntax for the UNITY_WEB plugin is

u.getUnity().SendMessage("MyGameObject","MyFunction", "SomeValue");

What is the syntax for sending a message to UNITY_WEBGL from browser javascript?

I’m using Unity 5b12. In UnityConfig.js there is a SendMessage function. The WebGL build automatically includes this in the build.

So just change:

u.getUnity().SendMessage("MyGameObject","MyFunction", "SomeValue");

to:

SendMessage("MyGameObject","MyFunction", "SomeValue");

Hint: You need to wait for WebGL/Your game object to be loaded before calling it.

See http://forum.unity3d.com/threads/how-to-ensure-that-webgl-is-loaded-and-can-call-sendmessage.394512/

FYI, after upgrading to Unity 5.6, SendMessage stopped working for me; instead of

SendMessage(...)

I had to use

gameInstance.SendMessage(...)

and even that wasn’t available immediately on page load… I had to wrap it in a setTimeout loop.

The standard WebGL template file was changed but I see no mention of this in any documentation or release notes.

I was able to call a function from my a gameobject in my scene from my javascript code.

My scene was loading very slow, even when I only have two gameobjects inside it.
And because I called my function “SendMessage” before the page finish loading, a pop up with the error “SendMessage is not defined” appears.

-The steps to solve it were:

1-The form to call the funtion is :

function SaySomethingToUnity(_parameter_To_Send)
{

SendMessage(“GameObject_To_Call”, “Function_To_Call”, _parameter_To_Send);

}

You DO NOT need to use "“u.getUnity()” anymore.

Remember that (SendMessage will then call the function Function_To_Call() on the game object named GameObject_To_Call, passing a piece of string, int or float, data as an argument:)

2-Disable thee Exceptions in Unity(PlayerSettings->Publishing Settings->Enable Exceptions: None). This form, my scene was loading faster.

3-When everything was loaded. The “SendMessage” error did not appears anymore (the file UnityConfig.js that Unity generated when building does not have a definition for the function SendMessage, so do not look for it).

I found the function defined of SendMessage in {YourGameProjectName}.js
It’s seems like this:
function SendMessage(gameObject,func,param
line 1,Col 536

I was having the error: “SendMessage not defined” when I was integrating unity3d using iframe. I switched to using webgl-template (Unity - Manual: WebGL templates) and added my SendMessage call there, that worked fine.

Use:

gameInstance.SendMessage("MyGameObject","MyFunction", "SomeValue");