Created
May 31, 2019 08:01
-
-
Save tsubaki/3f7291a2c14e3310f4ab339f78d43b30 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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