[Unityエディター拡張] 選択式ボタンを使用する

ヨメレバCSS
オリジナルCSS

ボタンのグループ

複数のボタンから選択するような場合は、GUILayout.SelectionGridを使います。

Editorのメンバ変数にprivate int selectedGridIndex= 0;を用意しておいて、

selectedGridIndex = GUILayout.SelectionGrid(selectedGridIndex, new string[] { "box1", "box2", "box3" }, 1);

とすると、

3_4_selectiongrid

というように、ボタンが並びます。

引数に渡した配列でボタンのグループを作成していて、戻り値に選択されたindexが返ってきています。

最後の数値は横に並べる数で、例えばこれを2にすると、

3_4_selectiongrid2

こうなります。

画像だとこんな感じに。

Texture t1 = Resources.Load<Texture>("02_Hackadoll_minichara/01_standing_0");
Texture t2 = Resources.Load<Texture>("02_Hackadoll_minichara/02_thinking_0");
Texture t3 = Resources.Load<Texture>("02_Hackadoll_minichara/03_waving_0");

selectedGridIndex = GUILayout.SelectionGrid(selectedGridIndex, new Texture[] { t1, t2, t3 }, 2, GUILayout.Width(200), GUILayout.Height(200));

3_4_selectiongrid3

これもWidthとHeightを指定しないと画像の大きさそのまま出てしまうので、適当に指定しています。

ツールバー

ボタンをただ横に並べたものを使用したければ、GUILayout.Toolbarを使用します。

メンバ変数にselectedToolbarIndexを用意して、

selectedToolbarIndex = GUILayout.Toolbar(selectedToolbarIndex, new string[] { "toolbar1", "toolbar2", "toolbar3" });

こういう書き方で、

3_4_toolbar

こういう。

戻り値は選択されているインデックスが返ってきます。

SelectionGridと違って、横にぴったり並んでいます。

画像の表示はこうなります。

selectedToolbarIndex = GUILayout.Toolbar(selectedToolbarIndex, new Texture[] { t1, t2, t3 }, GUILayout.Height(100));

3_4_toolbar2

こっちは、横幅は勝手に調整してくれるみたい。

けれども縦に伸びるので、そちらは適当に指定をしています。

スポンサーリンク
GoogleAdSence レクタングル(大)

シェアする

スポンサーリンク
GoogleAdSence レクタングル(大)