!!!Unityエディタの拡張 参考ページ http://docs.unity3d.com/Documentation/ScriptReference/EditorWindow.html http://docs-jp.unity3d.com/Documentation/Components/gui-ExtendingEditor.html http://wiki.unity3d.com/index.php/Wizards http://www.slideshare.net/lucifuges/extending-the-unity-editor-extended Unity自身に独自のメニューを追加したり機能拡張できます。 !!拡張スクリプトの作成手順 プロジェクトのAssetsフォルダに「Editor」フォルダを作成。 この中に「MyWindow.cs」のようなスクリプトファイルを作成。 Assets Editor MyWindow.cs <== images Materials のように配置。 MyWindow.cs内は以下のように記述。 using UnityEngine; using UnityEditor; public class MyWindow : EditorWindow { private string myString = "Hello World"; private bool groupEnabled; private bool myBool = true; private float myFloat = 1.23f; // Add menu named "My Window" to the Window menu [MenuItem ("Window/My Window")] static void Init() { // Get existing open window or if none, make a new one: MyWindow window = (MyWindow)EditorWindow.GetWindow(typeof(MyWindow)); window.Show(); } void OnGUI() { GUILayout.Label("Base Settings", EditorStyles.boldLabel); myString = EditorGUILayout.TextField ("Text Field", myString); groupEnabled = EditorGUILayout.BeginToggleGroup("Optional Settings", groupEnabled); myBool = EditorGUILayout.Toggle("Toggle", myBool); myFloat = EditorGUILayout.Slider("Slider", myFloat, -3, 3); EditorGUILayout.EndToggleGroup(); } } ※ ファイル名とクラス名(ここではMyWindow)は同じである必要があります。 ※ コントロールで全角文字は指定できません。 Unityを再起動すると、メインメニューの「Window」-「My Window」のメニュー項目が追加されています。 これを選択すると、独自ウィンドウが表示されます。 {{ref_image unity_mywindow.png}} !!メニューの表示位置 [MenuItem ("Window/My Window")] と指定した箇所で、Windowメニュー内に配置しています。 ここで、 [MenuItem ("Custom/My Window")] のように存在しないメニュー名を指定すると、メインメニューに新しく「Custom」が追加され、この中にMy Windowのメニュー項目が表示されます。 !!ウィンドウの位置とサイズ EditorWindow派生クラスの「positon」の(x, y)がスクリーン上での位置、 (width, height)がウィンドウサイズ。 !!ウィンドウの種類 !EditorWindow UIとしてUnity内でドッキングできるウィンドウ。 !ScriptableWizard ウィザード形式で形状(GameObject)を作成するツールを提供できる。 こちらのほうがEditorWindowよりも簡単に操作できる。 http://docs.unity3d.com/Documentation/ScriptReference/ScriptableWizard.html EditorWindowを継承しているため、EditorWindowでできることは一通り使える。 !!使えるコントロール !縦横配置のボックス EditorGUILayout.BeginHorizontal 〜 EditorGUILayout.EndHorizontalで横にコントロールを並べたグループにする。 EditorGUILayout.BeginVertical〜 EditorGUILayout.EndVerticalで縦にコントロールを並べたグループにする。 第一引数には"box"を指定。 EditorGUILayout.BeginHorizontal("box"); GUILayout.Label("Item1"); GUILayout.Label("Item2"); GUILayout.Label("Item3"); EditorGUILayout.EndHorizontal(); EditorGUILayout.Space(); EditorGUILayout.BeginVertical("box"); GUILayout.Label("Item1"); GUILayout.Label("Item2"); GUILayout.Label("Item3"); EditorGUILayout.EndVertical(); {{ref_image unity_editor_guiLayout_box.png}} ※ Labelの第二引数は指定できない模様。 BeginHorizontal/BeginVerticalの引数を指定しない場合は、枠は描画されない。 !Space (EditorGUILayout) 1行分のスペースを空ける。 EditorGUILayout.Space(); !Label (GUILayout) 静的なテキスト。 GUILayout.Label("Item1"); GUILayout.Label("Item1", EditorStyles.boldLabel); GUILayout.Label("Item1", EditorStyles.largeLabel); GUILayout.Label("Item1", EditorStyles.miniLabel); {{ref_image unity_editor_guiLayout_label.png}} 引数を指定できるが、あまり変化はないかも。 !LabelField (EditorGUILayout) 「項目名 値」で表示される静的テキスト。整列してくれる。 EditorGUILayout.LabelField("Item1:", "012345"); EditorGUILayout.LabelField("ItemTest:", "hoehoe"); EditorGUILayout.LabelField("Data:", "xxxxxx"); {{ref_image unity_editor_guiLayout_labelfield.png}} !TextField (EditorGUILayout) 1行入力ボックス。 myString = EditorGUILayout.TextField ("Text Field", myString); OnGUIコールバック関数が呼ばれるたびに第二引数で指定した文字列を表示し、変更があった場合は戻り値で返る。 {{ref_image unity_editor_guiLayout_textfield.png}} !Toggle (EditorGUILayout) チェックボックス。 myBool = EditorGUILayout.Toggle("Toggle", myBool); OnGUIコールバック関数が呼ばれるたびに第二引数で指定したboolでOn/Offし、変更があった場合は戻り値で返る。 {{ref_image unity_editor_guiLayout_toggle.png}} !Button (GUILayout) プッシュボタン。 if (GUILayout.Button("Push!")) { Debug.Log("Push button."); } {{ref_image unity_editor_guiLayout_button.png}} !Vector2Field (EditorGUILayout) Vector2型の入力フィールド。 p1 = EditorGUILayout.Vector2Field("Point 1:", p1); p2 = EditorGUILayout.Vector2Field("Point 2:", p2); {{ref_image unity_editor_guiLayout_vector2.png}} !Vector3Field (EditorGUILayout) Vector3型の入力フィールド。 p1 = EditorGUILayout.Vector3Field("Point 1:", p1); p2 = EditorGUILayout.Vector3Field("Point 2:", p2); {{ref_image unity_editor_guiLayout_vector3.png}} !Popup (EditorGUILayout) ポップアップメニュー。 private string [] items = {"Item1", "Item2", "Item3"}; private int selectIndex = 0; void OnGUI() { selectIndex = EditorGUILayout.Popup(selectIndex, items); } 第一引数の選択インデックス、第二引数に表示する文字列の配列を指定。 戻り値に選択されたインデックスが返る。 {{ref_image unity_editor_guiLayout_popup.png}} selectIndex = EditorGUILayout.Popup("Popup:", selectIndex, items); とすると、左に項目名を表示できる。 !IntField (EditorGUILayout) int値の入力フィールド。 iVal = EditorGUILayout.IntField("Int Value:", iVal); {{ref_image unity_editor_guiLayout_int.png}} !FloatField (EditorGUILayout) float値の入力フィールド。 fVal = EditorGUILayout.FloatField("Float Value:", fVal); {{ref_image unity_editor_guiLayout_float.png}} !ColorField (EditorGUILayout) Colorの指定。 col = EditorGUILayout.ColorField("Color :", col); {{ref_image unity_editor_guiLayout_color.png}} !ObjectField (EditorGUILayout) Texture/Material/Object/Lightなどの選択。 public Object source; void OnGUI() { source = EditorGUILayout.ObjectField(source, typeof(Object), true); } 第三引数がtrueの場合はシーンのGameObjectも含む。 {{ref_image unity_editor_guiLayout_objectfield.png}} また、第四引数以降で「GUILayout.Width」「GUILayout.Height」などを順不同で列挙していくことで配置を調整できる。 private Texture2D orgTexture; void OnGUI() { orgTexture = EditorGUILayout.ObjectField(orgTexture, typeof(Texture2D), true, GUILayout.Width(64), GUILayout.Height(64)) as Texture2D; } これで、64x64 pixelのプレビュー領域付きのテキスト選択ができる。 Unity 4.3では、「GUILayout.Width(64), GUILayout.Height(64)」の指定をすると コントロール自身が非表示になってしまうようです。 Unity 4.3ではこれを指定しなくても、適切な正方形のサイズで表示されます。 Unity 4.3では「GUILayout.MinWidth(64), GUILayout.MinHeight(64)」と指定するとリサイズ可能。 {{ref_image unity_editor_guiLayout_objectfield_size.png}} !DrawPreviewTexture (EditorGUI) ウィンドウの左上を(0, 0)としたときの座標とサイズを指定して、テクスチャを描画する。 Texture2D tex = 何かテクスチャ. if (tex != null) EditorGUI.DrawPreviewTexture(new Rect(x, y, wid, hei), tex); {{ref_image unity_editor_preview_texture.png}} !ScrollView (EditorGUILayout) スクロール領域を設け、その中にコントロールを入れる。 private Vector2 m_scrollPos = new Vector2(0, 0); private bool m_chk1 = true; private bool m_chk2 = true; private bool m_chk3 = true; private Color m_col = Color.black; void OnGUI() { m_scrollPos = EditorGUILayout.BeginScrollView(m_scrollPos, GUILayout.Height(60)); m_chk1 = EditorGUILayout.Toggle("Chk1:", m_chk1); m_chk2 = EditorGUILayout.Toggle("Chk2:", m_chk2); m_chk3 = EditorGUILayout.Toggle("Chk3:", m_chk3); m_col = EditorGUILayout.ColorField("Color:", m_col); EditorGUILayout.EndScrollView(); } EditorGUILayout.BeginScrollView 〜 EditorGUILayout.EndScrollViewで囲った間でスクロールする。 また、Vector2型のm_scrollPosを第一引数に指定、戻り値として受け取りとすることで、 スクロールに対応。 「GUILayout.Height(60)」の指定で、高さを60 pixelで固定化。 {{ref_image unity_editor_scrollView.png}} ---- {{lastmodified}}