はじめに
- Unity の 2D 表示と NGUI では、ともに画面の縦幅を固定するオプションはあるが、横幅を固定することができないので、スクリプトで対応したメモ。
Unity 2D で横幅を固定する
- 以下のスクリプトをシーン上の任意のオブジェクトにアタッチする
sing UnityEngine;
using System.Collections;
public class MainCameraAdjustScript : MonoBehaviour {
float FixedWidth = 6.5f;
void Start () {
Camera.main.orthographicSize = ((FixedWidth * Screen.height) / Screen.width) / 2;
}
}
NGUI で横幅を固定する
- 以下のスクリプトを UI Root にアタッチする
using UnityEngine;
using System.Collections;
public class UIRootAdjustScript : MonoBehaviour {
int FixedWidth = 640;
void Awake () {
UIRoot root = GetComponent<UIRoot> ();
root.scalingStyle = UIRoot.Scaling.FixedSize;
root.manualHeight = Mathf.FloorToInt ((Screen.height * FixedWidth) / Screen.width);
}
}