인벤토리/퀵슬롯 시스템 개발
This commit is contained in:
@@ -1,12 +1,46 @@
|
||||
using UnityEngine;
|
||||
using System.Collections.Generic;
|
||||
|
||||
public class ItemDatabase : MonoBehaviour
|
||||
[CreateAssetMenu(menuName = "Inventory/Item Database")]
|
||||
public class ItemDatabase : ScriptableObject
|
||||
{
|
||||
public static ItemDatabase Instance;
|
||||
private static ItemDatabase _instance;
|
||||
|
||||
// 전역 어디서든 접근 가능한 Instance 프로퍼티
|
||||
public static ItemDatabase Instance
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_instance == null)
|
||||
{
|
||||
// Resources 폴더 안에 있는 "ItemDatabase"라는 이름의 파일을 찾습니다.
|
||||
_instance = Resources.Load<ItemDatabase>("ItemDatabase");
|
||||
|
||||
if (_instance == null)
|
||||
{
|
||||
Debug.LogError("ItemDatabase 에셋을 찾을 수 없습니다! Resources 폴더 안에 이름이 'ItemDatabase'인 에셋이 있는지 확인하세요.");
|
||||
}
|
||||
}
|
||||
return _instance;
|
||||
}
|
||||
}
|
||||
|
||||
public List<ItemData> allItems;
|
||||
private Dictionary<int, ItemData> _itemCache;
|
||||
|
||||
void Awake() => Instance = this;
|
||||
public ItemData GetItemByID(int id)
|
||||
{
|
||||
if (_itemCache == null) InitCache();
|
||||
return _itemCache.GetValueOrDefault(id);
|
||||
}
|
||||
|
||||
public ItemData GetItemByID(int id) => allItems.Find(x => x.itemID == id);
|
||||
private void InitCache()
|
||||
{
|
||||
_itemCache = new Dictionary<int, ItemData>();
|
||||
foreach (var item in allItems)
|
||||
{
|
||||
if (item != null)
|
||||
_itemCache[item.itemID] = item;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user