Legacy Documentation: Version 4.5
Previous
Web Player Deployment
Next
Working with UnityObject2

HTML code to load Unity content

Unity content is loaded in the browser by the Unity Web Player plugin. HTML code usually does not communicate with this plugin directly but via the script called UnityObject2. Its primary task is to make Unity content embedding very simple by shielding the user from various browser- and platform-specific issues. It also enables easy Web Player installation.

The HTML file generated by Unity when building a web player contains all the commonly required functionality. In most cases you don’t have to modify the HTML file at all. The rest of the document explains the inner workings of this file.

The UnityObject2 script has to be loaded before it can be used. This is done at the top of the <head> section.

<script type="text/javascript">
<!--
var unityObjectUrl = "http://webplayer.unity3d.com/download_webplayer-3.x/3.0/uo/UnityObject2.js";
if (document.location.protocol == 'https:')
    unityObjectUrl = unityObjectUrl.replace("http://", "https://ssl-");
document.write('<script type="text/javascript" src="' + unityObjectUrl + '"></script>');
-->
</script>


You can now instantiate the UnityObject2 class to assist you in various Unity-related tasks, the most important one being embedding Unity content. This is performed by instantiating UnityObject2 and calling initPlugin on the new instance. initPlugin accepts several parameters. The first one specifies the id of the HTML element that will be replaced by Unity content. It could be any HTML element with <div> being the most common. Think of it as a temporary placeholder where Unity should be placed. The second parameter specifies the path to the web player data file to be displayed. See UnityObject2.initPlugin for more info.

var u = new UnityObject2();
u.initPlugin(jQuery("#unityPlayer")[0], "Example.unity3d");


Finally, the HTML placeholder is placed inside the <body> section. It could be as simple as <div id=“unityPlayer” />. However for maximum compatibility it’s best to place some warning message in case the browser doesn’t support JavaScript and the placeholder isn’t replaced by UnityObject.

<div id="unityPlayer">
    <div class="missing">
        <a href="http://unity3d.com/webplayer/" title="Unity Web Player. Install now!">
            <img alt="Unity Web Player. Install now!" src="http://webplayer.unity3d.com/installation/getunity.png" width="193" height="63" />
        </a>
    </div>
</div>


Previous
Web Player Deployment
Next
Working with UnityObject2