Unity擴充編輯器1:Editor Windows

來源:互聯網
上載者:User

標籤:

Extending the Editor

Unity允許你使用自己定製的inspectors和Editor Windows擴充編輯器,並且你可以使用定製的Property Drawers定義屬性集在inspector中如何展示,這一塊講述如何使用這些特性。

Editor Windows

你可以在你的app中建立任意數量的定製視窗。它們的表現就像Inspector,Scene或者其它內建的視窗。這是給你的遊戲添加一個子系統使用者介面的絕佳方式。[比如地圖編輯器]

做一個自訂的Editor Window包含以下幾個簡單步驟:

1.建立指令碼,繼承EditorWindow類

2.使用代碼去觸發視窗顯示自己

3.為你的工具實現GUI代碼

Derive From EditorWindow

為了製作你的Editor Window,你的指令碼必須存放在一個叫做"Editor"的檔案夾中。在這個指令碼中建立一個類,它繼承自EditorWindow.然後在內部的OnGUI方法裡面編寫你的GUI控制。

using UnityEngine;using UnityEditor;using System.Collections;public class Example:EditorWindow{  void onGUI()  {    // The actual window code goes here     }}

Showing the window

為了在螢幕上顯示視窗,製作一個menu item來顯示它,這個靠建立一個方法完成,而啟用這個方法則是靠MenuItem屬性。

Unity的預設行為是迴圈利用視窗(即:再次選擇menu item會顯示已經存在的視窗。這是靠使用EditorWindow.GetWindow方法實現),例如:

using UnityEngine;using UnityEditor;using System.Collections;class MyWindow : EditorWindow {    [MenuItem ("Window/My Window")]    public static void  ShowWindow () {        EditorWindow.GetWindow(typeof(MyWindow));    }        void OnGUI () {        // The actual window code goes here    }}

這會創造一個標準的,可停靠的編輯視窗,它會在指令之間儲存它的位置,可以使用自訂的布局,等等。想要獲得更多的操控,你可以使用GetWindowWithRect.

Implementing Your Window‘s GUI

視窗中實際的內容靠實現OnGUI方法繪製。你在遊戲內部GUI中使用的UnityGUI類(GUI和GUILayout)在這裡也可以使用。此外我們提供了一些額外的GUI操作,它們在editor-only類裡面:EditorGUI和EditorGUILayout。這些類添加在了普通類已經可用的控制項集合裡,所以你可以隨意的混合搭配使用它們。

下面的C#代碼展示了你如何添加GUI元素到自訂的EditorWindow中:

using UnityEditor;using UnityEngine;public class MyWindow : EditorWindow{    string myString = "Hello World";    bool groupEnabled;    bool myBool = true;    float myFloat = 1.23f;        // Add menu item named "My Window" to the Window menu    [MenuItem("Window/My Window")]    public static void ShowWindow()    {        //Show existing window instance. If one doesn‘t exist, make one.        EditorWindow.GetWindow(typeof(MyWindow));    }        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 ();    }}

範例代碼的執行結果如:

還想獲得更多資訊,可以看看在EditorWindow頁面的範例和文檔

Unity擴充編輯器1:Editor Windows

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.