Saving procedural meshes in Prefabs

Hello,

I’m working on game with lots of prefabs as building blocks, and I’m generating the meshes inside procedurally - imagine each prefab having multiple mesh-generating-children.
However if I create a new Mesh and assign it to my Mesh Filter and then save the Prefab - upon opening it again the Mesh is gone - it doesn’t get serialised.

As far as I can tell this is different to how Scenes work - where I can procedurally generate a mesh, and it gets saved along with the Scene - which is perfect (at least for my use-case).

Is there any reason why these two are different? Why local meshes are serialised in Scenes but not Prefabs?
Is it just a bug, or am I perhaps doing something wrong?

Thanks!

p.s
I currently sort-of get around this by creating a mesh asset and adding it to my prefab as a sub-asset, however that’s quite a convoluted workflow and it raises other issues:

  • You cannot delete sub-assets. If I changed the content of the prefab and now need to generate new meshes, the old ones are still there.
  • When duplicating prefabs they end up sharing the mesh sub-assets (which is correct in a normal prefab-mesh relationship, but not in this case) - meaning when I’m working on one prefab the other one gets affected.

I could only solve the above by having a post-save scripts that validates all the prefabs and their meshes but as you can imagine it increases project save time and it’s just not entirely foolproof.

1 Like

Scene saving and Prefab saving works in different ways. If a scene has a reference to an Asset type object which is not serialized on disk, that Asset type object gets saved into the scene. This is not the case for Prefabs. You’ll need to save the Asset type objects (like meshes) to disk yourself, either as separate Assets, or as part of the Prefab Asset using AddObjectToAsset.

Thanks for the reply!

As I’ve mentioned I am using AddObjectToAsset - however that is not really working as neatly as Scenes, and I’ve listed a few of the issues above.
Duplicating the prefab makes the new prefab use the source mesh, removing components that use the generated mesh doesn’t clean up the meshes, the inability to rename/delete asset objects - these are all some of the reasons why doing this myself is not really viable I think.

I guess if this is by design, my questions would be

  • Is there any way to do save it into the Prefab as Unity does with Scenes?
  • If not - is there a technical reason why this wouldn’t be possible? I’m just wondering whether this can be a feature request, as I think it would simplify the workflow by orders of magnitude.

Searching for answers on this it seems like this “issue” has been around for a while and the only solutions I found were either what I’m doing (sub-assets) or serialising the mesh info (verts,tris,uvs) and regenerating at runtime.
Neither are good enough I feel - and if Unity could treat Prefab assets as it does Scenes’ - that could be amazing.

1 Like

Sure it would be possible and we’ve done some experiments with it internally as you can see in this silly video. But we haven’t had resources to finish and ship the feature yet, unfortunately.

One of the things we didn’t find a solution to yet, for example, if how to handle things if you duplicate one of those Prefabs with sub-Assets in it.

Haha ok well that’s good to know, at least I’m not scratching my head for nought :slight_smile:

I’m currently testing the relevant components inside the new prefab, and invalidate their mesh if it’s part of a different prefab. I also do a similar thing when deleting components - I find any sub-assets that are not referenced, and delete them.

Though as I said that increases save time and it all feels a bit hacky, but it sounds like that’s my only option at the moment.

Is there a tracker for this work anywhere, something I can upvote? Or is it just way down on the priority list?

1 Like

Did you ever come up with an ideal solution to this?

Unfortunately not - left with sub-assets and lots of custom editor code.

Dang. You’d think being able to serialize a mesh to a prefab asset like how scene assets do would be built-in functionality. There’s probably a good reason for why though, hopefully.

This approach just breaks the Prefab’s strip function with potential data-loss. Please check this thread: https://forum.unity.com/threads/probuilder-convert-to-mesh.1027114/#post-8749089

I’ve tried doing this myself using AddObjectToAsset, but this causes unity to crash when I’m in the prefab editor. Outside it works fine.

You can use UnityEditor.SceneManagement.PrefabStageUtility.GetCurrentPrefabStage() to check if you are in prefab mode.

The issue is, that I want to be able to save assets to the prefab while in prefab mode, since materials and meshes can be generated within.

Since you seem to be addressing me directly (quoting a post from 2020) I should say I haven’t been an Unity employee since 2020, so you’ll need to get someone else’s attention. I’m unsubscribing from the thread now. Good luck.