42 lines
1.0 KiB
C#
42 lines
1.0 KiB
C#
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
namespace EnergyBarToolkit
|
|
{
|
|
[ExecuteInEditMode]
|
|
public class InstantiateRandom : MonoBehaviour
|
|
{
|
|
public EnergyBarSpawnerUGUI prefab;
|
|
|
|
public Bounds bounds;
|
|
|
|
private List<GameObject> instances = new List<GameObject>();
|
|
|
|
private void OnGUI()
|
|
{
|
|
if (!Application.isPlaying)
|
|
{
|
|
GUILayout.Label("Hit the Play button!");
|
|
return;
|
|
}
|
|
if (GUILayout.Button("Instantiate Now"))
|
|
{
|
|
EnergyBarSpawnerUGUI energyBarSpawnerUGUI = Object.Instantiate(prefab, RandomPosition(), Quaternion.identity);
|
|
instances.Add(energyBarSpawnerUGUI.gameObject);
|
|
}
|
|
if (GUILayout.Button("Destroy Any") && instances.Count > 0)
|
|
{
|
|
int index = Random.Range(0, instances.Count);
|
|
GameObject obj = instances[index];
|
|
Object.Destroy(obj);
|
|
instances.RemoveAt(index);
|
|
}
|
|
}
|
|
|
|
private Vector3 RandomPosition()
|
|
{
|
|
return new Vector3(Random.Range(bounds.min.x, bounds.max.x), Random.Range(bounds.min.y, bounds.max.y), Random.Range(bounds.min.z, bounds.max.z));
|
|
}
|
|
}
|
|
}
|