Extending Unity Editor
When i just started to work with Unity i'd tried to extend Unity editor several times in order to do my current work faster. I couldn't find structured and complete guide on coding for the editor. There was a lot of articles about particular issues but it would not help me in my current task so i rather got back to manual work on my current tasks in order to finish them in time. There also were cases when i resolved some tasks but in an ugly way and with inappropriate tools or APIs since i didn't know about those which can fit my needs.
Spending more time on working with editor i had learned more and more ways to use it. I think the best development manual is working example. But only in case if it clean and short. So i decided to collect examples of appliance different ways of extending Unity in hope you can examine it all and choose appropriate for you tasks. So i guess i can call it a little Cookbook for extending Unity editor.
Table of contents
- Intro. Exploring abilities of standard inspector.
- Extending basics. Call editor code through menu items.
- Using OnValidate().
- ExecuteInEditMode. The interaction which acts the same in edit and play mode.
- Other ways to make interactive tools in editor.
- Laying out the GUI.
- Property drawers - defining custom inspector for fields of exact type.
- Writing custom inspectors for yours MonoBehaviours.
Demonstration of the standard inspector abilities based on built-it Unity types and meta-attributes.
Several scripts for everyday use
Build and run current scene (Windows-only)
Sets up the build configuration to include the opened scene only, adds name, makes build and runs it. Can be found in menu "Build tools/Build and run current scene" or by pressing alt+x.
Distribute objects by name
Script for processing scenes (from asset store for example) which consist of many objects of several types. Objects can be separated by name into groups. You need to define pattern to define a part of the name which would be used as basis for grouping. For example, you have objects:
- Wall (1)
- Wall (2)
- Fence (1)
- Fence (2)
In this case you may want to group objects by part before first whitespace or "(" character. Open "02-EditorScriptingBasics.unity" scene, select objects and call "Window/Distribute items by name" menu item.
Distribute objects in space
Distributes items in space on equal distances between first and last object (according ti the scene hierarchy). Correct working granted only if all selected objects have same parent.
Makes new gameobject called "Group" and puts all selected ones to it as a children. Press Ctrl+g or choose in the menu.
OnValidate() usage example
[ExecuteInEditMode] usage example
The original content from http://forum.unity3d.com/threads/set-up-ik-in-editor.332035/
This script allows to use Mecanim inverse kinematics in edit mode.
Samples of interactive tools in Unity editor
The pathway represented by List of Vector3 points and can be edited as if each point would be separate GameObject with it's own transform handler/
Custom radial grid with sector subdivision and snapping for children.
The example of laying out most common GUI elements in Unity editor.
Custom property drawer
This example shows how to make customization for rendering field of exact type in any MonoBehaviour. In this case it would be bit mask picker (like unity layers) where you can mark channels on different objects and then check if any two of them crosses or not. You can see the application of this property drawer in the following sample "08-CustomInspector/AnimationSample"
In this sample you will able to control state of the children list filtered by bit mask. I. e. you set "channel" values for child and for controller and if controller and child have at least one same channel, the controller would handle such child. The custom inspector includes a button which would create child with all controller channels selected. This sample also involves custom property field drawer and OnValidate().
It's internal unity utility class which allows to change order of list elements in the inspector by dragging and dropping. Details http://va.lent.in/unity-make-your-lists-functional-with-reorderablelist/ This sample shows how to draw default inspector for list's elements.