diff --git a/Assets/Scenes/SampleScene.unity b/Assets/Scenes/SampleScene.unity index 986a95c..1f619c4 100644 --- a/Assets/Scenes/SampleScene.unity +++ b/Assets/Scenes/SampleScene.unity @@ -435,6 +435,7 @@ GameObject: serializedVersion: 6 m_Component: - component: {fileID: 1539476655} + - component: {fileID: 1539476656} - component: {fileID: 1539476654} - component: {fileID: 1539476653} - component: {fileID: 1539476652} @@ -528,6 +529,19 @@ RectTransform: m_AnchoredPosition: {x: 0, y: 0} m_SizeDelta: {x: 0, y: 0} m_Pivot: {x: 0, y: 0} +--- !u!114 &1539476656 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1539476651} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: e090ce5928c145c0b2e514412bc9cdb2, type: 3} + m_Name: + m_EditorClassIdentifier: + k__BackingField: {fileID: 1539476654} --- !u!1 &1683319378 GameObject: m_ObjectHideFlags: 0 diff --git a/Assets/Scripts/CanvasManager.cs b/Assets/Scripts/CanvasManager.cs new file mode 100644 index 0000000..be92004 --- /dev/null +++ b/Assets/Scripts/CanvasManager.cs @@ -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); + } + } +} diff --git a/Assets/Scripts/CanvasManager.cs.meta b/Assets/Scripts/CanvasManager.cs.meta new file mode 100644 index 0000000..1a90450 --- /dev/null +++ b/Assets/Scripts/CanvasManager.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: e090ce5928c145c0b2e514412bc9cdb2 +timeCreated: 1768300347 \ No newline at end of file diff --git a/Assets/Scripts/WindowBase.cs b/Assets/Scripts/WindowBase.cs index 060ecf3..635d6b0 100644 --- a/Assets/Scripts/WindowBase.cs +++ b/Assets/Scripts/WindowBase.cs @@ -8,6 +8,7 @@ namespace InterfaceOff public void InstantiateWindowBase() { + transform.position = CanvasManager.GetRandomPositionOnCanvas(); } } } diff --git a/Assets/Scripts/WindowSpawner.cs b/Assets/Scripts/WindowSpawner.cs index cb9f866..725d01d 100644 --- a/Assets/Scripts/WindowSpawner.cs +++ b/Assets/Scripts/WindowSpawner.cs @@ -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");