Skip to content

AndanteTribe/ObjectReference

Repository files navigation

ObjectReference

Releases GitHub license openupm CI

English | 日本語

Overview

ObjectReference provides a simple interface and implementations for asynchronously loading Unity objects.

The IObjectReference<T> interface gives you a unified API for loading assets regardless of the underlying source — whether a direct serialized reference or Unity Addressables. A custom property drawer is included so you can configure the reference type directly in the Unity Inspector using [SerializeReference].

Requirements

Installation

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

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

Quick Start

Using with [SerializeReference] (Inspector-configurable)

using System.Threading;
using Cysharp.Threading.Tasks;
using ObjectReference;
using UnityEngine;

public class ObjectReferenceSample : MonoBehaviour
{
    // The type can be switched in the Inspector via the gear icon.
    [SerializeReference]
    private IObjectReference<GameObject> _reference;

    private async UniTaskVoid Start()
    {
        var obj = await _reference.LoadAsync(destroyCancellationToken);
        Instantiate(obj, Vector3.zero, Quaternion.identity);
    }

    private void OnDestroy()
    {
        _reference?.Dispose();
    }
}

Using AddressableObjectReference directly

using System.Threading;
using Cysharp.Threading.Tasks;
using ObjectReference;
using UnityEngine;

public class AddressableSample : MonoBehaviour
{
    private readonly IObjectReference<GameObject> _reference
        = new AddressableObjectReference<GameObject>("assets/prefabs/MyPrefab.prefab");

    private async UniTaskVoid Start()
    {
        var prefab = await _reference.LoadAsync(destroyCancellationToken);
        Instantiate(prefab, Vector3.zero, Quaternion.identity);
    }

    private void OnDestroy()
    {
        _reference.Dispose();
    }
}

API

IObjectReference<T>

Method Description
LoadAsync(CancellationToken cancellationToken) Loads the object asynchronously.
LoadAsync(IProgress<float> progress, CancellationToken cancellationToken) Loads the object asynchronously with progress reporting.
Dispose() Releases the loaded asset handle.

AddressableObjectReference<T>

An IObjectReference<T> implementation that loads assets via Unity Addressables. Requires Addressables and UniTask.

Constructor Description
AddressableObjectReference(string address) Creates an instance with the given Addressables address string.

License

This library is released under the MIT license.

About

Provides a simple interface and implementation for asynchronously loading Unity objects.

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages