• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by Jake-L · Feb 13, 2011 at 04:17 PM · editormeshmeshfilterexecuteineditmode

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.

Comment
Joshua

People who like this

1 Show 0
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
Best Answer

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.

Comment
Jake-L
nicloay
ddf

People who like this

3 Show 11 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Jake-L · May 06, 2011 at 11:03 AM 0
Share

Thanks for your answer. I didn't knew Graphics.DrawMesh until now, but unfortunately it seems like the problem is the mesh. Even if I don't touch sharedMesh and use a private mesh instead, the scene changes by the first call to mesh.clear(). I've added the code to my question above. Any ideas?

avatar image Quazistax · May 06, 2011 at 10:41 PM 0
Share

I tried few things and... I updated answer.

avatar image Jake-L · May 07, 2011 at 07:39 AM 0
Share

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.

avatar image Quazistax · May 07, 2011 at 08:45 AM 0
Share

I would put object creation in Awake and initialization in OnEnable. Because mMesh object survives recompilation, so in OnEnable only thing you have to do is mMesh.Clear() (if you want to start with clear mesh).

avatar image Quazistax · May 07, 2011 at 08:56 AM 0
Share

Or... I just tried this... You can have all in OnEnable, but have to do a cleanup in OnDisable. (Object.DestroyImmediate(mMesh);) And "leaked objects" message is not appearing.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

1 Person is following this question.

avatar image

Related Questions

Weird issue with mesh.Clear() 1 Answer

Is there any way to reset the default meshes? 3 Answers

Mesh memory leak error 0 Answers

How can I modify the mesh in OnPostprocessModel? 2 Answers

Prevent Unity from serializing MeshFilter.mesh 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges