From 034ed30da10c2117e9c2507ed1fef3cab43ee79c Mon Sep 17 00:00:00 2001
From: miepzerino <o.skotnik@gmail.com>
Date: Wed, 02 Apr 2025 08:56:56 +0000
Subject: [PATCH] #36 load all items into itemDatabase on startup
---
Assets/Scripts/Inventory/ItemDatabase.cs | 27 +++++++++++++++++++++++++--
1 files changed, 25 insertions(+), 2 deletions(-)
diff --git a/Assets/Scripts/Inventory/ItemDatabase.cs b/Assets/Scripts/Inventory/ItemDatabase.cs
index c0a0067..b4832cb 100644
--- a/Assets/Scripts/Inventory/ItemDatabase.cs
+++ b/Assets/Scripts/Inventory/ItemDatabase.cs
@@ -5,9 +5,7 @@
{
public static ItemDatabase Instance { get; private set; }
- [SerializeField]
private List<Item> items = new List<Item>();
-
private Dictionary<string, Item> itemDictionary = new Dictionary<string, Item>();
private Dictionary<int, Item> itemIdDictionary = new Dictionary<int, Item>();
@@ -16,6 +14,7 @@
if (Instance == null)
{
Instance = this;
+ LoadItemsFromResources();
InitializeItemDictionary();
}
else
@@ -24,6 +23,30 @@
}
}
+ private void LoadItemsFromResources()
+ {
+ // Load all Item prefabs from the "Resources/Items" folder
+ GameObject[] itemPrefabs = Resources.LoadAll<GameObject>("Items");
+
+ foreach (GameObject prefab in itemPrefabs)
+ {
+ Item item = prefab.GetComponent<Item>();
+ if (item != null)
+ {
+ items.Add(item);
+ }
+ else
+ {
+ Debug.LogWarning($"Prefab {prefab.name} does not have an Item component");
+ }
+ }
+
+ if (items.Count == 0)
+ {
+ Debug.LogWarning("No items found in Resources/Items folder");
+ }
+ }
+
private void InitializeItemDictionary()
{
itemDictionary.Clear();
--
Gitblit v1.10.0