English | 日本語
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].
- Unity 6000.0 or later
- (Optional) Addressables and UniTask for
AddressableObjectReference
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
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 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();
}
}| 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. |
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. |
This library is released under the MIT license.