From 3fb2121cc0d00cbd42b2ca10b5dfb399a4df1a04 Mon Sep 17 00:00:00 2001 From: chai <215380520@qq.com> Date: Thu, 21 Mar 2024 10:28:46 +0800 Subject: *misc --- GameCode/UIManager.cs | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 GameCode/UIManager.cs (limited to 'GameCode/UIManager.cs') diff --git a/GameCode/UIManager.cs b/GameCode/UIManager.cs new file mode 100644 index 0000000..2a7081f --- /dev/null +++ b/GameCode/UIManager.cs @@ -0,0 +1,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()?.SpawnUI(); + } + if (Input.GetMouseButtonDown(1)) + { + CloseUI(currentUI); + } + } +} -- cgit v1.1-26-g67d0