Publishing active content
Unity Manual > Advanced > Web Player Deployment > Publishing active content

Publishing active content

When publishing active content, like Unity Web Player content, developers must consider how that content will be handled in various browsers. Specifically, developers need to consider how that content will be handled by Microsoft's Internet Explorer browser as it offers slightly different behavior than all other supported browsers today. Depending on how your content is embedded within your web page it may require the end-user to activate the content before it will be sensitive to mouse and keyboard input. In order to activate the content the end-user will have to either click once on the content itself, or click OK in a dialog box to load the ActiveX Control. While this doesn't prevent web player content from being used in Internet Explorer, it does offer a potentially confusing and less than optimal end-user experience. Fortunately, Microsoft has provided page design solutions so that developers can create or update their web pages in such a way that end-users do not have to click to activate web player content. Below you will find details on how to implement one of those solutions.

For more information on activating ActiveX Controls in Internet Explorer please read Activating ActiveX Controls.

In order to prevent your end-user from having to activate your Unity Web Player content prior to using it you must use an external JavaScript file to dynamically write the required object tags into your web page at run-time. In order to keep things simple, the solution outlined here will use an external JavaScript file to write both the object and embed tags so the page loads and plays the same in all browsers. In the simplest case you would create an external JavaScript file with a function that when called, writes out the required object and embed tags using document.write() calls.

Here is an example JavaScript function that uses document.write() calls to dynamically create the necessary object and embed tags:

<script type="text/javascript" language="javascript">
function writeUnityTags (aSrc, aWidth, aHeight) {
    // write the content object and embed tags
    document.write("<object ");
    document.write("    classid='clsid:444785F1-DE89-4295-863A-D46C3A781394' \n");
    document.write("    codebase='http://webplayer.unity3d.com/download_webplayer/UnityWebPlayer.cab#version=2,0,0,0' \n");
    document.write("    id='UnityObject' width='" + aWidth + "' height='" + aHeight + "'  > \n");
    document.write("    <param name='src' value='" + aSrc + "' /> \n");
    document.write("    <embed type='application/vnd.unity' pluginspage='http://www.unity3d.com/unity-web-player-2.x' \n");
    document.write("        id='UnityEmbed' width='" + aWidth + "' height='" + aHeight + "' src='" + aSrc + "' \n");
    document.write("    /> \n");
    document.write("</object\>");
}
</script>

Notice that when the function is called you must pass three arguments, those arguments being the path to the web player data file as well as the width and height of the content to be displayed. Using those arguments the function then writes out the object and embed tags directly into the web page that called the function.

Here is an example of using the writeUnityTags() JavaScript function in a web page, where that function is defined in an external JavaScript file named unityweb.js:

<script type="text/javascript" language="javascript" src="unityweb.js"></script>
<script type="text/javascript" language="javascript">

    // call the writeUnityTags function
    writeUnityTags("MyFile.unity3d", 640, 480);

</script>

Using an external JavaScript file with a simple function whose sole purpose is to write out the object and embed tags is sufficient to avoid requiring your end-user to activate the content prior to using and interacting with it. Additionally the above solution functions properly in all browsers and therefore implementing it ensures that the end-user experience is consistent regardless of their browser and platform combination.

Combining the technique described here along with the web player detection routine discussed in Detecting the Unity Web Player using browser scripting yields a predictable and optimized end-user experience. Here is an example of using both the detectUnityWebPlayer() and writeUnityTags() functions, where both functions are defined in an external JavaScript file named unityweb.js:

<script type="text/javascript" language="javascript" src="unityweb.js"></script>
<script type="text/javascript" language="javascript">

    // check for the Unity Web Player and respond appropriately
    var tIsInstalled = detectUnityWebPlayer();
    if (tIsInstalled) {

        // call the writeUnityTags function
        writeUnityTags("MyFile.unity3d", 640, 480);

    } else {

        // write out a simple message prompting the user to install the Unity Web Player
        document.write("<div align='center'> \n");
        document.write("  This content requires the Unity Web Player,");
        document.write("  please use the link below to install the player today:<br /><br /> \n");
        document.write("  <a href='http://www.unity3d.com/unity-web-player-2.x'> \n");
        document.write("    Install the Unity Web Player \n");
        document.write("  </a> \n");
        document.write("</div> \n");

    }
</script>