- Home /
Is EditorUtility.SetDirty restricted to prefabs or inspected GameObject?
We're working on an Unity tool and we need to manage the resources (texture atlases) used by the Game Objects (sprites) we create.
So, our Game Objects are executed in the Unity editor ( [ExecuteInEditMode] attribute) and perform some resource checks in the Update method. The checks could lead to rebuild some data in the sprites if something is outdated (for example, if a texture atlas has changed, the GameObject may need to update its sprite mesh UVs). After rebuilding the data, we use EditorUtility.SetDirty on the sprite component to save the changes.
We discovered that SetDirty seems not to work in our case: the scene is not marked as dirty and we can exit Unity without asking to save. When reloading the scene, the components rebuild their data (which are not saved, and so on). The most important data to save is just an ID string (which timestamps the last atlas regeneration: if the sprite and the atlas have the same, everything is OK, otherwise, we need to rebuild the sprite).
So we're wondering if SetDirty works only in a particular context/use? Does anyone faced the same issue?
Thanks.
Unity 3.5.6f4 / Mac OS X 10.6.8
I know it's been two years, but I just came across this problem, so bump!
i know its been a long time now,
but this is still persistent in unity5
consider the following script in a new scene on the directional light gameobject:
[ExecuteInEdit$$anonymous$$ode]
public class SetDirty : $$anonymous$$onoBehaviour {
void OnRenderObject()
{
EditorUtility.SetDirty(gameObject);
Debug.Log("changed");
}
}
changed gets logged many times when you fly through the scene.
but you can still just exit the scene fine without being prompted to save it
is there anyone who has been able to force a scene to save? even though i didnt actually change anything?
I'm looking into this same problem. Pressing Ctrl + S does save the changed object, but the editor doesn't actually prompt you to do so when you close it or change scenes.
Answer by SilentSin · Apr 25, 2015 at 10:53 AM
Found it.
In Unity 5, they added EditorApplication.MarkSceneDirty().
Interesting... From the description of $$anonymous$$arkSceneDirty() it sounds like they expect you to use the "undo" system, and won't tell the user about changes (e.g. by adding an asterisk to the title bar) otherwise. I'm not sure what the value of having "secret" changes is, but this at least fixes things. (Tested with Unity 5.1.3.)
It was moved to: UnityEditor.Scene$$anonymous$$anagement.EditorScene$$anonymous$$anager.$$anonymous$$arkAllScenesDirty();
Answer by BlackManta · Mar 15, 2016 at 08:50 PM
EditorSceneApplication is still in Unity 5 but you have to import it.
using UnityEditor.SceneManagement;
then mark all or an individual scene as dirty.
Absolutely necessary if you are going to have a script dynamically create a lot of objects in your scene. (I am not sure why it doesn't mark it as dirty by default). I had a script that helps me make a tile based map. Everything worked fine if I added one tile at a time. However, when I did a recursive fill the scene would not save, even though I manually asked it to save. (It would show up in the editor though, at least until I opened another scene).
Cheers, BM
Answer by ecesis_llc · Feb 16, 2016 at 12:39 AM
I came across this topic while trying to resolve a similar issue. If you are making edits to non-unity objects, like custom structs, and the changes are not reflected you can use Repaint().
EditorApplication.MarkSceneDirty is now obsolete in 5.3. The message states to use EditorSceneManager... which doesn't exist in my version of unity. So the solution that seems to work for now is Repaint().
Try UnityEditor.Scene$$anonymous$$anagement.EditorScene$$anonymous$$anager.$$anonymous$$arkAllScenesDirty();
EditorSceneApplication is still in Unity 5 but you have to import it.
using UnityEditor.Scene$$anonymous$$anagement;
then mark all or an individual scene as dirty.
Absolutely necessary if you are going to have a script dynamically create a lot of objects in your scene. (I am not sure why it doesn't mark it as dirty by default). I had a script that helps me make a tile based map. Everything worked fine if I added one tile at a time. However, when I did a recursive fill the scene would not save, even though I manually asked it to save. (It would show up in the editor though, at least until I opened another scene).
Cheers, B$$anonymous$$
Answer by BlackManta · Mar 15, 2016 at 08:50 PM
I have had a similar problem when a custom editor script dynamically creates (instantiates objects) it is possible to have the dirty bit not get set for the scene.
The odd part is if you create one object at a time, it doesn't seem to have this problem. But when you are, say doing a recursive fill. Then the changes will not be saved... even if you manually save the scene.
My solution: using UnityEditor.SceneManagement;
at the top of your code. Then use when you have made your changes via script:
EditorSceneManager.MarkAllScenesDirty(); //or mark individual scenes as dirty
To the part regarding resources, I am not sure if setDirty works that way. From my understanding it only marks whether or not the scene has been altered. So modifying a script, or component that an object uses may not have the desired affect.
Cheers, BM
[Using Unity 5]
Answer by Careless · May 03, 2016 at 03:28 AM
You can also try to
if (GUI.changed)
{
Debug.Log("Changed");
EditorUtility.SetDirty(_script);
}
in your Editor script, (_script) here is the target that references to your monobehaviour script