Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AddressableAssets;
public class SpawnerWithAssetReference : MonoBehaviour
{
[SerializeField] AssetReferenceGameObject obj;
void Awake()
{
obj.LoadAssetAsync();
}
void OnDestroy()
{
obj.ReleaseAsset();
}
public void Spawn()
{
if( obj.Asset == null)
return;
var x = Random.Range(-3f, 3f);
var z = Random.Range(-3f, 3f);
GameObject.Instantiate(obj.Asset, new Vector3(x, 10, z), Quaternion.identity);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.ResourceManagement.AsyncOperations;
using UnityEngine.ResourceManagement.ResourceLocations;
public class SpawnerWithHandle : MonoBehaviour
{
AsyncOperationHandle<GameObject> handle;
[SerializeField] string addressName = "White";
void Awake()
{
handle = Addressables.LoadAssetAsync<GameObject>(addressName);
}
void OnDestroy()
{
Addressables.Release(handle);
}
public void Spawn()
{
if( handle.IsDone == false) return;
var x = Random.Range(-3f, 3f);
var z = Random.Range(-3f, 3f);
GameObject.Instantiate(handle.Result, new Vector3(x, 10, z), Random.rotation);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.