- Home /
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. This 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 this 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
How are you editing the changes? If you're using SerializedProperties, you need to use their version of SetDirty (Apply$$anonymous$$odifiedProperties).
If not, make sure that you're calling SetDirty on the $$anonymous$$eshRenderer component itself.
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 $$anonymous$$eshRenderer
mr.gameObject.isStatic = true;
mr.lightmapScaleOffset = scaleOffset;
mr.lightmapIndex = lightmapIndex;
// Throw any dirty flags into the mix that I can find
PrefabUtility.RecordPrefabInstanceProperty$$anonymous$$odifications(go);
EditorUtility.SetDirty(go);
EditorUtility.SetDirty(mr);
As per the code, I've tried setting the dirty flag on the GameObject and the $$anonymous$$eshRenderer 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
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
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:
EditorScene$$anonymous$$anager.$$anonymous$$arkSceneDirty(EditorScene$$anonymous$$anager.GetActiveScene());
Try that.
Hi Baste,
Thanks for the suggestion! I upgraded to Unity 5.3 to get access to EditorScene$$anonymous$$anager but sadly it still doesn't seem to save the $$anonymous$$eshRenderer 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 shared$$anonymous$$esh copying over the data, create an actual asset from that and then instantiate that into the scene, the $$anonymous$$eshRenderer lightmap values still get reset. :/ $$anonymous$$aybe it's time for a new approach...
Thanks again for the suggestion, much appreciated!
Your answer
Follow this Question
Related Questions
Unity Editor SerializedProperty how to assign to array with larger data directly. 1 Answer
Binary Serialization (In Editor) - Path Access denied / File not found 0 Answers
How to save custom data types 1 Answer
How are unity references serialized? (instanceID related) 1 Answer
Issues when using Unity Serializer 1 Answer