mirror of
				https://github.com/DerTyp7/harvestdale-unity.git
				synced 2025-10-31 05:27:07 +01:00 
			
		
		
		
	hotbar
This commit is contained in:
		| @@ -28,14 +28,13 @@ public class GuiManager : MonoBehaviour | ||||
|  | ||||
|   private void Awake() | ||||
|   { | ||||
|     if (instance == null) | ||||
|     if (instance != null && instance != this) | ||||
|     { | ||||
|       instance = this; | ||||
|     } | ||||
|     else | ||||
|     { | ||||
|       Destroy(gameObject); | ||||
|       Destroy(this.gameObject); | ||||
|       return; | ||||
|     } | ||||
|     instance = this; | ||||
|     DontDestroyOnLoad(gameObject); | ||||
|   } | ||||
|  | ||||
|  | ||||
| @@ -65,6 +64,10 @@ public class GuiManager : MonoBehaviour | ||||
|     { | ||||
|       OpenPanel(panel); | ||||
|     } | ||||
|     else | ||||
|     { | ||||
|       Debug.Log("Panel not found: " + name); | ||||
|     } | ||||
|   } | ||||
|  | ||||
|   public void OpenPanel(GuiPanel panel) | ||||
| @@ -73,7 +76,7 @@ public class GuiManager : MonoBehaviour | ||||
|     if (!panel.gameObject.activeSelf) | ||||
|     { | ||||
|       panel.gameObject.SetActive(true); | ||||
|       panel.OnClose(); | ||||
|       panel.OnOpen(); | ||||
|       Debug.Log("Open Panel"); | ||||
|       return; | ||||
|     } | ||||
|   | ||||
							
								
								
									
										56
									
								
								Assets/Scripts/UI/HotbarSlotUI.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										56
									
								
								Assets/Scripts/UI/HotbarSlotUI.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,56 @@ | ||||
| using System.Collections; | ||||
| using UnityEngine.UI; | ||||
| using System.Collections.Generic; | ||||
| using UnityEngine; | ||||
| using TMPro; | ||||
| public class HotbarSlotUI : MonoBehaviour | ||||
| { | ||||
|   public int slotIndex; | ||||
|  | ||||
|   private PlayerController playerController; | ||||
|  | ||||
|   private InventoryItem invItem; | ||||
|  | ||||
|   [SerializeField] private Image border; | ||||
|   [SerializeField] private Image image; | ||||
|   [SerializeField] private TextMeshProUGUI quantityText; | ||||
|  | ||||
|   private void Start() | ||||
|   { | ||||
|     playerController = GameObject.FindGameObjectWithTag("Player").GetComponent<PlayerController>(); | ||||
|     PlayerController.OnActiveSlotChanged += CheckSelectedSlot; | ||||
|     CheckSelectedSlot(); | ||||
|   } | ||||
|  | ||||
|   private void CheckSelectedSlot() | ||||
|   { | ||||
|     if (playerController.activeHotbarSlot == slotIndex) | ||||
|     { | ||||
|       border.gameObject.SetActive(true); | ||||
|     } | ||||
|     else | ||||
|     { | ||||
|       border.gameObject.SetActive(false); | ||||
|     } | ||||
|   } | ||||
|  | ||||
|   public void SetInventoryItem(InventoryItem newInvItem) | ||||
|   { | ||||
|     invItem = newInvItem; | ||||
|     UpdateSlot(); | ||||
|   } | ||||
|  | ||||
|   private void UpdateSlot() | ||||
|   { | ||||
|     image.sprite = invItem?.item?.sprite ?? null; | ||||
|     quantityText.SetText(invItem?.count.ToString() ?? ""); | ||||
|     if (image.sprite == null) | ||||
|     { | ||||
|       image.color = new Color(0, 0, 0, 0); | ||||
|     } | ||||
|     else | ||||
|     { | ||||
|       image.color = Color.white; | ||||
|     } | ||||
|   } | ||||
| } | ||||
							
								
								
									
										11
									
								
								Assets/Scripts/UI/HotbarSlotUI.cs.meta
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								Assets/Scripts/UI/HotbarSlotUI.cs.meta
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,11 @@ | ||||
| fileFormatVersion: 2 | ||||
| guid: 12c5ff2bade265a499f7b2d983c6508c | ||||
| MonoImporter: | ||||
|   externalObjects: {} | ||||
|   serializedVersion: 2 | ||||
|   defaultReferences: [] | ||||
|   executionOrder: 0 | ||||
|   icon: {instanceID: 0} | ||||
|   userData:  | ||||
|   assetBundleName:  | ||||
|   assetBundleVariant:  | ||||
							
								
								
									
										52
									
								
								Assets/Scripts/UI/HotbarUI.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										52
									
								
								Assets/Scripts/UI/HotbarUI.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,52 @@ | ||||
