LoginSignup
7
5

More than 5 years have passed since last update.

UnityのGUILayoutで右寄せ表示

Last updated at Posted at 2016-05-30

ただ間に FlexibleSpace() を挟むだけでは画面全体に対して右寄せにはできなくて手間取ったので、メモしておく

画面全体に対して右寄せにならなかった例

void OnGUI()
{
    GUILayout.BeginHorizontal("box");

    GUILayout.Label("Left");
    GUILayout.FlexibleSpace();
    GUILayout.Label("Right");

    GUILayout.EndHorizontal();
}

スクリーンショット 2016-05-30 18.36.21.png

void OnGUI()
{
    GUILayout.BeginHorizontal("box");
    {
        GUILayout.Label("Left");
        GUILayout.FlexibleSpace();

        GUILayout.BeginVertical("box");
        {
            GUILayout.Label("Right 1");
            GUILayout.Label("Right 2");
        }
        GUILayout.EndVertical();
    }
    GUILayout.EndHorizontal();
}

スクリーンショット 2016-05-30 18.40.12.png

GUILayout.BeginArea を使う

GUILayout.BeginArea を利用してスクリーンサイズのエリアを作ってやることで、FlexibleSpaceが画面端まで伸びてくれるようになった。
http://docs.unity3d.com/ScriptReference/GUILayout.BeginArea.html

void OnGUI()
{
    GUILayout.BeginArea(new Rect(0, 0, Screen.width, Screen.height));
    GUILayout.BeginHorizontal("box");
    {
        GUILayout.Label("Left");
        GUILayout.FlexibleSpace();

        GUILayout.BeginVertical("box");
        {
            GUILayout.Label("Right 1");
            GUILayout.Label("Right 2");
        }
        GUILayout.EndVertical();
    }
    GUILayout.EndHorizontal();
    GUILayout.EndArea();
}

スクリーンショット 2016-05-30 18.49.08.png

デバッグ機能などをサクッと右上につけようと思った時などにどうぞ。
横幅のサイズがわかってれば、そのRectを適当にBeginVerticalとかに渡せばいいけど、面倒なことを意識せずに右によって欲しかったのでこうした。

もっと簡単でスマートな方法などありましたらご教授願いします :bow:

7
5
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
7
5