Made children randomly spawn

This commit is contained in:
Pasha Bibko
2026-01-13 10:42:09 +00:00
parent e700523706
commit 37e4ee8273
5 changed files with 52 additions and 0 deletions

View File

@@ -0,0 +1,32 @@
using UnityEngine;
namespace InterfaceOff
{
public class CanvasManager : MonoBehaviour
{
[field: SerializeField] public Canvas GameCanvas { get; private set; }
public static CanvasManager Instance { get; private set; }
private void Awake()
{
if (DebugUtils.IsNull(Instance))
{
Instance = this;
return;
}
Debug.LogError("Multiple instances of Canvas Manager have been found");
}
public static Vector3 GetRandomPositionOnCanvas()
{
Rect rect = Instance.GameCanvas.pixelRect;
float x = Random.Range(rect.xMin, rect.xMax);
float y = Random.Range(rect.yMin, rect.yMax);
return new Vector3(x, y, 0f);
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: e090ce5928c145c0b2e514412bc9cdb2
timeCreated: 1768300347

View File

@@ -8,6 +8,7 @@ namespace InterfaceOff
public void InstantiateWindowBase()
{
transform.position = CanvasManager.GetRandomPositionOnCanvas();
}
}
}

View File

@@ -35,12 +35,14 @@ namespace InterfaceOff
{
if (Input.GetKeyDown(KeyCode.Space))
{
/* Creates the gameobject with a random class */
GameObject go = Instantiate(SampleChild, GameCanvas.transform);
go.SetActive(true);
Type type = WindowTypes.GetRandom();
WindowBase windowBase = go.AddComponent(type) as WindowBase;
/* Checks it created correctly before instantiating further */
if (DebugUtils.IsNull(windowBase))
{
Debug.LogError("How did this happen");