• 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 stu_chineseroom · Jan 13, 2016 at 09:35 AM · unity 5serializationlightmappingsavingmeshrenderer

Saving changes to MeshRenderer at edit time

Hi,

I'm changing a few properties of the MeshRenderer component on my objects at edit time (e.g. lightmapIndex, lightmapScaleOffset). I also update the LightmapSettings structure since I'm setting up some custom lightmaps. T$$anonymous$$s all seems to work fine, however when I quit and reload the level the changes are gone. If I were making edits to the meshes themselves I'd look at editing the sharedMesh or creating a new asset, but since t$$anonymous$$s is the MeshRenderer components and a general engine structure, I'm not sure how to best proceed.

For what it's worth, I've tried using EditorUtility.SetDirty(), PrefabUtility.RecordPrefabInstancePropertyModifications() etc to make sure I'm notifying the editor of my changes. I can view the intended values in the Lighting/Object tab under Baked Lightmap too.

Any help would be much appreciated. Thanks, Stu

Comment
Fattie
E-Cone

People who like this

2 Show 7
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 Fattie · Jan 13, 2016 at 01:01 PM 0
Share

good question

avatar image Baste · Jan 13, 2016 at 01:05 PM 0
Share

How are you editing the changes? If you're using SerializedProperties, you need to use their version of SetDirty (ApplyModifiedProperties).

If not, make sure that you're calling SetDirty on the MeshRenderer component itself.

avatar image stu_chineseroom · Jan 13, 2016 at 06:51 PM 0
Share

Hi @Baste, thanks for replying.

I'm currently doing this essentially:

 GameObject go = GameObject.Find(meshName);
 Renderer mr = go.GetComponent<Renderer>();
 
 // Update the values on the MeshRenderer
 mr.gameObject.isStatic = true;
 mr.lightmapScaleOffset = scaleOffset;
 mr.lightmapIndex = lightmapIndex;
 
 // Throw any dirty flags into the mix that I can find
 PrefabUtility.RecordPrefabInstancePropertyModifications(go);
 EditorUtility.SetDirty(go);
 EditorUtility.SetDirty(mr);

As per the code, I've tried setting the dirty flag on the GameObject and the MeshRenderer component and have tried many combinations thereof. I don't think I can set a dirty flag on the LightmapSettings structure itself, so I'm not sure what I can do to coax that to save either.

Thanks for the help, Stu

avatar image stu_chineseroom · Jan 18, 2016 at 02:49 PM 0
Share

I've additionally tried cloning the objects, setting the new params on the clones and then destroying the originals. Sadly this doesn't work either. I thought perhaps the values might get pulled from the prefab so I use PrefabUtility.DisconnectPrefabInstance() after cloning but to no avail.

I've also tried updating the components on the prefabs themselves, but that didn't seem to make any difference (I changed the hideFlags to allow editing first, just in case).

I'm kinda out of ideas now other than setting the values during Awake() at runtime to make sure they persist during gameplay at least. Any other thoughts?

cheers, stu

avatar image Baste stu_chineseroom · Jan 19, 2016 at 12:06 AM 0
Share

Okay, I've been noticing similar things too, and just replied to a thread on the forums.

At one point, the SetDirty method stopped setting the scene dirty. I'm not sure if it's a bug or a horribly bad idea, but in the mean time, you'll have to manually set the scene dirty when you do a change.

This seems to be done through the very wordy:

 EditorSceneManager.MarkSceneDirty(EditorSceneManager.GetActiveScene());

Try that.

avatar image stu_chineseroom Baste · Jan 19, 2016 at 11:47 AM 0
Share

Hi Baste,

Thanks for the suggestion! I upgraded to Unity 5.3 to get access to EditorSceneManager but sadly it still doesn't seem to save the MeshRenderer changes I need. I also tried using Undo.RecordObject from the forum thread as I'd not considered that before but it still doesn't make the changes stick. Perhaps the settings are saved, but the engine is overwriting them with the defaults on-play or reload (e.g. offsetScale is reset to <0, 0, 1, 1>, lightmapIndex to -1).

I'm now beginning to wonder if I'm doing something fundamentally not possible by injecting these lighting values. If I clone an object, create a new sharedMesh copying over the data, create an actual asset from that and then instantiate that into the scene, the MeshRenderer lightmap values still get reset. :/ Maybe it's time for a new approach...

Thanks again for the suggestion, much appreciated!

Show more comments

0 Replies

· Add your reply
  • Sort: 

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I set the number of CPU threads for lightmapping in Unity 5? 0 Answers

Saving players progress in ScriptableObject asset 1 Answer

Serializing Dictionary with Vector3 keys 1 Answer

Serialization issue after updating Unity from 2018.2.1 to 2019.2.6 1 Answer

How to replicate 1 gameObjects face with a material 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