Firstborn/Library/PackageCache/com.unity.addressables@1.19.19/Tests/Editor/DocExampleCode/LoadWithEvent.cs

34 lines
953 B
C#
Raw Normal View History

namespace AddressableAssets.DocExampleCode
{
#region doc_LoadWithEvent
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.ResourceManagement.AsyncOperations;
internal class LoadWithEvent : MonoBehaviour
{
public string address;
AsyncOperationHandle<GameObject> opHandle;
void Start() {
// Create operation
opHandle = Addressables.LoadAssetAsync<GameObject>(address);
// Add event handler
opHandle.Completed += Operation_Completed;
}
private void Operation_Completed(AsyncOperationHandle<GameObject> obj) {
if (obj.Status == AsyncOperationStatus.Succeeded) {
Instantiate(obj.Result, transform);
} else {
Addressables.Release(obj);
}
}
void OnDestroy() {
Addressables.Release(opHandle);
}
}
#endregion
}