- Home /
TextEditor in editor gui?
I've been trying to get access to the TextEditor for a TextArea object in an Editor window. I've used the same patterns I see in other examples and I think they work only in game gui, not editor gui. For example this works:
Rect bounds = new Rect(10, 20, Screen.width - 10, Screen.height - 20);
stringToEdit = GUI.TextArea(bounds, stringToEdit);
int controlID = GUIUtility.GetControlID(bounds.GetHashCode(), FocusType.Keyboard);
TextEditor editor = (TextEditor)GUIUtility.GetStateObject(typeof(TextEditor), controlID -1);
but this does not:
the_text= EditorGUILayout.TextArea(the_text, GUILayout.ExpandHeight(true));
int controlID = EditorGUIUtility.GetControlID(b.GetHashCode(), FocusType.Keyboard);
TextEditor editor = (TextEditor)EditorGUIUtility.GetStateObject(typeof(TextEditor), controlID -1);
Am I right in thinking TextEditor can't be accessed in the editor? Or am I missing something obvious?
Answer by theodox · Mar 07, 2013 at 09:34 PM
For future reference: I've verified that the TextEditor is available when using GUI.Textarea but not when using EditorGUI.TextArea.
how odd. Thanks, I could not think of checking that...
Did you find a solution to get the TextEditor for an EditorGUI TextField by any chance? Using regular GUI TextField in the editor prevents nice things like cut/copy/paste :(
Answer by Demigiant · May 27, 2017 at 06:10 PM
Found a way to grab the TextEditor for an EditorGUI.TextArea/Field (requires Reflection):
TextEditor tEditor = typeof(EditorGUI)
.GetField("activeEditor", BindingFlags.Static | BindingFlags.NonPublic)
.GetValue(null) as TextEditor;
How do you actually use this? I got my TextArea in OnGui that draws properly but I'm not sure I get how to apply this.
The TextEditor is what lies under the TextArea, and allows you to do more advanced text manipulation. You don't need it at all if you just want to use a normal TextArea (unless I misunderstood your comment :P)
I think you're spot on. If I understand correctly, it allows grabbing the TextEditor instance of a text area which in turn provides access to a series of events and values needed to build a more complex text editor than a simple text box.
ie.: Is text selected, the content of the selection, etc...
Thanks so much! I got this to work on an EditorGUILayout.PropertyField as well. Don't forget to perform a null check since this texteditor will refer to any textfield/propertyfield currently edited. If none is being edited this 'tEditor' is null.
Could any of you guys answer my question as well? I just don't understand how this all works. I'm not that deep into editor scripting.