Legacy Documentation: Version 4.6
Language: English
Customizing the Unity Web Player's Behavior
Using web player templates

Unity Web Player and browser communication

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Sumbission failed

For some reason your suggested change could not be submitted. Please try again in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

The HTML page that contains Unity Web Player content can communicate with that content and vice versa. Basically there are two communication directions:

  • The web page calls functions inside the Unity web player content.
  • The Unity web player content calls functions in the web page.

Each of these communication directions is described in more detail below.

Calling Unity web player content functions from the web page

The Unity Web Player object has a function, SendMessage(), that can be called from a web page in order to call functions within Unity web player content. This function is very similar to the GameObject.SendMessage function in the Unity scripting API. When called from a web page you pass an object name, a function name and a single argument, and SendMessage() will call the given function in the given game object.

In order to call the Unity Web Player’s SendMessage() function you must first get a reference to the Unity web player object. You can use the GetUnity() function in the default html generated by Unity to obtain a reference to the object. Here is an example JavaScript function that would execute the SendMessage() function on the Unity web player; in turn SendMessage() will then call the function MyFunction() on the game object named MyObject, passing a piece of string data as an argument:

<script type="text/javascript" language="javascript">
<!--
//initializing the WebPlayer
var u = new UnityObject2();
u.initPlugin(jQuery("#unityPlayer")[0], "Example.unity3d");

function SaySomethingToUnity()
{
    u.getUnity().SendMessage("MyObject", "MyFunction", "Hello from a web page!");
}
-->
</script>


Inside of the Unity web player content you need to have a script attached to the GameObject named MyObject, and that script needs to implement a function named MyFunction:

function MyFunction(param : String)
{
    Debug.Log(param);
}


Note: keep in mind that if the function doesn’t have any arguments, then an empty string ("") should be passed as an argument.

A single string, integer or float argument must be passed when using SendMessage(), the parameter is required on the calling side. If you don’t need it then just pass a zero or other default value and ignore it on the Unity side. Additionally, the game object specified by the name can be given in the form of a path name. For example, /MyObject/SomeChild where SomeChild must be a child of MyObject and MyObject must be at the root level due to the ‘/’ in front of its name.

Note: u.getUnity() might return null if the game isn’t fully loaded yet, so it’s a good idea to check if it’s value is not null before using SendMessage(). Or wait for your game to be fully loaded before trying to communicate with it.

Calling web page functions from Unity web player content

In order to call a web page function from within your Unity web player content you must use the Application.ExternalCall() function. Using that function you can call any JavaScript function defined in the web page, passing any number of parameters to it. Here is an example Unity script that uses the Application.ExternalCall() function to call a function named SayHello() found within the web page, passing a piece of string data as an argument:

Application.ExternalCall( "SayHello", "The game says hello!" );


The web page would need to define the SayHello() function, for example:

<script type="text/javascript" language="javascript">
<!--
function SayHello( arg )
{
    // show the message
    alert( arg );
}
-->
</script>


Executing arbitrary browser code from Unity web player content

You don’t even have to define functions in the embedding web page, instead you can use the Application.ExternalEval() function to execute arbitrary browser code from the web player content.

The following example checks that the page embedding the web player content is fetched from a certain host (unity3d.com), if that’s not the case then it will redirect to another URL. This technique can be used to prevent deep linking to your web player content:

Application.ExternalEval(
    "if(document.domain != 'unity3d.com') { document.location='http://unity3d.com'; }"
);


Customizing the Unity Web Player's Behavior
Using web player templates