Custom editors: input text fileds

From Unify Community Wiki
Jump to: navigation, search

Author: Serge Billault


The following tips are not meant to be absolutes but rather guidelines for Unity users that want to produce well performing code.

Scripting

When making custom editors the need to detect changes made to the object being edited quickly emerge in order to propose to save changes to the user, for exemple.
If dealing with the GUI family controls to edit your data fields, you got no callback system to track changes made to a variable being edited. Thus, you have to perform
yourself a comparison in order to mark your edited object as dirty.

Input texts are among the most misused controls due to the nature of the data type it deals with: string.
When being sent as a contractor in some development studios you will notice that some fail to display custom editors / inspectors under 0.5ms even with only a few thousand entries.
Part of it is due to the absence of occlusion of non visible datas (performance is always quantity independant in a well written custom editor), but part of it is tied to the way they use
some GUI controls, and especially the input text fields.

A string is an immutable object passed by reference, since it's a class. Any modification entered in the input field will cause the string reference returned by the control to differ from
the string reference passed as an argument since the modification has to be stored into a new imutable string. You can take advantage of that and, to detect a change, rather than
performing costly string comparison, simply check the references of the strings entered and received.

Exemple

We can see below that the changes are correctly detected without having to make string comparisons.

Input text field change detection.jpg

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox