- Home /
How to prevent editor to set the scene dirty if a script changes a mesh (@ExecuteInEditMode)
I've got a script that manages a custom mesh and needs to run in the editor. It works like intended, but changing the mesh (say by calling "GetComponent().sharedMesh.Clear()") causes the scene to set it's state to dirty (the * in the editor's titlebar shows up permanently and indicates a changed scene).
As I generate the mesh by code there's no reason for the user to save those changes, so is there a way to disable this behavior?
This must be possible somehow as the buildin ParticleSystem both runs in the editor and changes it's mesh without setting the scene dirty.
Update: I tried HideFlags.DontSave but without luck. As soon as I change some vertices the scene went dirty.
Update 2: Like tranquility suggested, I've tried the following:
[ExecuteInEditMode] public class Meshtest : MonoBehaviour { Mesh mMesh;
void OnEnable () {
mMesh = new Mesh(); // create a private mesh
}
void Update()
{
mMesh.Clear(); // scene will turn to dirty state
}
}
but even without any MeshFilter and MeshRenderer present in the scene it will turn to changed state immediately.
Answer by Quazistax · May 05, 2011 at 08:13 AM
Changes to "sharedMesh" are persistent, for example, if you change it in play mode, changes will be kept after exiting play mode, scene will be changed. If you change MeshFilter's "mesh" property, changes will disappear after exiting play mode. Unfortunately, seems like you can not change "mesh" property in edit mode.
What you could do is creating one or more Mesh you want to show and use Graphics.DrawMesh. If you want to keep "sharedMesh" intact as part of your game object, but want to hide it, disable MeshRenderer component.
EDIT: I set hideFlags and it worked - private mesh changes don't make scene dirty.
void OnEnable ()
{
mMesh = new Mesh(); // create a private mesh
mMesh.hideFlags = HideFlags.HideAndDontSave;
}
You will get this message, probably when saving scene: "Cleaning up leaked objects in scene since no game object, component or manager is referencing them. Mesh has been leaked # times." But it is harmless.
However, if you find a way to avoid that message, it would be nice to know how.
EDIT: Move the initialization code from OnEnable to Awake function, I don't get "leaked objects" message after that.
Thanks for your answer. I didn't knew Graphics.Draw$$anonymous$$esh until now, but unfortunately it seems like the problem is the mesh. Even if I don't touch shared$$anonymous$$esh and use a private mesh ins$$anonymous$$d, the scene changes by the first call to mesh.clear(). I've added the code to my question above. Any ideas?
This seems to work, thank you very much! The problem with Awake is that it won't be called in the editor after recompiling while OnEnable does, so I placed all my initialization code in OnEnable. But that message is not a big issue. If I find out anything new about this message I'll let you know.
I would put object creation in Awake and initialization in OnEnable. Because m$$anonymous$$esh object survives recompilation, so in OnEnable only thing you have to do is m$$anonymous$$esh.Clear() (if you want to start with clear mesh).
Or... I just tried this... You can have all in OnEnable, but have to do a cleanup in OnDisable. (Object.DestroyImmediate(m$$anonymous$$esh);) And "leaked objects" message is not appearing.