2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

GUIのボタンの右に▼を入れる

Last updated at Posted at 2015-04-29

GUIでポップアップを作りたかった

  1. GUIでポップアップを作ろうと思った(EditorWindowで遊んでた)
  2. ポップアップメニューによくある右のやつ(スクリーンショット 2015-04-30 02.14.30.png
    )を表示したかった
  3. GUIStyleとか調べたけどそんなもんはねえって言われた
  4. GetCursorPixelPositionを使って長さ計測してちょうどいい感じにするやり方の話

その他やったこと

  • 画像を使ってimagePositionでできないの?
      右側なんてオプションはねえ
  • GUIStyle.overflowでずらせば?
      文字よりも画像のほうが先に描画された

GetCursorPixelPosition の使い方

public Vector2 GetCursorPixelPosition(Rect position, GUIContent content, int cursorStringIndex);

公式ドキュメントにすら「Get the pixel position of a given string index.」とかしか書いてないこのメソッドは試してみたところ

positionを起点としてこのスタイルでcontentの内容を描画した時cursorStringIndexの文字の描画位置の左上を返してくれる

ということがわかったのでこれを元にforで回して描画範囲を超えたらその一個前で止めるってことにしました。
(多分空白計算したほうが早いけど思考停止)

                var text = content.text;
                int i = 0;
                int border = (int)(rect.position.x + rect.size.x);
                for (; ; i++)
                {
                    content.text = text + new String(Enumerable.Repeat(' ', i).ToArray()) + "▼▼";//これがボーダー超えるまで回す
                    var s = style.GetCursorPixelPosition(rect, content, content.text.Length);
                    if (s.x > border)
                    {
                        break;
                    }
                }
                content.text = text + new String(Enumerable.Repeat(' ', i).ToArray()) + "▼";

                if (GUI.Button(rect, content, style))
                {
                    //やりたいこと
                }

これでとりあえずいいかんじになりました。
スクリーンショット 2015-04-30 02.40.36.png

というわけで出来たはできたんだけどUnityちゃんの便利な機能でできませんかね!
EditorGUILayout使えってことですね!

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?