javascript - 调整浏览器大小时调整 Unity WebPlayer 元素的大小

标签 javascript css unity-game-engine resize unityscript

我正在尝试让 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() 中,我可以验证当我更改窗口大小时是否正在调用 ResizeUnity()。我可以验证 winWidthwinHeight 在调整大小时是否获得了分配给它们的正确值。但是,无论我对浏览器窗口做什么,Unity WebPlayer 对象都保持与加载时相同的大小。

我做错了什么?

最佳答案

似乎 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";
        }
}

关于javascript - 调整浏览器大小时调整 Unity WebPlayer 元素的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10899374/

相关文章:

javascript - 使用 React 时我应该使用什么来代替 document.queryselector()?

html - figcaption 显示在 img 上,但 figcaption 边框在悬停时显示在图像后面

php - 如何从 Unity 监控数据库中的更改而不淹没 NIC

c# - 嵌套协程生成的代码是什么

javascript - 在两个 React.js 组件之间传递 JSON 作为 Prop

javascript - Javascript 中加载事件的竞争条件

java - 将连续的字符串数据拆分为所需的垂直输出

javascript - 使用 Angular 2/4 多选元素

javascript - Unity3D 播放器尺寸变化?

javascript - 随机 CSS 值