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

45 lines
1.2 KiB
C#

namespace AddressableAssets.DocExampleCode
{
#region doc_LoadWithAddress
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.ResourceManagement.AsyncOperations;
internal class LoadWithAddress : MonoBehaviour
{
// Assign in Editor or in code
public string address;
// Retain handle to release asset and operation
private AsyncOperationHandle<GameObject> handle;
// Start the load operation on start
void Start()
{
handle = Addressables.LoadAssetAsync<GameObject>(address);
handle.Completed += Handle_Completed;
}
// Instantiate the loaded prefab on complete
private void Handle_Completed(AsyncOperationHandle<GameObject> operation)
{
if (operation.Status == AsyncOperationStatus.Succeeded)
{
Instantiate(operation.Result, transform);
}
else
{
Debug.LogError($"Asset for {address} failed to load.");
}
}
// Release asset when parent object is destroyed
private void OnDestroy()
{
Addressables.Release(handle);
}
}
#endregion
}