Skip to content

Instantly share code, notes, and snippets.

@tsubaki
Created May 31, 2019 08:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tsubaki/3f7291a2c14e3310f4ab339f78d43b30 to your computer and use it in GitHub Desktop.
Save tsubaki/3f7291a2c14e3310f4ab339f78d43b30 to your computer and use it in GitHub Desktop.
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