- Home /
Change text in text component from editor script
Hi
I am currently working on a custom button script and I want to be able to change the text of the child text object in the editor without going to the text child. So I did a custom editor script that would allow me to do this.
This is my current code for doing this.
script.textChild.text = EditorGUILayout.TextField("Text", script.textChild.text);
(textChild is basically just a Text variable in the base script.) Now this works but you need to save the scene everytime you want to update the text. I would like it to update as you type. How can I do such a thing?
Thanks in advance
Answer by Baste · Sep 05, 2015 at 06:36 PM
The text only changes if Unity notices that it has changed. In an editor script, Unity auto-detects that for whatever the Editor is targeting (your script variable) if you use EditorGUILayout, but changes to other objects are not noticed. You'll have to set those dirty manually.
Luckily, that's easy. Just check if the GUI was changed, and set the textChild dirty if that's the case:
script.textChild.text = EditorGUILayout.TextField("Text", script.textChild.text);
if (GUI.changed) {
EditorUtility.SetDirty(script.textChild);
}
Just tested it: the text will update in the scene if you add the SetDirty line.
Your answer
Follow this Question
Related Questions
Rotating a EditorGUI.LabelField (or making vertical text) 1 Answer
Text missing in Editor; huge amounts of bugs and glitches 0 Answers
Custom editor for Text / Button for externalising texts 1 Answer
Workflow for importing text files on a PC and exporting to mobile 0 Answers
Display the set up key for a button 2 Answers