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

People who like this

0 Show 0
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

  • Sort: 
avatar image

Answer by rajeshred · Feb 08, 2022 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

People who like this

0 Show 0 · 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

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta on June 13. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

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

Saving the state of a monobehavior in play mode. 1 Answer

Saving editor-only variables 0 Answers

Displaying System.objects not serialized by Unity in a Custom Editor 0 Answers

Showing properties from a base class in inspector? 0 Answers

Unable to draw propertyfield in inspector for some classes, findProperty returns null. 0 Answers


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