Saving a volume created during runtime

Siedge posted this 15 February 2016

I am generating and populating a mesh through code. Would be possible to save it as a prefab with the data stored? (I tried but it doesn't save the mesh or the voxel data)

Thanks for the help in advance!

Order By: Standard | Newest | Votes
GarethIW posted this 15 February 2016

You can use Frame.ToCompressedByteArray() and Frame.FromCompressedByteArray() to save and load the contents of a frame's voxel array at runtime.

If you wanted to get that data back into a volume at edit time... that would be more difficult, but possible. You would have to do the following:

  1. Save the byte array to a file at runtime
  2. Extend the volume editor inspector (VolumeInspector.cs) to provide a button to open your saved file (you can use a button combined with a file dialog to do this, look at the editor windows for importing Magica/Cubicle for an idea), read the byte array and then pass it to Frame.FromCompressedByteArray.
  3. Re-create all chunks (Volume.CreateChunks())

Your edit-time volume would have to the same dimensions as your runtime volume.

Edit: to answer more directly; I don't know of any way to alter the contents of a prefab at runtime so that it is persisted when going back into edit-time. That's more of a general Unity issue though.

Last edited 15 February 2016

Siedge posted this 15 February 2016

You answered my question perfectly. Thank you for the continued help you have provided!

Our 127 members have posted 466 times in 112 discussions