mirror of
https://github.com/DerTyp7/harvestdale-unity.git
synced 2025-10-30 13:07:10 +01:00
GuiManager
This commit is contained in:
@@ -35,6 +35,11 @@ public class PlayerController : MonoBehaviour
|
||||
}
|
||||
}
|
||||
|
||||
if (Input.GetKeyDown(KeyCode.Tab))
|
||||
{
|
||||
GuiManager.Instance.TogglePanel("Inventory");
|
||||
}
|
||||
|
||||
//! DEBUG
|
||||
if (Input.GetKeyDown(KeyCode.U))
|
||||
{
|
||||
|
||||
@@ -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
|
||||
|
||||
140
Assets/Scripts/UI/GuiManager.cs
Normal file
140
Assets/Scripts/UI/GuiManager.cs
Normal file
@@ -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<GuiManager>();
|
||||
if (instance == null)
|
||||
{
|
||||
Debug.LogError("No GuiManager found in the scene.");
|
||||
}
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
[SerializeField] private List<GuiPanel> panels = new List<GuiPanel>();
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/UI/GuiManager.cs.meta
Normal file
11
Assets/Scripts/UI/GuiManager.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 2ccedadcedce99843833ee718fc06377
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
22
Assets/Scripts/UI/GuiPanel.cs
Normal file
22
Assets/Scripts/UI/GuiPanel.cs
Normal file
@@ -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();
|
||||
}
|
||||
|
||||
|
||||
|
||||
11
Assets/Scripts/UI/GuiPanel.cs.meta
Normal file
11
Assets/Scripts/UI/GuiPanel.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 65c73c21bddab2d45b5b2f5a01bc9056
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -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>();
|
||||
Inventory.OnPlayerInventoryChanged += UpdateSlots;
|
||||
CreateSlots();
|
||||
}
|
||||
public override void OnOpen()
|
||||
{
|
||||
UpdateSlots();
|
||||
|
||||
}
|
||||
|
||||
public override void OnClose()
|
||||
{
|
||||
|
||||
}
|
||||
private void CreateSlots()
|
||||
{
|
||||
playerInventory = GameObject.FindGameObjectWithTag("Player").GetComponent<Inventory>();
|
||||
|
||||
Reference in New Issue
Block a user