【Unity エディタ拡張】簡単なレイアウトを作成するメモ
2019-09-21 2019-12-04
エディタ拡張を行う際に,毎回タブ内にLineを書いたりボタンを配置するだけの部分でも忘れるのでメモを残す.
追記予定.
Environment
- Unity 2019.1.12f1
- Windows 10 64bit
Method
タブを作成する
以下のようなタブを作成するクイックリファレンス
EditorWindowクラスを継承させ,Openメソッド内のif文ではタブを複数開けないように設定している.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
using UnityEditor; public class Tools : EditorWindow { static Tools tools; [MenuItem("Tools/Main")] static void Open() { if (tools == null) { tools = CreateInstance<Tools>(); } tools.Show(); } } |
色付きのボタンを配置する
まず便利なスコープを追加する
参照元 : https://anchan828.github.io/editor-manual/web/part1-editorgui.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
/// <summary> /// Extention scope /// https://anchan828.github.io/editor-manual/web/part1-editorgui.html /// </summary> public class BackgroundColorScope : GUI.Scope { private readonly Color color; public BackgroundColorScope(Color color) { this.color = GUI.backgroundColor; GUI.backgroundColor = color; } protected override void CloseScope() { GUI.backgroundColor = color; } } |
その次に以下のソースを追加することで色付きのボタンを実装できる.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
private Vector2 _buttonMinSize = new Vector2(100f, 40f); private Vector2 _buttonMaxSize = new Vector2(150f, 40f); private void Sample(){ using (new BackgroundColorScope(Color.cyan)) { //色付きボタン if (GUILayout.Button("Setup input settings", GUILayout.MinWidth(_buttonMinSize.x), GUILayout.MinHeight(_buttonMinSize.y), GUILayout.MaxWidth(_buttonMaxSize.x), GUILayout.MaxHeight(_buttonMaxSize.y))) { Debug.Log("Button clicked"); } } } |
色付きの線を描画する
上記のBackgroundColorScopeを追加の後,以下のソースを追加する
1 2 3 4 5 6 7 |
private void Sample(){ //Draw line using (new BackgroundColorScope(Color.white)) { GUILayout.Box("", GUILayout.Width(this.position.width), GUILayout.Height(1)); } } |
タブビューを実装する
タブビューを実装する方法.個人的に簡単でいろいろできて好き.
以下のTabA, TabB, TabCに対応したレイアウト構築用メソッドを別途用意する必要がある.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
void OnGUI() { int selectedTabNumber = GUILayout.Toolbar(_selectedTabNumber, new string[] { "Initialize", "Main tools", "Options" }, EditorStyles.toolbarButton); switch (selectedTabNumber) { case 0: TabA(); break; case 1: TabB(); break; case 2: TabC(); break; default: Debug.LogError("Unknown tab number"); break; } } |
Ref.
https://anchan828.github.io/editor-manual/web/part1-editorwindow.html