Custom editors/menus tips

We can set custom hotkeys for MenuItems. For example, we can make the K key trigger our Menu item method, by defining the MenuItem attribute as follows:

[MenuItem("My Menu/Menu Item _k")]

We can also include modifier keys such as Ctrl (Cmd), Shift, and Alt using the %, #, and & characters, respectively.

MenuItems also have two overloads, which allows us to set two additional parameters: a Boolean that determines whether the menu item requires a validation method, and an integer that determines the menu item's priority in the hierarchy.

Check the documentation for MenuItems for a complete list of available hotkey modifiers, special keys, and how to create validation methods:

http://docs.unity3d.com/ScriptReference/MenuItem.html

It is possible to "ping" an object in the Hierarchy, similar to what happens when we click on a GameObject reference in the Inspector View, by calling EditorGUIUtility.PingObject().

The original implementation of the Editor class, and the way that most people learned how to write Editor scripts, originally involved writing all logic and content drawing in the same class. However, PropertyDrawers are an effective way of delegating Inspector drawing to a different class from the main Editor class. This effectively separates input and validation behavior from display behavior, allowing more fine-tuned control of rendering on a per-field basis and more effective reuse of code. We can even use Property Drawers to override default Unity drawing for built-in objects, such as Vectors and Quaternions.

PropertyDrawers make use of SerializedProperties to accomplish serialization of individual fields, and they should be preferred when writing editor scripts, since they make use of built-in undo, redo, and multiedit functionality. Data validation can be a little problematic, and the best solution is to use OnValidate() calls on setter properties for fields. A session at Unite 2013 by Unity Technologies developer Tim Cooper, which explains the benefits and pitfalls of various serialization and validation approaches in great detail at https://www.youtube.com/watch?v=Ozc_hXzp_KU.

We can add entries to Component context menus and even the context menus of individual fields with the [ContextMenu] and [ContextMenuItem] attributes. This allows an easy way to customize Inspector behavior for our Components without needing to write broad Editor classes or custom Inspectors.

Advanced users may find it useful to store custom data within Unity metadata files through the AssetImporter.userData variable. There are also a multitude of opportunities to make use of Reflection of the Unity codebase. Ryan Hipple's session at Unite 2014 outlines a huge number of neat little hacks and tricks one achieve with Reflection in the Unity Editor:

https://www.youtube.com/watch?v=SyR4OYZpVqQ

An undocumented feature was introduced in Unity v4.5, Reorderable Lists. These allow us to have an Inspector View of a generic List<T>, which can be easily reordered by dragging and dropping the elements around. However, this feature appears to be unfinished, as it requires a custom Editor class to make use of them properly. The following post on Unity Answers explains how to use Reorderable Lists fairly succinctly:

http://answers.unity3d.com/questions/826062/re-orderable-object-lists-in-inspector.html

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.225.72.245