Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
1
Question by MathiasDG · Oct 27, 2015 at 09:31 AM · c#arrayserializationloadingdata

Best way to serialize a big array of a class

What is the best way of serializing a big array of a class, that will be loaded when the game starts?

Right now i'm doing this, storing everything in a scriptable object.

 public class MeshData : ScriptableObject, ISerializationCallbackReceiver
 {
     [SerializeField]
     [HideInInspector]
     internal Tiles[] tiles;
 
     [SerializeField]
     [HideInInspector]
     internal Vertex[] vertices;
 
     [SerializeField]
     [HideInInspector]
     internal Triangle[] triangles;
 
     public void OnBeforeSerialize()
     {
         if (triangles != null && Ready)
         {
             _serializing = true;
             SerializeTriangles();
             _serializing = false;
          }
      }
 
     public void OnAfterDeserialize()
     {
         _serializing = true;
         DeserializeTriangles();
         _serializing = false;
     }
 }

It works as expected, tiles and vertices have only serializable ints and floats and they dont need a custom serialization, but triangles have pointers to vertex and to other triangles, so they need that serialization step. It works.

Only problem is, it takes a few seconds to load every time the game starts. Array sizes are of about 256x256 = 65536 for the tiles, which hold a float, a int and a bool each, and about 500 to 1000 vertices, and 1000 to 2000 triangles. Vertices have only 2 floats each. Triangles carry a lot of data, something like 20 to 30 floats or ints each.

  1. Should i worry about this start loading time? Is it normal? It takes up to 5 seconds.

  2. Is there a better way of storing these other than in a scriptable object?

  3. When using scriptable objects, should they be stored in a prefab? Or is it fine to leave them as a field on a object that is on the hierarchy. (See screenshot, that is all and the only reference there is to the scriptable object. no prefabs created, no nothing. The mesh class has a meshData variable which gets assigned to the MeshData.

alt text

meshdata.jpg (11.9 kB)
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
0

Answer by MathiasDG · Oct 28, 2015 at 02:04 PM

I think i found the answer i was looking for using RGFloat textures for the 256x256 array. I might end up storing the vertices and maybe even the triangles if possible in textures as well. I haven't tested this solution enough, but it seems to be good enough this far.

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

Answer by Statement · Oct 28, 2015 at 10:18 PM

Should i worry about this start loading time? Is it normal? It takes up to 5 seconds.

If loading takes that long and you don't need it at startup, you could consider loading it asynchronously. If you need it at startup, try exporting your content into a file which you load and parse manually. I don't know if you'll get any performance boost but it can be worth a shot. If you need it at startup but either method is too slow, figure out if you can compress the contents. Etc.

Is there a better way of storing these other than in a scriptable object?

If you can represent the mesh data as a tilemap, it may be better to consume CPU to build the triangles at load rather than waiting for the file system to deliver the data. Also if it's a tilemap like in most games, a lot of the tiles will be similar and can be RLE compressed for example.

When using scriptable objects, should they be stored in a prefab?

It is up to you how you want to store them. You can have a reference to it in your scene, or create an asset file or add it to an existing asset, such as a prefab.

Note that if you have it attached to an object in the scene, you'll lose the scriptable object if you accidentally remove the game object that had the reference to it. Why? Because it is deemed unused and will be cleared as part of a usual Resources.UnloadUnusedAssets. To put it in other words, scriptable objects that are referenced by the scene game objects will be serialized in the scene, unless the scriptable object is an asset. In that case it just store a reference to the asset.

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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Custom Class - What did I do wrong? 0 Answers

JsonUtility and Arrays - JSON must represent an object type. 0 Answers

Variables changes even on pause 0 Answers

How to load a folder of textures automatically to an array of Raw Images? 2 Answers

GameObject array 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