Reliable $1 Web Hosting by 3iX

Saturday, September 25, 2010

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:
    "if(document.location.host != 'unity3d.com') { document.location='http://unity3d.com'; }"

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 theApplication.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:

Calling Unity web player content functions from the web page

The Unity Web Player plugin and ActiveX Controls both have 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 content object being displayed. You can use JavaScript's document object and its getElementById() function to obtain a reference to the content. Here is an example JavaScript function that would execute the SendMessage() function on the Unity web player content with an object/embed tag id value of UnityContent; in turn SendMessage() will then call the function MyFunction() on the game object named MyObject, passing a piece of string data as an argument:

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)
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.

The default html file generated when you publish web player content includes both an object and embed tag in order to have the content load properly in all browsers. In order to allow browser-based JavaScript to distinguish between the two tag elements they each use a unique id value, UnityObject for the object tag and UnityEmbed for the embed tag. Because of this, the default html file also includes a JavaScript function, GetUnity(), that performs some simple browser detection and returns a reference to the tag element in use. Here is an example using that function: