gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 22:23:33 25 4
gpt4 key购买 nike

我正在尝试让 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com