[Unity]NGUIで画面サイズに合わせる(NGUI2.3.0対応版)
以前 [Unity]NGUIで画面サイズに合わせる(その2) で書いたスクリプトが、NGUI 2.3.0 の変更との兼ね合いで正しく動かなくなっていたので、今更ですが対応版です。
UIRoot.automatic が obsolete 扱いになり、代わりに scalingStyle というパラメータに変更になったようです。
横がはみ出ないように画面の幅に合わせる機能は現在でもないようですね。
というわけで、数行しか変わっていませんが 2.3.0 対応版です。
2013/5/4 20:23 追記 : スクリプトに余計な行があったのを修正しました。
UIRoot.automatic が obsolete 扱いになり、代わりに scalingStyle というパラメータに変更になったようです。
- PixelPerfect : 拡大縮小しない(automatic = true 相当)
- FixedSize : 画面の高さに合わせる(automatic = false 相当)
- FixedSizeOnMobile : iOS, Android のときは FixedSize、それ以外では PixedPerfect
横がはみ出ないように画面の幅に合わせる機能は現在でもないようですね。
というわけで、数行しか変わっていませんが 2.3.0 対応版です。
using UnityEngine;
[ExecuteInEditMode]
public class UIRootScale : MonoBehaviour
{
public int manualWidth = 1280;
public int manualHeight = 720;
UIRoot uiRoot_;
public float ratio {
get {
if(!uiRoot_){ return 1.0F; }
return (float)Screen.height / uiRoot_.manualHeight;
}
}
void Awake()
{
uiRoot_ = GetComponent<UIRoot>();
}
void Update()
{
if(!uiRoot_ || manualWidth <= 0 || manualHeight <= 0){ return; }
int h = manualHeight;
float r = (float)(Screen.height * manualWidth) / (Screen.width * manualHeight); // (Screen.height / manualHeight) / (Screen.width / manualWidth)
if(r > 1.0F){ h = (int)(h * r); } // to pretend target height is more high, because screen width is too smaller to show all UI
//if(uiRoot_.automatic){ uiRoot_.automatic = false; } // for before NGUI 2.3.0
if(uiRoot_.scalingStyle != UIRoot.Scaling.FixedSize){ uiRoot_.scalingStyle = UIRoot.Scaling.FixedSize; } // for NGUI 2.3.0 or later
if(uiRoot_.manualHeight != h){ uiRoot_.manualHeight = h; }
if(uiRoot_.minimumHeight > 1){ uiRoot_.minimumHeight = 1; } // only for NGUI 2.2.2 to 2.2.4
if(uiRoot_.maximumHeight < System.Int32.MaxValue){ uiRoot_.maximumHeight = System.Int32.MaxValue; } // only for NGUI 2.2.2 to 2.2.4
}
}
2013/5/4 20:23 追記 : スクリプトに余計な行があったのを修正しました。
[ExecuteInEditMode]
返信削除を付けられたのは理由があるのでしょうか?
また、Updateせずに1度だけでも大丈夫と思います。
どちらも Unity エディタの Game View の動的なサイズ変更に対応するためです。
削除お気付きの通り、リリース時には一度だけ実行にした方がよいと思います。