WuhuIslandTesting/Library/PackageCache/com.unity.addressables@1.21.12/Tests/Editor/DocExampleCode/LoadWithEvent.cs
2025-01-07 02:06:59 +01:00

41 lines
1,015 B
C#

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
}