Adding positions of all assets to the list without loading into RAM

Hi everyone, I need to create a list of Addressable asset positions from the group in order to load them later in real time when approaching, but I can’t fully load them into memory, because the entire map takes 2 GB, which is unacceptable on mobile, now despite the fact that I load and unload separately, they are loaded into RAM That’s it.

public class AddressableTestRuntimeLoad : MonoBehaviour
{
    [SerializeField] private List<AssetReference> _mapAssets = new();
    [SerializeField] private List<Vector3> _mapAssetsPositions = new();

    public IEnumerator Start()
    {
        var loadAssetLocationsOperation = Addressables.LoadResourceLocationsAsync("map");

        yield return loadAssetLocationsOperation;

        foreach (IResourceLocation location in loadAssetLocationsOperation.Result)
        {
            AssetReference assetReference = new(location.PrimaryKey.ToString());
            _mapAssets.Add(assetReference);

            var instantiateAsset = assetReference.InstantiateAsync();

            yield return instantiateAsset;

            _mapAssetsPositions.Add(instantiateAsset.Result.transform.position);
            Addressables.Release(instantiateAsset);
        }
    }
}

This sounds like you can do this with OnValidate. These are a few assumptions I’m making right now:

  • Your list of map asset positions is supposed to be in sync with the number of map assets.
  • Your map assets won’t change their position frequently in your project’s life time.

You could do something like the following:

#if UNITY_EDITOR
private void OnValidate()
{
    _mapAssetsPositions.Clear();
    foreach (AssetReference assetReference in _mapAssets)
    {
        if (string.IsNullOrEmpty(assetReference.AssetGUID) || (assetReference.editorAsset is not GameObject gameObject))
        {
            continue;
        }
    
        _mapAssetsPositions.Add(gameObject.transform.position);
    }
}
#endif
1 Like

That’s cool, thanks a lot, but is it possible to do this by clicking some UpdateMapAssets button in the editor, I saw that you can only use static methods with [MenuItem]?

You can use a context item if you want to do it quickly (Unity - Scripting API: ContextMenu). The context item will also be visible if you right click on your component in the inspector.

Otherwise you can build a custom inspector that adds a button to do this, yes.

1 Like

I tried your code, the fact is that I need to record the positions of assets, and in the code, that is, the position of the object on which the script weighs “_mapAssetsPositions.Add(GameObject.transform.position);” Maybe I misunderstood you

Maybe I don’t need it. I use Addressables for open world streaming. I need to get references to the map’s prefabs (AssetReference) and its positions, then divide the world into chunks, and then in the Update go through all the AssetReference in the chunk and compare the position of the player and the prefab