- Home /
The question is answered & thread keeps being targeted by spammers
Unity API for checking if an object was modified
Is there any API that Unity provides for determining whether an object was edited (e.g: some value changed in its inspector) ?
There is a method called EditorUtility.SetDirty that accepts an Object and sets it as dirty.
Is there a method for determining if a given object is currently dirty?
Answer by vexe · Sep 01, 2014 at 04:09 PM
If you want this at edit-time and you like hacks, this looks like it should work (not tested)
bool isDirty = (bool) typeof(Editor).GetProperty("isInspectorDirty", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(myEditor, null);
Actually, i am not sure this will not work, but it's close. This checks for the Editor class, whether the inspector is dirty. Usually, you create classes derived from Editor for different asset types. You would then have to get the currently active editor object, and then check for it whether it's dirty or not. This seems to be what the ActiveEditorTracker class offers (http://docs-jp.unity3d.com/Documentation/ScriptReference/ActiveEditorTracker.html). I will give that a check.
This will check if the editor/inspector for a certain object is dirty or not (thus the object is modified or not) - What is your actual use-case I might help you better given more detail
This only seems to work very intermittently. In testing within OnInspectorGUI and checking for the isInspectorDirty state on the target Editor, most frames return false. Occasionally a frame will return true for isInspectorDirty, specifically when the object is first selected, but the vast majority of updates return false after the initial selection.
Answer by BMayne · Sep 01, 2014 at 04:07 PM
Hey There,
You can use OnValidate(). That functions gets called whenever something changes in the inspector.
This will not tell you if something changed in the scene. You can hook into the AssetModificationProcessor to see when something is going to be saved if it was dirty.
I am not sure if there is a direct way to check if an Object is dirty.
Regards,
Thanks. I know Asset$$anonymous$$odificationProcessor, but that would only work for assets that are being saved. Any modification that is done inside the editor is not immediately saved to disk. OnValidate is nice but i want to listen to ANY object that is being modified, not specifically to ones that i have control over.
You will have to break into using Reflection like vexe said then.
Answer by JasonC_ · Mar 21 at 01:52 AM
EditorUtility.IsDirty
https://docs.unity3d.com/ScriptReference/EditorUtility.IsDirty.html
Follow this Question
Related Questions
Saving class fields in the Asset 0 Answers
Updating object on inspector value changes in editor 1 Answer
AutoComplete Text in TextField 2 Answers
Help with Missing Monobehaviours and Asset Serialization? 0 Answers
How do I solve a BuildAssetBundles Compilation Error that only occurs in the Editor? 1 Answer