Unity Web Player无法在.xhtml中运行

时间:2014-03-28 15:34:49

标签: javascript xhtml unity3d

当我尝试使用javascript在xhtml文件中启动统一网页时,网页无法检测到网页播放器并要求我下载它。

与.html相同的源(由Unity本身生成)可以正常工作

为什么呢? :o

<script type='text/javascript' src='https://ssl-webplayer.unity3d.com/download_webplayer-3.x/3.0/uo/jquery.min.js'></script>
    <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>
    <script type="text/javascript">
    <!--
        var config = {
            width: 512, 
            height: 260,
            params: { enableDebugging:"0" }

        };
        var u = new UnityObject2(config);

        jQuery(function() {

            var $missingScreen = jQuery("#unityPlayer").find(".missing");
            var $brokenScreen = jQuery("#unityPlayer").find(".broken");
            $missingScreen.hide();
            $brokenScreen.hide();

            u.observeProgress(function (progress) {
                switch(progress.pluginStatus) {
                    case "broken":
                        $brokenScreen.find("a").click(function (e) {
                            e.stopPropagation();
                            e.preventDefault();
                            u.installPlugin();
                            return false;
                        });
                        $brokenScreen.show();
                    break;
                    case "missing":
                        $missingScreen.find("a").click(function (e) {
                            e.stopPropagation();
                            e.preventDefault();
                            u.installPlugin();
                            return false;
                        });
                        $missingScreen.show();
                    break;
                    case "installed":
                        $missingScreen.remove();
                    break;
                    case "first":
                    break;
                }
            });
            u.initPlugin(jQuery("#unityPlayer")[0], "BulletImages.unity3d");
        });
    -->
    </script>



        <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>

1 个答案:

答案 0 :(得分:1)

JavaScript不允许在XHTML页面上使用document.write()

请注意有问题的摘录:

<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>

您可能会注意到有问题的脚本执行的操作非常简单。如果你知道你的部署,你可以剥离它并明确地包括结果:

如果部署到HTTP网站:

<script type="text/javascript" src="http://webplayer.unity3d.com/download_webplayer-3.x/3.0/uo/UnityObject2.js"></script>

如果部署到HTTPS网站:

<script type="text/javascript" src="https://ssl-webplayer.unity3d.com/download_webplayer-3.x/3.0/uo/UnityObject2.js"></script>

如果您不确定,理想的解决方法是修改网页的DOM,但是edit a node's innerHTML directly会有一个快速而又肮脏的解决方法。