LoginSignup
11
12

More than 5 years have passed since last update.

UnityのGUIが実機でずれるときの簡単な対応

Posted at

Unity公式的にもNGUIをお勧めしてて、使いづらいことで有名なUnityのGUIですが、NGUI使わない場合はどうやるのって、ちょっとハマったのメモです。

Unity4.6からこのへんやりやすくなったuGUIが搭載されたりして今だけの話かもしれません。そうであって欲しい。

やることとしてはGUI.matrixで想定しているスクリーンサイズにあわせて変形させるだけです。GUI.matrixは指定して以降の全てのGUIの描画に影響するのでGUI.matrixを既に使ってるよって場合は使いづらい作戦かもしれません。

まず、GUI.matrixに設定する行列を計算する関数をつくって、

public static void AutoResize(int screenWidth, int screenHeight)
{
    Vector2 resizeRatio = new Vector2((float)Screen.width / screenWidth, (float)Screen.height / screenHeight);
    GUI.matrix = Matrix4x4.TRS(Vector3.zero, Quaternion.identity, new Vector3(resizeRatio.x, resizeRatio.y, 1.0f));
}

OnGUIの最初で呼び出します。これだけで以降に描画したパーツにmatrixが適応されます。AutoResizeの引数は想定しているサイズ、自分がUnityのアプリケーション上で表示しているスクリーンサイズになると思います。適当なシーンで停止ボタン押して調整するとやりやすいです。

void OnGUI () {
    AutoResize(320, 480);

    GUI.Label (new Rect (10, 0, 300, 70), "hogehoge");
}

参考
http://www.bensilvis.com/?p=500

11
12
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
12