Made children spawn
This commit is contained in:
52
Assets/Scripts/WindowSpawner.cs
Normal file
52
Assets/Scripts/WindowSpawner.cs
Normal file
@@ -0,0 +1,52 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace InterfaceOff
|
||||
{
|
||||
public class WindowSpawner : MonoBehaviour
|
||||
{
|
||||
private List<Type> WindowTypes { get; } = new();
|
||||
|
||||
[field: SerializeField] private Canvas GameCanvas { get; set; }
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
/* Fetches all window types created */
|
||||
Type[] types = typeof(WindowBase).Assembly.GetTypes();
|
||||
foreach (Type t in types)
|
||||
{
|
||||
if (t.IsSubclassOf(typeof(WindowBase)))
|
||||
{
|
||||
WindowTypes.Add(t);
|
||||
}
|
||||
}
|
||||
|
||||
/* Logs the amount of types found and errors if there is none */
|
||||
Debug.Log($"Found [{WindowTypes.Count}] different window types ");
|
||||
if (WindowTypes.Count == 0)
|
||||
{
|
||||
Debug.LogError("Could not find any window types");
|
||||
}
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (Input.GetKeyDown(KeyCode.Space))
|
||||
{
|
||||
Type type = WindowTypes.GetRandom();
|
||||
GameObject go = new();
|
||||
go.transform.SetParent(GameCanvas.transform);
|
||||
|
||||
WindowBase windowBase = go.AddComponent(type) as WindowBase;
|
||||
if (DebugUtils.IsNull(windowBase))
|
||||
{
|
||||
Debug.LogError("How did this happen");
|
||||
return;
|
||||
}
|
||||
|
||||
windowBase.InstantiateWindowBase();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user