- Home /
"When a value is changed..."
I want my script to do something when one of its Serializable varabies has been changed through the editor. (IE: Can't rely on a Setter function.)
Is there a simple way to do this?
Answer by Dave 11 · May 10, 2011 at 08:37 PM
I'd set another variable with the same value and check if they're equal constantly. If not, make your action happen and set the variable to the new value.
Now that I think about it, my scripts already inherit from $$anonymous$$onobehaviour. I hate to constantly call Updates while in Edit $$anonymous$$ode, especially since the value is a fairly bulky class. I already have the ability to update it when the value is changed through the Setter, but while Serializing it, the update seems like the only solution.
$$anonymous$$aybe I'll make it conditional or somethin'. That shouldn't be too big a problem.
Answer by yoyo · May 10, 2011 at 09:39 PM
You could create a custom editor for the type. The editor would be active any time the properties of your object are being changed, so your editor code could check for changes, rather than putting it in the runtime Update. You can use Editor.DrawDefaultInspector to avoid re-implementing the editor functionality.
Alternatively you could tag your script to ExecuteInEditMode, but and use the C# pre-processor to remove the editor update code from your final build, like so ...
void Update()
{
#if UNITY_EDITOR
if (val != savedVal)
{
DoEditorStuff();
}
#endif
}
(Nope, doesn't work in javascript, sorry ...)
is there a way to do this without having to heck if a value has changed in update, kinda seems like a waste.
Your answer
Follow this Question
Related Questions
Declaring animations on a script in the editor 1 Answer
Prefab not changing variable's values 2 Answers
layout serializable objects in unity 1 Answer
How to add objects in Inspector 1 Answer
How to find a variable with a string? 2 Answers