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 /
avatar image
0
Question by JasonCG · Mar 03, 2018 at 11:20 PM · editorinspectorserializationmonobehaviour

Large serializable field in a MonoBehaviour causing poor performance in Inspector

I have asset bundles where I group 3d models with additional meta data. A key requirement is I need an image of each 3d model. The simplest solution I've found is to generate these images/snapshots in the Unity Editor using Editor APIs, and storing the result with the model.

So I have a couple classes that look like this (simplified here):

 [Serializable]
 public class Model {
     [HideInInspector]    //Serialized, but not shown in inspector
     public byte[] texture;
     public GameObject model;
 
     public string name;
     public string description;
 
     public void GenerateTexture() { ... } //This is called with a custom Inspector
 
     public Texture2D Texture {
         get {...}    //Converts this.texture to an actual Texture2D
         set {...}    //Converts a Texture2D to a byte[]
     }
 }
 
 public class ModelPack:MonoBehaviour {
     public List<Model> gameModels;
 }



ModelPack(s) are stored in asset bundles which are then loaded at runtime.

This works perfectly, except one minor-ish problem: performance in the Unity Inspector is terrible. The Unity (Editor) profiler shows a large amount of garbage collection happening in every UI update causing the whole Unity UI to freeze and skip. For the longest time I thought there was an issue with the custom inspector I wrote for the ModelPack/Model classes, but even completely disabling that the performance issue remains. I've finally come to the conclusion that the problem seems to be the texture byte array. If I mark it as NonSerialized then performance is fine in the Inspector.

I suspect the problem is that it's constantly getting/setting and serializing/deserializing the value of the whole object in the inspector, even though I have ensured the texture value itself isn't changing unless the associated model is changed. Since the texture is rather large (megabytes in size) that could create quite a strain.

Does this make sense? Is there a recommended limit on how much data should be stored when serializing a Unity MonoBehaviour? Is the entire MonoBehaviour serialized in one go every time? Is there a way to filter out that one field, or will I have to find an alternate way of storing these large byte arrays such as saving them to files?

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by rajeshred · Feb 08 at 03:45 AM

Storing any kind of array with very large amount of elements is always a problem. Unity have to Serialize and Deserialize each elements individually, and in the inspector it does this frequently. I would recommend you to convert the whole byte array into a single string so that its just a single serialization. you can use this method to convert a byte array into a string System.Convert.ToBase64String and use System.Convert.FromBase64String to convert the string back to byte array. This way the serialization load can be reduced.

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

106 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 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 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 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

Order of a serializable list not saved correctly using ReorderableList 2 Answers

How can I determine if property was set to 'None (Audio Clip)' 1 Answer

Prevent 'Paste Component Values' on MonoBehaviour 1 Answer

Can i extend or customise editor for all MonoBehaviours? 3 Answers

Populating list of custom class through inspector 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