My custom editor lets the user type in a text field ...
How can I tell if the user pressed enter on the string?
This detects when the user presses enter ...
But this detects enter when any control (or no control) in the editor has focus, not just my text field.
This worked in Unity 2.6.1 ...
... but it doesn't in Unity 3.1. Seems like the text field has lost focus by the time the keyboard event comes through. (KeyDown doesn't work either.)
Here is the workaround I've come up with, but it seems awfully convoluted for what should be a simple thing. Something cleaner would be appreciated.
asked Jan 08 '11 at 12:37 AM
Well I am not sure whether you still require an alternative solution to your code above but this is how I do it.
Because Unity 3.x ships with the newer version of Mono, we have a new tool at our disposal called the extension method.
In a static class you create a generic extension method that evaluates the input like so.
Then in the OnInspectorGUI() method you do the following for each field that you want to evaluate (changing the target class name to yours where required):
The reason I like doing it this way is that all the logic needed to test for any type of key press is located in the one location and can be reused anywhere within your project and I think it reads a little nicer.
It should be able to be used for any type of field type however I have only used it for int and string types.
Anyhow I hope it helps. :)