2

ブラウザのサイズが変更されたときにUnityWebPlayerコントロールのサイズを変更しようとしています。これが私が適切だと思うコードです:

<!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()、ウィンドウのサイズを変更したときに呼び出されていることを確認できます。winWidthサイズ変更時に、それを確認し、winHeight適切な値が割り当てられていることを確認できます。ただし、ブラウザウィンドウに何をしても、UnityWebPlayerオブジェクトはロードされたときと同じサイズのままです。

私は何が間違っているのですか?

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 に答える