From 50916358f2f1abf1f7796521f2924f4940b3bcfb Mon Sep 17 00:00:00 2001 From: Janis Date: Wed, 22 Feb 2023 14:23:26 +0100 Subject: [PATCH] GuiManager --- Assets/Scenes/SampleScene.unity | 16 ++- Assets/Scripts/PlayerController.cs | 5 + Assets/Scripts/UI/DraggableSlotContentUI.cs | 2 - Assets/Scripts/UI/GuiManager.cs | 140 ++++++++++++++++++++ Assets/Scripts/UI/GuiManager.cs.meta | 11 ++ Assets/Scripts/UI/GuiPanel.cs | 22 +++ Assets/Scripts/UI/GuiPanel.cs.meta | 11 ++ Assets/Scripts/UI/InventoryUI.cs | 10 +- 8 files changed, 212 insertions(+), 5 deletions(-) create mode 100644 Assets/Scripts/UI/GuiManager.cs create mode 100644 Assets/Scripts/UI/GuiManager.cs.meta create mode 100644 Assets/Scripts/UI/GuiPanel.cs create mode 100644 Assets/Scripts/UI/GuiPanel.cs.meta diff --git a/Assets/Scenes/SampleScene.unity b/Assets/Scenes/SampleScene.unity index 6c033b8..b61d9e0 100644 --- a/Assets/Scenes/SampleScene.unity +++ b/Assets/Scenes/SampleScene.unity @@ -56726,6 +56726,7 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: 9269c8c372de3f5498d2af345ee9c855, type: 3} m_Name: m_EditorClassIdentifier: + panelName: Inventory slotPrefab: {fileID: 9079120991637034566, guid: 9fb7f575f90d73343b4841c421d17567, type: 3} slotListObj: {fileID: 1707136373} slotUIList: [] @@ -57525,8 +57526,9 @@ GameObject: - component: {fileID: 2143575112} - component: {fileID: 2143575111} - component: {fileID: 2143575110} + - component: {fileID: 2143575114} m_Layer: 5 - m_Name: Canvas + m_Name: GUI m_TagString: Untagged m_Icon: {fileID: 0} m_NavMeshLayer: 0 @@ -57615,3 +57617,15 @@ RectTransform: m_AnchoredPosition: {x: 0, y: 0} m_SizeDelta: {x: 0, y: 0} m_Pivot: {x: 0, y: 0} +--- !u!114 &2143575114 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 2143575109} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 2ccedadcedce99843833ee718fc06377, type: 3} + m_Name: + m_EditorClassIdentifier: diff --git a/Assets/Scripts/PlayerController.cs b/Assets/Scripts/PlayerController.cs index 2272a49..0902392 100644 --- a/Assets/Scripts/PlayerController.cs +++ b/Assets/Scripts/PlayerController.cs @@ -35,6 +35,11 @@ public class PlayerController : MonoBehaviour } } + if (Input.GetKeyDown(KeyCode.Tab)) + { + GuiManager.Instance.TogglePanel("Inventory"); + } + //! DEBUG if (Input.GetKeyDown(KeyCode.U)) { diff --git a/Assets/Scripts/UI/DraggableSlotContentUI.cs b/Assets/Scripts/UI/DraggableSlotContentUI.cs index dc2cab8..268821c 100644 --- a/Assets/Scripts/UI/DraggableSlotContentUI.cs +++ b/Assets/Scripts/UI/DraggableSlotContentUI.cs @@ -2,8 +2,6 @@ using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.UI; - - public class DraggableSlotContentUI : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler { public int slotIndex; // slotIndex is set by SlotUI diff --git a/Assets/Scripts/UI/GuiManager.cs b/Assets/Scripts/UI/GuiManager.cs new file mode 100644 index 0000000..4a15ac3 --- /dev/null +++ b/Assets/Scripts/UI/GuiManager.cs @@ -0,0 +1,140 @@ +using System.Collections.Generic; +using UnityEngine; + +public class GuiManager : MonoBehaviour +{ + + private static GuiManager instance; + + public static GuiManager Instance + { + get + { + if (instance == null) + { + instance = FindObjectOfType(); + if (instance == null) + { + Debug.LogError("No GuiManager found in the scene."); + } + } + return instance; + } + } + + + + [SerializeField] private List panels = new List(); + + private void Awake() + { + if (instance == null) + { + instance = this; + } + else + { + Destroy(gameObject); + } + } + + + public void RegisterPanel(GuiPanel panel) + { + panel.gameObject.SetActive(false); + panels.Add(panel); + } + + public GuiPanel GetPanelByName(string name) + { + name = name.ToLower(); + foreach (GuiPanel panel in panels) + { + if (panel.GetName().ToLower() == name) + { + return panel; + } + } + return null; + } + + public void OpenPanel(string name) + { + GuiPanel panel = GetPanelByName(name); + if (panel != null) + { + OpenPanel(panel); + } + } + + public void OpenPanel(GuiPanel panel) + { + + if (!panel.gameObject.activeSelf) + { + panel.gameObject.SetActive(true); + panel.OnClose(); + Debug.Log("Open Panel"); + return; + } + Debug.Log("Panel is already opened"); + } + + + public void ClosePanel(string name) + { + GuiPanel panel = GetPanelByName(name); + if (panel != null) + { + ClosePanel(panel); + } + } + + public void ClosePanel(GuiPanel panel) + { + if (panel.gameObject.activeSelf) + { + panel.gameObject.SetActive(false); + panel.OnClose(); + Debug.Log("Closed Panel"); + return; + } + Debug.Log("Panel is already closed"); + } + + + public void CloseAllPanels() + { + foreach (GuiPanel panel in panels) + { + if (panel.gameObject.activeSelf) + { + ClosePanel(panel); + } + } + } + + public void TogglePanel(string name) + { + GuiPanel panel = GetPanelByName(name); + if (panel != null) + { + TogglePanel(panel); + } + } + + public void TogglePanel(GuiPanel panel) + { + if (panel != null) + { + if (panel.gameObject.activeSelf) + { + ClosePanel(panel); + } + else + { + OpenPanel(panel); + } + } + } +} \ No newline at end of file diff --git a/Assets/Scripts/UI/GuiManager.cs.meta b/Assets/Scripts/UI/GuiManager.cs.meta new file mode 100644 index 0000000..d5dc4b9 --- /dev/null +++ b/Assets/Scripts/UI/GuiManager.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 2ccedadcedce99843833ee718fc06377 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/UI/GuiPanel.cs b/Assets/Scripts/UI/GuiPanel.cs new file mode 100644 index 0000000..72d9637 --- /dev/null +++ b/Assets/Scripts/UI/GuiPanel.cs @@ -0,0 +1,22 @@ +using UnityEngine; + +public abstract class GuiPanel : MonoBehaviour +{ + [SerializeField] private string panelName; + + public string GetName() + { + return panelName; + } + + private void Awake() + { + GuiManager.Instance.RegisterPanel(this); + } + + public abstract void OnOpen(); + public abstract void OnClose(); +} + + + diff --git a/Assets/Scripts/UI/GuiPanel.cs.meta b/Assets/Scripts/UI/GuiPanel.cs.meta new file mode 100644 index 0000000..22031cd --- /dev/null +++ b/Assets/Scripts/UI/GuiPanel.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 65c73c21bddab2d45b5b2f5a01bc9056 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/UI/InventoryUI.cs b/Assets/Scripts/UI/InventoryUI.cs index 17bea10..91b93ea 100644 --- a/Assets/Scripts/UI/InventoryUI.cs +++ b/Assets/Scripts/UI/InventoryUI.cs @@ -2,7 +2,7 @@ using System.Collections; using System.Collections.Generic; using UnityEngine; -public class InventoryUI : MonoBehaviour +public class InventoryUI : GuiPanel { [SerializeField] private GameObject slotPrefab; [SerializeField] private GameObject slotListObj; @@ -15,10 +15,16 @@ public class InventoryUI : MonoBehaviour playerInventory = GameObject.FindGameObjectWithTag("Player").GetComponent(); Inventory.OnPlayerInventoryChanged += UpdateSlots; CreateSlots(); + } + public override void OnOpen() + { UpdateSlots(); - } + public override void OnClose() + { + + } private void CreateSlots() { playerInventory = GameObject.FindGameObjectWithTag("Player").GetComponent();