- Home /
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 :)
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:
So you can make a decision to use"Level03/Textures/texture-123_diff" is 33 bytes when encoded as UTF8
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[]
).
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
Follow this Question
Related Questions
Difference between Resources folder Vs normal folder 0 Answers
Do Addressable assets generate memory allocation? 0 Answers
Assigning a texture for object in inspector. Is it loaded right away? 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