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
Found it.
In Unity 5, they added EditorApplication.MarkSceneDirty().
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
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().
@SilentSin
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().
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]
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