Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 Jun 22
sparklines
Close Help
  • 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
0
Question by Elenesski · Aug 12, 2015 at 05:22 PM · meshruntimesaveload

How do I save/restore a procedurally generated mesh in runtime in Unity 5?

I am writing a game in which I need to procedurally generate a mesh. The AssetDatabase class is available to me if this mesh is generated in the Editor and save it into the assets folder, but the meshes I am working with are specific to a specific game instance, and would be stored in the user's save game directory. How do I save the generated mesh? I don't need to save the mesh in any exchangeable format, just something Unity can read/write.

According to most posts, I'd create some kind of serializer but has Unity 5 introduced any new capabilities?

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by Cherno · Aug 12, 2015 at 06:00 PM

The easiest is most reliable way is to not serialize the mesh itself, but rather the data that is used be the mesh generating algorithm to generate it. This could be, for example, a 2-dimensional int array for a tile-based mesh. The array is flattened so it can be serialized, and then serialized and saved to a file. To load it, the file is loaded, deserialized, and then the array is made multidimensional again and the resulting 2-dimensional array is then used by then mesh generator to create teh mesh anew.

Here's one possible asset to save and load data:

SerializeHelper

Comment
Add comment · Show 2 · 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 Elenesski · Aug 12, 2015 at 06:26 PM 0
Share

ah, so that the saved meshes are in an alternate format and aren't coupled to a mesh format that has the potential to change as Unity evolves.

avatar image Cherno · Aug 12, 2015 at 07:57 PM 0
Share

Yes. As long as the data that is serialized can be read by the mesh genereration script, it can be sued with any Unity version.

avatar image
0

Answer by Rs · Nov 18, 2017 at 11:26 AM

As in THIS article the trick is to create a serializable mesh class that holds all mesh parts into serializable values and as it's System.Serializable it can be serialized and written to a binary file using a BinaryFormatter.

Comment
Add comment · Show 2 · 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 Bunny83 · Nov 18, 2017 at 01:40 PM 0
Share

This article you've linked has several serious problems. First of all it does not cache any of the $$anonymous$$eshs vertex attribute arrays. This will creates tons of garbage. Just to put that into perspective: If the $$anonymous$$esh has 1000 vertices, just the vertices loop alone will allocate 3000 times the vertices array. Since the array contains 1000 Vector3 values (1000 3 4 bytes == 12000 bytes). You will have 1000 times 12$$anonymous$$B. That's 12$$anonymous$$B of garbage. The two uv array loops will do the same each. The normals loop is even worse since it's 4000 times the array so 16$$anonymous$$B


Another thing is it doesn't process all mesh vertex attributes. There are in total 9 vertex attributes: boneWeights, colors, normals, tangents, uv, uv2, uv3, uv4 and vertices but the code only saves 5 of them


In addition to completely restore a $$anonymous$$esh you may want to not read the triangles array but read the individual submeshes if the mesh has submeshes (sub$$anonymous$$eshCount). Otherwise you will loose the material assignment to the individual submeshes. If it's a skinned mesh you also need to store the "bindposes" matrix array.


Finally it's better to store the vertex colors as Color32. This reduces the space for the colors by a factor of 4 (1x Color has 16bytes, 1x Color32 has 4bytes)

avatar image Bunny83 · Nov 18, 2017 at 01:53 PM 0
Share

Note that the garbage generation grows O(n²) the way he does this. So while you generate 12$$anonymous$$B for 1000 vertices you will generate 1.2GB for an array with 10000 vertices. At 65$$anonymous$$ vertices the vertices array alone will generate 150GB+ of garbage.


Even all those allocated arrays go out of scope / extent the next loop iteration it's very possible that the GC can't cope with the pressure and you get an OutOf$$anonymous$$emory exception. Even if you don't it would run extremely slow.

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.

Follow this Question

Answers Answers and Comments

27 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Is it possible to save loaded textures and reuse after close app? 1 Answer

Android download and save "Resources.assets" 0 Answers

Best way to save procedural world/mesh? 0 Answers

Serialize GameObject 1 Answer

Save/ Load in Editor created meshes 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