| using System.Collections; | ||||
| using System.Collections.Generic; | ||||
| using UnityEngine; | ||||
| using UnityEngine.UI; | ||||
|  | ||||
| public class HotbarUI : GuiPanel | ||||
| { | ||||
|   [SerializeField] private GameObject hotbarSlotPrefab; | ||||
|   private List<HotbarSlotUI> slots = new List<HotbarSlotUI>(); | ||||
|   private Inventory playerInventory; | ||||
|   private PlayerController playerController; | ||||
|   private int hotbarSlotCount = 9; | ||||
|  | ||||
|   private void Start() | ||||
|   { | ||||
|     Inventory.OnPlayerInventoryChanged += UpdateSlots; | ||||
|   } | ||||
|   public override void OnOpen() | ||||
|   { | ||||
|     if (slots.Count == 0) | ||||
|     { | ||||
|       playerInventory = GameObject.FindGameObjectWithTag("Player").GetComponent<Inventory>(); | ||||
|       playerController = GameObject.FindGameObjectWithTag("Player").GetComponent<PlayerController>(); | ||||
|       hotbarSlotCount = playerController.hotbarSlotCount; | ||||
|       CreateSlots(); | ||||
|     } | ||||
|     UpdateSlots(); | ||||
|   } | ||||
|  | ||||
|   public override void OnClose() | ||||
|   { | ||||
|   } | ||||
|  | ||||
|   private void CreateSlots() | ||||
|   { | ||||
|     for (int i = 0; i < hotbarSlotCount; i++) | ||||
|     { | ||||
|       HotbarSlotUI newSlot = Instantiate(hotbarSlotPrefab, Vector3.zero, Quaternion.identity, transform).GetComponent<HotbarSlotUI>(); | ||||
|       newSlot.slotIndex = i; | ||||
|       slots.Add(newSlot); | ||||
|     } | ||||
|   } | ||||
|  | ||||
|   private void UpdateSlots() | ||||
|   { | ||||
|     for (int i = 0; i < slots.Count; i++) | ||||
|     { | ||||
|       slots[i].SetInventoryItem(playerInventory.items[i] ?? null); | ||||
|     } | ||||
|   } | ||||
|  | ||||
| } | ||||
							
								
								
									
										11
									
								
								Assets/Scripts/UI/HotbarUI.cs.meta
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								Assets/Scripts/UI/HotbarUI.cs.meta
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,11 @@ | ||||
| fileFormatVersion: 2 | ||||
| guid: daff4f23cf5534f478f13df54b51124a | ||||
| MonoImporter: | ||||
|   externalObjects: {} | ||||
|   serializedVersion: 2 | ||||
|   defaultReferences: [] | ||||
|   executionOrder: 0 | ||||
|   icon: {instanceID: 0} | ||||
|   userData:  | ||||
|   assetBundleName:  | ||||
|   assetBundleVariant:  | ||||
| @@ -12,12 +12,15 @@ public class InventoryUI : GuiPanel | ||||
|  | ||||
|   private void Start() | ||||
|   { | ||||
|     playerInventory = GameObject.FindGameObjectWithTag("Player").GetComponent<Inventory>(); | ||||
|     Inventory.OnPlayerInventoryChanged += UpdateSlots; | ||||
|     CreateSlots(); | ||||
|   } | ||||
|   public override void OnOpen() | ||||
|   { | ||||
|     if (slotUIList.Count == 0) | ||||
|     { | ||||
|       playerInventory = GameObject.FindGameObjectWithTag("Player").GetComponent<Inventory>(); | ||||
|       CreateSlots(); | ||||
|     } | ||||
|     UpdateSlots(); | ||||
|   } | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Janis
					Janis