添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

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");
SendMessage("MyGameObject","MyFunction", "SomeValue");
              

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