summaryrefslogtreecommitdiff
path: root/GameCode/UIManager.cs
blob: 2a7081fc97978ce8d35dc4676699ceeb00e803ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
using UnityEngine;

public class UIManager : MonoBehaviour
{
	public static UIManager instance;

	[SerializeField]
	private LayerMask clickableMask;

	private GameObject currentUI;

	private void Awake()
	{
		instance = this;
	}

	public void SetNewUI(GameObject newUI)
	{
		if (currentUI != null)
		{
			Object.Destroy(currentUI);
		}
		currentUI = newUI;
	}

	public void CloseUI(GameObject oldUI)
	{
		currentUI = null;
		Object.Destroy(oldUI);
	}

	private void Update()
	{
		if (Input.GetKeyDown(KeyCode.Escape) && currentUI != null)
		{
			CloseUI(currentUI);
		}
		if (!BuildingManager.instance.buildMode && Input.GetMouseButtonDown(0) && Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out var hitInfo, 2000f, clickableMask, QueryTriggerInteraction.Collide))
		{
			hitInfo.collider.GetComponent<IBuildable>()?.SpawnUI();
		}
		if (Input.GetMouseButtonDown(1))
		{
			CloseUI(currentUI);
		}
	}
}