Extending Unity's Editor with a Custom Window

Published January 23, 2016
Advertisement

Personal Update:


Woohoo, my previous journal on Unity's Content Pipeline got featured. :D

Snow-pocalypse 2016 has hit Tennessee! I think Nashville received five inches of snow and even the airport shut down. We're a little further south so only got about two inches.

Snow1.jpgSnow2.jpg

This is the dogs' first snow day and they didn't know what to make of it. Malcolm barks at anything different, so his first couple of trips out he kept leaping into the air barking at the snow.

Development Update:


I continued my dive into tools development and decided to look into extending the editor. A Voxel Adventure will be data driven by various XML files (or JSON, or whatever) so the various game objects will be modable by the community (monsters, weapons, quests, etc). While hand-editing XML files isn't difficult, having a dedicated editor window will greatly speed up content generation. And with a little extra work, you can show where any problems are.

Here's what I have so far:

The panel on the left shows a list of all the monsters categorized by type. When a monster has a validation error, the category and name are colored red to draw your attention to problems. The panel on the right shows the selected monster and allows you to edit it's stats. It also shows you the model you have selected for that monster.

I plan to put in more configuration data for things like scale, and texture tinting to squeeze more mileage out of my assets.

Unity provides several ways to extend the editor: custom menus, custom inspectors, scene gizmos, and custom windows. And they provide several helper classes to make your life easier. When building your own window four classes stand out from the rest: GUI, GUILayout, EditorGUI, EditorGUILayout. They have some overlap and there's not much guidance on when you should use which class, but here's what I've observed.

The GUI classes focus on producing controls and elements that would be useful in a game's UI. The EditorGUI classes focus on elements that are more useful in the editor's UI. The Layout classes let you construct series of automatically laid out controls whereas the non-layout classes give you manual control over everything.

Here's an incomplete code sample:
[spoiler]

// Make a class and inherit it from EditorWindowpublic class MonsterDatabaseWindow: EditorWindow{ // Add a MenuItem attribute to a static method and it shows up in the menu bar for you. [MenuItem("Window/EckTech Games/Monster Database")] public static void ShowMonsterDatabaseWindow() { EditorWindow.GetWindow(true, "EckTech Games - Monster Database", true); } // OnGUI gets called every time Unity needs to update the window. public void OnGUI() { EditorGUILayout.BeginHorizontal(); ShowMonsterList(); // Draw the left panel ShowSelectedMonsterData(); // Draw the right panel EditorGUILayout.EndHorizontal(); } // The right panel of data private void ShowSelectedMonsterData() { // Tell the layout class we want a vertical set of controls. EditorGUILayout.BeginVertical(); // This was kind of a weird line at first. This creates a labeled text field // automatically formatted and lined up pretty. We pass in // selectedMonster.MonsterName and it returns the modified value selectedMonster.MonsterName = EditorGUILayout.TextField("Monster Name", selectedMonster.MonsterName); selectedMonster.Level = EditorGUILayout.IntField("Level", selectedMonster.Level); MonsterTypeEnum prevMonsterType = selectedMonster.MonsterType; selectedMonster.MonsterType = (MonsterTypeEnum)EditorGUILayout.EnumPopup("Monster Type", selectedMonster.MonsterType); selectedMonster.Gold = EditorGUILayout.TextField("Gold", selectedMonster.Gold); // ... more control logic here, but you get the point. EditorGUILayout.EndVertical(); }}
And a data file: Spiderling 1 Spider 1d6 Spider Giant Spider 2 Spider 1d6 Spider
[/spoiler]


Tip from your uncle Eck:


If you don't have the right tool for the job... You can MAKE the right tool for the job. Spend a little time thinking about how you can make your content generators' lives easier. In lots of indie game dev case,s the content developer IS the programmer. So do yourself a favor and make a tool that will make you more productive. Faster cycles equals more or better content.

http://docs.unity3d.com/Manual/ExtendingTheEditor.html

3 likes 0 comments

Comments

Nobody has left a comment. You can be the first!
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Advertisement