Skip to content

Repository files navigation

AssetsRegistry

unity-test Releases GitHub license openupm

English | 日本語

Overview

AssetsRegistry is a Unity Addressables utility for temporarily caching asset handles during usage.

When using Unity's Addressables system, you need to manage AsyncOperationHandle instances carefully — failing to release them causes memory leaks, while releasing them too early causes issues. AssetsRegistry solves this by collecting all handles loaded within a given scope and releasing them all at once when the scope ends (via Dispose).

Requirements

Installation

Open Window > Package Manager, select [+] > Add package from git URL, and enter the following URL:

https://github.com/AndanteTribe/AssetsRegistry.git?path=src/AssetsRegistry.Unity/Packages/jp.andantetribe.assetsregistry

Quick Start

using System.Threading;
using AndanteTribe.Unity.Extensions;
using Cysharp.Threading.Tasks;
using UnityEngine;

public class AssetsRegistrySample : MonoBehaviour
{
    private readonly AssetsRegistry _registry = new AssetsRegistry();

    private async UniTaskVoid Start()
    {
        // 1. Load a prefab and instantiate it manually
        var prefab = await _registry.LoadAsync<GameObject>("assets/prefabs/MyPrefab.prefab", destroyCancellationToken);
        Instantiate(prefab, Vector3.zero, Quaternion.identity);

        // 2. Load a prefab and instantiate a specific component from it
        //    (assumes that MyComponent is attached to the prefab)
        var component = await _registry.InstantiateAsync<MyComponent>("assets/prefabs/MyPrefabWithComponent.prefab", transform, destroyCancellationToken);
        component.transform.localPosition = Vector3.up;
    }

    private void OnDestroy()
    {
        // Releases all cached handles at once
        _registry.Dispose();
    }
}

API

Method Description
LoadAsync<TObject>(string address, CancellationToken cancellationToken) Loads an asset asynchronously by its Addressables address string and caches the handle.
LoadAsync<TObject>(AssetReferenceT<TObject> reference, CancellationToken cancellationToken) Loads an asset asynchronously by its AssetReferenceT and caches the handle.
InstantiateAsync<TComponent>(string address, Transform parent, CancellationToken cancellationToken) Loads a prefab by address string, instantiates it, and returns the specified component. The handle is cached.
InstantiateAsync<TComponent>(AssetReferenceT<GameObject> reference, Transform parent, CancellationToken cancellationToken) Loads a prefab by AssetReferenceT, instantiates it, and returns the specified component. The handle is cached.
Clear() Releases all cached asset handles and clears the registry.
Dispose() Equivalent to Clear(). Releases all cached asset handles.

License

This library is released under the MIT license.

About

Unity Addressables utility for temporarily caching asset handles during usage.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages