Manual     Reference     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:

  • Browser Detection: is the page being viewed on Windows using Microsoft Internet Explorer or some other browser?
  • ActiveX Control Detection: If the page is being viewed on Windows in Internet Explorer, is the web player's ActiveX Control installed?
  • Plugin Detection: if the page is being viewed in any other browser, is the mime type understood and the plugin installed?

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, res, ua, re, matches, major
    res = 0
    set tControl = CreateObject("UnityWebPlayer.UnityWebPlayer.1")
    if IsObject(tControl) then
        if tControl.GetPluginVersion() = "2.5.0f5" then
            ' 2.5.0f5 on Vista and later has an auto-update issue
            ' on Internet Explorer. Detect Vista (6.0 or later)
            ' and in that case treat it as not installed
            ua = Navigator.UserAgent
            set re = new RegExp
            re.Pattern = "Windows NT (\d+)\."
            set matches = re.Execute(ua)
            if matches.Count = 1 then
                major = CInt(matches(0).SubMatches(0))
                if major < 6 then
                    res = 1
                end if
            end if
        else
            res = 1
        end if
    end if
    detectUnityWebPlayerActiveX = res
end function
</script>
<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;	
}
</script>

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:

  • For IE, a VBScript snippet is used. VBScript is used instead of JavaScript because after detection it releases the plugin resource immediately (whereas in JavaScript it would only happen the next time garbage collection is performed). The script tries to create an ActiveX object named "UnityWebPlayer.UnityWebPlayer.1" which is the name of Unity Web Player ActiveX control.
  • For other browsers it is checked whether a MIME type "application/vnd.unity" is understood and plugin named "Unity Player" is installed and enabled.

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='http://webplayer.unity3d.com/download_webplayer/UnityWebPlayer.cab#version=2,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='http://www.unity3d.com/unity-web-player-2.x' \n");
    document.write("    id='UnityEmbed' width='600' height='450' src='MyDataFile.unity3d' \n");
    document.write("  /> \n");
    document.write("</object>");
}
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>

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.