How can you create a Unity3d ActiveX control from within the WebBrowser control?

I am trying to use the Unity3d ActiveX control from within the .Net WebBrowser control.

I published the sample island project provided by Unity and when I open the generated html file in IE8 on Windows 7, everything works fine. I checked the "Allow active content to run in files on My Computer" option in the IE advanced options, so I do not get the security prompt about running the ActiveX control.

However, when I try to do the same in the WebBrowser control, the VBScript in the html file that attempts to create the ActiveX control fails.

set tControl = CreateObject("UnityWebPlayer.UnityWebPlayer.1")

Is there a trick to getting the Unity3d ActiveX control to work in the WebBrowser control?

It looks like this is specific to the WebPlayer ActiveX because the first .html file using the Windows Media Player ActiveX object loads correctly in IE8 and in the WebBrowser control. The second .html file works in IE8 but fails to create the WebPlayer ActiveX object in the WebBrowser control.

From this post on the forum, http://forum.unity3d.com/viewtopic.php?p=203996#203996, I assume that using the WebPlayer ActiveX from inside the WebBrowser control is possible though.

Windows Media Player HTML

<html>
    <head>
        <title>WMP Test</title>
    </head>
    <body>
        <script language="JavaScript">

            var WMP7;

            WMP7 = new ActiveXObject("WMPlayer.OCX.7");

            if ( WMP7 )
            {
                document.write ('<OBJECT ID=MediaPlayer ');
                document.write (' CLASSID=CLSID:6BF52A52-394A-11D3-B153-00C04F79FAA6');
                document.write (' standby="Loading Microsoft Windows Media Player components..."');
                document.write (' TYPE="application/x-oleobject" width="400" height="400">');
                document.write ('<PARAM NAME="url" VALUE="http://download.microsoft.com/download/5/7/1/57139364-d7af-4c18-9ce3-5149a3a727ca/EC304_Friend.wmv">');
                document.write ('<PARAM NAME="AutoStart" VALUE="false">');
                document.write ('<PARAM NAME="ShowControls" VALUE="1">');
                document.write ('<PARAM NAME="uiMode" VALUE="mini">');
                document.write ('</OBJECT>');
            }

        </script>
    </body>
</html>

WebPlayer HTML

<html>
    <head>
        <title>Unity Test</title>
    </head>
    <body>
        <script language="JavaScript">

            var Unity;

            Unity = new ActiveXObject("UnityWebPlayer.UnityWebPlayer.1");

            if ( Unity )
            {
                document.write ('<OBJECT ID=UnityObject ');
                document.write (' CLASSID=CLSID:444785F1-DE89-4295-863A-D46C3A781394');
                document.write (' TYPE="application/vnd.unity" width="400" height="300">');
                document.write ('<PARAM NAME="src" VALUE="IslandDemo.unity3d">');
                document.write ('<PARAM NAME="pluginspage" VALUE="http://www.unity3d.com/unity-web-player-2.x">');
                document.write ('</OBJECT>');
            }

        </script>
    </body>
</html>

The reason the CreateObject call was failing with the WebPlayer ActiveX control was because the call to RegOpenKey(HKCR\UnityWebPlayer.UnityWebPlayer.1) was failing with NAME NOT FOUND.

HKCR is a merged view of HKCU\Software\Classes and HKLM\Software\Classes. That key existed under HKCU, but not under HKLM, so I am not sure why it was failing.

The reason it worked in IE8 directly was because IE8 was reading from HKCU not HKCR.

I assume the reason the key existed under HKCU and not HKLM is that when I installed the WebPlayer ActiveX control, I installed it just for the current user and not all users.

I changed the permissions on Software\Classes\UnityWebPlayer.UnityWebPlayer.1 to give Everyone read access and copied the Software\Classes\UnityWebPlayer.UnityWebPlayer.1 and Software\Classes\CLSID{444785F1-DE89-4295-863A-D46C3A781394} keys from HKCU to HKLM and now I can use the WebPlayer ActiveX control from within the WebBrowser control.