Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
1
Question by Inc8877 · Sep 04, 2021 at 01:40 PM · assetsmemory managementmanagement

The cheapest way to store a pointer to the addressable asset

Hi, straight to the point.

Traditionally, to save a link to the addressable asset, we use the AssetReference class with the [SerializeField] attribute, as a rough estimate, one serialized link weighs 370 bytes, for most this will hardly seem a problem, but in my case, I use cloud storage and every byte matters. I use OdinLZ4 and it gives a gain of 100 bytes, but that's still not enough for me.

In my project, one entity has about 30 AssetReference fields, there can be a large number of such entities, over time, with the development of the project, the amount of content will increase and accordingly the amount of data required for storage.

Therefore, I am interested in the question of how to reduce the addressable asset reference weight.

I looked at what data the AssetReference has and found the RuntimeKey field. Serialize this field is three times cheaper than the AssetReference field.

Having looked at the code that loads the addressable asset ( LoadAsset/Async), I found that it doesn't matter what I pass in the method ( AssetReference or RuntimeKey), in the end everything will come down to the UnityEngine.AddressableAssets.AddressablesImpl.EvaluateKey method that will return the RuntimeKey.

So, can i use the RuntimeKey instead of the AssetReference? Everything seems to be going perfectly, or not? Are there any nuances that I do not know?

Thanks to everyone who helps me :)

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
Best Answer

Answer by andrew-lukasik · Sep 05, 2021 at 09:04 PM

That is an interesting question! I'm not anything near an expert on addressables, so there might be something more obvious that I'm missing completely, but my first two ideas would be:


I.

Since Addressables.LoadAssetAsync<T>("AssetAddress"); uses string you can use them and figure out a way to serialize them in the most compact format. Which one is that?

 using UnityEngine;
 using Encoding = System.Text.Encoding;
 public class LetsCompareEncodedTextByteLength : MonoBehaviour
 {
     [SerializeField] string _text = "Level03/Textures/texture-123_diff";
     void Start ()
     {
         byte[] bytesASCII = Encoding.ASCII.GetBytes(_text);
         Debug.Log($"\"{Encoding.ASCII.GetString(bytesASCII)}\" is {bytesASCII.Length} bytes when encoded as ASCII");
 
         byte[] bytesUTF7 = Encoding.UTF7.GetBytes(_text);
         Debug.Log($"\"{Encoding.UTF7.GetString(bytesUTF7)}\" is {bytesUTF7.Length} bytes when encoded as UTF7");
         
         byte[] bytesUTF8 = Encoding.UTF8.GetBytes(_text);
         Debug.Log($"\"{Encoding.UTF8.GetString(bytesUTF8)}\" is {bytesUTF8.Length} bytes when encoded as UTF8");
 
         byte[] bytesUTF32 = Encoding.UTF32.GetBytes(_text);
         Debug.Log($"\"{Encoding.UTF32.GetString(bytesUTF32)}\" is {bytesUTF32.Length} bytes when encoded as UTF32");
 
         byte[] bytesUnicode = Encoding.Unicode.GetBytes(_text);
         Debug.Log($"\"{Encoding.Unicode.GetString(bytesUnicode)}\" is {bytesUnicode.Length} bytes when encoded as Unicode");
     }
 }

UTF8, for example:

"Level03/Textures/texture-123_diff" is 33 bytes when encoded as UTF8

So you can make a decision to use UTF-8 asset addresses only (no culture-specific characters) + serialize them in this format + purposefully limit their length, from now on. Realistically, this would buy you something like 32 (shorter address is ofc possible but impractical) to 128 bytes per asset ptr (assuming 128 chars is this self-imposed limit).

II.

You can generate 32-bit ( int, 4 bytes) or 64-bit ( long, 8 bytes) hashes for all the addressable assets (test for hash collisions ofc). Serialize keys while in editor but serialize hashes (only) for production builds & match the two in some kind of look-up table (Hashmap/Dict, file or database).

The most basic look-up table I can possibly imagine is a text file with all the possible string keys and hash is literally just a line number (an uint offset ptr).

This option would be a bit harder, because of required tooling, but would result in constant size 4 or 8 bytes/pointer cost from that point onward (given that the serialization is not lazy JSON, text etc... :T but byte[]).


Comment
Add comment · Show 1 · 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 Inc8877 · Sep 19, 2021 at 08:47 AM 1
Share

Thank you very much for your reply! The hash option sounds great, I will implement this idea and add such a function to the AddressablesMaster.

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

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

Difference between Resources folder Vs normal folder 0 Answers

Assigning a texture for object in inspector. Is it loaded right away? 0 Answers

Do Addressable assets generate memory allocation? 0 Answers

Any solutions for heating problem & large fbx & memory consumption ? 0 Answers

Is there a need to destroy assets created during runtime to prevent memory leaks? 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