2

我试图让 Unity WebPlayer 控件在调整浏览器大小时调整大小。这是我认为相关的代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <script type="text/javascript">
        <!--
        function GetUnity() 
        {
            if (typeof unityObject != "undefined") 
            {
                return unityObject.getObjectById("unityPlayer");
            }
            return null;
        }

        function ResizeUnity()
        {
            //This function properly assigns innerWidth and Height to winWidth and winHeight
            GetWindowSize();

            var unity = GetUnity();
            if(unity != null)
            {
                //This does not properly resize anything at all
                unity.width = winWidth;
                unity.height = winHeight;
            }
        }
    }

    if (typeof unityObject != "undefined") 
    {
        GetWindowSize();
        //This replaces the unityPlayer div below with an actual WebPlayer object
        unityObject.embedUnity("unityPlayer", "WebPlayer.unity3d", winWidth, winHeight, params);            
    }
    -->
    </script>
    <style type="text/css">
    <!--
    div#unityPlayer {
        cursor: default;
        height: 100%;
        width: 100%;
    }
    -->
    </style>
    </head>
    <body margin="0" marginwidth="0" marginheight="0" scroll="no" onResize="ResizeUnity()">
        <div class="content">
            <div id="unityPlayer">
                <div class="missing">
                    <a href="http://unity3d.com/webplayer/" title="Unity Web Player. Install now!">
                    </a>
                </div>
            </div>
        </div>
    </body>
</html>

从代码中的各种alert()s 中,我可以验证ResizeUnity()在更改窗口大小时正在调用它。我可以验证这一点,winWidthwinHeight在调整大小时为它们分配适当的值。但是,无论我对浏览器窗口做什么,Unity WebPlayer 对象都保持与加载时相同的大小。

我究竟做错了什么?

4

2 回答 2

2

似乎该GetUnity()函数没有在其层次结构中抓取足够高的元素,div以便我调整正确元素的大小。用这个更新我的ResizeUnity()函数就可以了:

function ResizeUnity()
{
    //This function properly assigns innerWidth and Height to winWidth and winHeight
    GetWindowSize();

    var unity = document.getElementById('unityPlayer');
    if(unity != null)
        {                   
            unity.style.width = winWidth + "px";
            unity.style.height = winHeight + "px";
        }
}
于 2012-06-07T18:53:32.273 回答
0

您需要调整div包含您的游戏的属性。大多数时候 CSS 就足以做到这一点,您不需要 JavaScript。

这是一个使用浏览器的全宽和全高显示游戏的工作示例:

<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <script type="text/javascript" src="http://code.jquery.com/jquery-2.0.3.min.js"></script>
        <script type="text/javascript" src="http://webplayer.unity3d.com/download_webplayer-3.x/3.0/uo/UnityObject2.js"></script>
        <script type="text/javascript">
            var u = new UnityObject2();
            jQuery(function() {
                u.initPlugin(jQuery("#unityPlayer")[0], "http://unity3d.com/files/live-demos/angrybots/AngryBots.unity3d");
            });
        </script>
        <style type="text/css">
        html, body {
            height: 100%;
            margin: 0;
        }
        #unityPlayer {
            position:fixed;
            top:0px;
            left:0px;
            height: 100%;
            width: 100%;
        }
        </style>
    </head>
    <body>
        <p>Some text that will be hidden...</p>
        <div id="unityPlayer"></div>
        <p>Some more text that will also be hidden.</p>
    </body>
</html>
于 2014-01-01T23:29:29.977 回答