- Home /
Trying to resize Unity WebPlayer control when resizing browser
I'm trying to get a Unity WebPlayer control to resize when the browser is resized. Here's the code I think is pertinent:
<!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();
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>
From various alert()
s in the code, I can verify that ResizeUnity()
is being called when I change the size of the window. I can verify that winWidth
and winHeight
are getting proper values assigned to them upon resizing. But, no matter what I do to the browser window, the Unity WebPlayer object stays the same size it was when it was loaded.
What am I doing wrong?
Answer by chaosTechnician · Jun 07, 2012 at 07:29 PM
It seems the GetUnity()
function wasn't grabbing an element high enough up its div
hierarchy for me to resize the correct element. Updating my ResizeUnity()
function with this did the trick:
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";
}
}
Your answer
Follow this Question
Related Questions
Documentation for Asset Store 1 Answer
How to pass variables to my WEBapp through the html provided after building the app? 1 Answer
Parse Array data to generate html 0 Answers
HTML Question, making web player fill out web browser window? 2 Answers
How do I call a Unity function from HTML using JavaScript? 1 Answer