- Home /
Updating object on inspector value changes in editor
I have a game object that displays text information (some number). The number is controlled from a script that is attached to that game object.
I'd like the changes to reflect in the editor as i change this number.
So far, i got away using this:
private void OnValidate()
{
UpdateText();
}
Whenever i change a value, the change will be reflected immediately.
There are 2 issues with this approach:
The OnValidate callback should be used for validating data. This is kind of a hack to use that for updating other things.
Whenever i load the game object (e.g: from scripting) and not by dragging it into the scene, it will not update its text using OnValidate (since no "change" was done to the data)
Is there any better way to achieve this? I know about [ExecuteInEditMode]
, but this is "dirty" - forcing me to add another component to my game object, one that i don't need when releasing the game.
Haven't really understood the scenario your describing. You want changes made in the editor to be reflected by the text(attached to the game object) which is in the scene, while in edit mode? Are you creating the game object in the scene while your in edit mode? As you don't want to use [ExecuteInEdit$$anonymous$$ode] the only suggestion I can give is you need to poll the data somehow? You might wanna try and see if EditorApplication.update can be of any help. (Aroyo Nimrod)
Answer by smoggach · Aug 13, 2014 at 01:54 PM
Perhaps there's a way to get what you want by implementing the getter/setter of your property.
In any case you must have some sort of way to change the value during runtime so the key will be to do it in the same manner in the editor.
You can also try making a CustomPropertyDrawer for that property. The code will only be compiled into the editor and you can use it to update your text when IT is invalidated.
What is the difference between a CustomPropertyDrawer and a CustomEditor ?