Introduction 介紹You can create your own custom design tools inside Unity through Editor Windows. Scripts that derive from EditorWindow instead of MonoBehaviour can leverage both GUI/GUILayout and EditorGUI/EditorGUILayout controls. Alternatively, you can use Custom Inspectors to expose these GUI controls in your GameObject Inspector. 可以在Unity中通過編輯器窗口創(chuàng)建自定義設(shè)計工具。腳本從EditorWindow繼承而不是MonoBehaviour,并能使用GUI/GUILayout中EditorGUI/EditorGUILayout的控件來定制。另一種方法,是你能在你的游戲?qū)ο蟊O(jiān)視器中使用自定義監(jiān)視器顯示這些GUI控件的屬性。 Editor Windows 編輯器窗口You can create any number of custom windows in your app. These behave just like the Inspector, Scene or any other built-in ones. This is a great way to add a user interface to a sub-system for your game. 你能夠創(chuàng)建任意數(shù)量的自定義窗體。他們可以像檢視面板,場景或者其他別的內(nèi)建GUI一樣工作。這是為你的游戲子系統(tǒng)創(chuàng)建用戶界面的好方法。
Making a custom Editor Window involves the following simple steps: 使用三個簡單步驟來創(chuàng)建一個自定義編輯器窗口。
Derive From EditorWindow 從EditorWindow繼承In order to make your Editor Window, your script must be stored inside a folder called "Editor". Make a class in this script that derives from EditorWindow. Then write your GUI controls in the inner OnGUI function. 為了創(chuàng)建你的編輯器窗口,你的腳本必須存儲在名叫"Editor"的文件夾中。在這個腳本中創(chuàng)建一個從EditorWindow繼承的類。然后再OnGUI函數(shù)中寫你的GUI控件代碼。
MyWindow.js - placed in a folder called 'Editor' within your project. MyWindow.js文件放置在Editor文件夾 Showing the window 顯示窗口In order to show the window on screen, make a menu item that displays it. This is done by creating a function which is activated by the MenuItem property. 為了在屏幕中顯示窗口,創(chuàng)建一個顯示它的菜單項。你可以通過MenuItem屬性來激活一個函數(shù)來完成這個過程。 The default behavior in Unity is to recycle windows (so selecting the menu item again would show existing windows. This is done by using the function EditorWindow.GetWindow Like this: Unity缺省的行為是循環(huán)使用窗體(因此再次選擇菜單會顯示已經(jīng)存在的窗體。這個功能是通過使用EditorWindow.GetWindow來實現(xiàn)的),如下
Showing the MyWindow This will create a standard, dockable editor window that saves its position between invocations, can be used in custom layouts, etc. To have more control over what gets created, you can use GetWindowWithRect 這會創(chuàng)建一個基礎(chǔ)的,能自定義布局,可停駐的,并在使用間隙保持了自身位置的編輯器窗體。如果希望顯示窗口能覆蓋上一次已存在的設(shè)置,可以使用GetWindowWithRect實現(xiàn)。 Implementing Your Window's GUI 實現(xiàn)窗體GUIThe actual contents of the window are rendered by implementing the OnGUI function. You can use the same UnityGUI classes you use for your ingame GUI (GUI and GUILayout). In addition we provide some additional GUI controls, located in the editor-only classes EditorGUI and EditorGUILayout. These classes add to the controls already available in the normal classes, so you can mix and match at will. 窗體實際渲染的內(nèi)容被實現(xiàn)在OnGUI函數(shù)中。你可以使用和你在游戲中使用的GUI控件(GUI和GUILayout)一樣的UnityGUI類。Unity還額外提供了控件,這些控件包含在只讀的EditorGUI類和EditorGUILayout類中。這些類增加到普通類中已經(jīng)生效的控件集合中,因此你能完全可以混合使用它們。 For more info, take a look at the example and documentation on the EditorWindow page. 如想看更多的例子和文章,請查閱EditorWindow 頁面. Custom Inspectors 自定義檢視面板A key to increasing the speed of game creation is to create custom inspectors for commonly used components. For the sake of example, we'll use this very simple script that always keeps an object looking at a point. 加速游戲開發(fā)的關(guān)鍵是為公共使用的組件創(chuàng)建自定義的檢查器。接下來的列子中,我們能看到一個非常簡單的例子來使一個對象保持指向一個點的方向。
LookAtPoint.js
This will keep an object oriented towards a world-space point. Let's make it cool! 這將保持物體朝向世界的原點,讓我們來讓它變得酷點。 The first step to making it work nicely in the editor is to make the script run even when you're not testing the game. We do this by adding an ExecuteInEditMode attribute to it: 第一步,讓它在編輯環(huán)境里工作的更好,使這個腳本在你沒有開啟游戲測試的時候也能運行。我們?yōu)槟_本添加一個ExecuteInEditMode屬性。
Try adding the script to your main camera and drag it around in the Scene view. 可以試著把這段腳本附加到你的主相機然后繞著場景視圖拖拽它觀察效果。 Making a Custom Editor 創(chuàng)建自定義的編輯器This is all well and good, but we can make the inspector for it a lot nicer by customizing the inspector. To do that we need to create an Editor for it. Create a JavaScript called LookAtPointEditor in a folder called Editor. Unity的功能都不錯,但我們通過自定義監(jiān)視器中的編輯器能讓這一切變得更加美好。我們可以通過在Editor中創(chuàng)建名叫"LookAtPointEditor"的javascript腳本來實現(xiàn)。
This class has to derive from Editor. The @CustomEditor attribute informs Unity which component it should act as an editor for. 這個類繼承至Editor. @CustomEditor屬性告訴Unity這個組件能扮演編輯器的功能。 The code in OnInspectorGUI is exectued whenever Unity displays the inspector. You can put any GUI code in here - it works just like OnGUI does for games, but is run inside the inspector. Editor defines the target property that you can use to access the object being inspected. Unity顯示檢查器的時候被執(zhí)行組件里OnInspectorGUI()中的代碼。你能在這個函數(shù)放置更多的代碼,- 它工作起來更像OnGUI在游戲中完成的一樣,但它確實工作在編輯模式中。編輯器定義了你能用來訪問被檢查的對象的target屬性。 The EditorUtility.SetDirty code is executed if the user has changed any of the values by checking GUI.changed. 我們可以通過檢查GUI.changed知道用戶是否改變了任何數(shù)值,然后EditorUtility.SetDirty將被執(zhí)行。 In this case, we make one of the Vector3 fields like is used in the Transform Inspector - like so: 使用這種方式,我們在Transform Inspector中創(chuàng)建了一個Vector3 fields
There's a lot more that can be done here, but this will do for now - We've got bigger fish to fry... 還有更多可以改進(jìn)的,但先這樣吧,我們還有更重要的事情要做。 Scene View Additions 場景視圖擴展You can add extra code to the Scene View by implementing an OnSceneGUI in your custom editor. In this case, we'll add a second set of position handles, letting users drag the look-at point around in the Scene view. 你可以通過在你的自定義編輯器中實現(xiàn)OnSceneGUI來實現(xiàn)將擴展代碼注入到場景視圖,我們可以添加第二個位置手柄,讓用戶可以在場景視圖中通過拖拽修改上例中的觀察點。
OnSceneGUI works just like OnInspectorGUI - except it gets run in the scene view. To help you make your editing interface, you can use the functions defined in Handles class. All functions in there are designed for working in 3D Scene views. OnSceneGUI會如同OnInspectorGUI一樣工作 – 但它增加了能在場景視圖中運行這個特性。你能使用定義在Handles類中的函數(shù),這將幫助你定制你的編輯界面。所有定義在Handles的函數(shù)都被設(shè)計工作在3D場景視圖中。 If you want to put 2D GUI objects (GUI, EditorGUI and friends), you need to wrap them in calls to Handles.BeginGUI() and Handles.EndGUI(). 如果你想放置2D GUI控件(GUI, EditorGUI 或者其他),你需要將他們包裹在Handles.BeginGUI() 和 Handles.EndGUI()中。 頁面最后更新于: 2010-07-19 |
|