Detecting the Unity Web Player using browser scripting
Unity Manual > Advanced > Web Player Deployment > Detecting the Unity Web Player using browser scripting

Detecting the Unity Web Player using browser scripting

Any time you build and post Web Player content you should take steps to ensure the end-user has the Unity Web Player before attempting to display the content. While the default HTML file generated when you publish web player content contains a noembed tag for the cases where the end-user doesn't have the player, that tag isn't used or displayed consistently across all browsers and fails to offer a consistent and predictable user experience. Therefore instead of relying on the browser's handling of the noembed tag you can use browser script to determine whether the end-user has the Unity Web Player installed and respond appropriately.

Detecting the Unity Web Player in all browsers can be done using a combination of JavaScript and VBScript browser code. The process of detecting the web player involves the following steps:

Here is an example JavaScript and VBScript function that performs the above steps in order to detect the Unity Web Player:

<script language='VBScript'>
function detectUnityWebPlayerActiveX
    on error resume next
    dim tControl
    dim res
    res = 0
    set tControl = CreateObject("UnityWebPlayer.UnityWebPlayer.1")
    if IsObject(tControl) then
        res = 1
    end if
    detectUnityWebPlayerActiveX = res
end function
<script language="javascript1.1" type="text/javascript">
function detectUnityWebPlayer () {
    var tInstalled = false;
    if (navigator.appVersion.indexOf("MSIE") != -1 &&
        navigator.appVersion.toLowerCase().indexOf("win") != -1)
        tInstalled = detectUnityWebPlayerActiveX();
    else if (navigator.mimeTypes && navigator.mimeTypes["application/vnd.unity"])
        if (navigator.mimeTypes["application/vnd.unity"].enabledPlugin &&
            navigator.plugins && navigator.plugins["Unity Player"])
            tInstalled = true;	
    return tInstalled;	

When the function above is called, it checks for the Unity Web Player and returns a boolean value as a result. A return value of true indicates that the Unity Web Player is installed whereas a return value of false indicates that it is not. The HTML file generated by Unity when building web player contains a very similar function.

The detection is performed separately for Internet Explorer and all the other browsers:

Here is an example of using the function within a HTML page to detect the Unity Web Player and then respond appropriately:

<script type="text/javascript" language="javascript">
-- check for the Unity Web Player
var tIsInstalled = detectUnityWebPlayer();
if (tIsInstalled)
    // write the content object and embed tags
    document.write("<object classid='clsid:444785F1-DE89-4295-863A-D46C3A781394' \");
    document.write("  codebase=',0,0,0' \n");
    document.write("  id='UnityObject' width='600' height='450' > \n");
    document.write("  <param name='src' value='MyDataFile.unity3d' /> \n");
    document.write("  <embed type='application/vnd.unity' pluginspage='' \n");
    document.write("    id='UnityEmbed' width='600' height='450' src='MyDataFile.unity3d' \n");
    document.write("  /> \n");
    // 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=''> \n");
    document.write("    Install the Unity Web Player \n");
    document.write("  </a> \n");
    document.write("</div> \n");

As you can see the function is used to detect the Unity Web Player, then if the web player is found the required object and embed tags are written into the page. Note that if the web player is not found then a message is displayed prompting the user to install the Unity Web Player.