46 lines
1.3 KiB
C#
46 lines
1.3 KiB
C#
using UnityEngine;
|
|
using System.Collections.Generic;
|
|
|
|
[CreateAssetMenu(menuName = "Inventory/Item Database")]
|
|
public class ItemDatabase : ScriptableObject
|
|
{
|
|
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;
|
|
|
|
public ItemData GetItemByID(int id)
|
|
{
|
|
if (_itemCache == null) InitCache();
|
|
return _itemCache.GetValueOrDefault(id);
|
|
}
|
|
|
|
private void InitCache()
|
|
{
|
|
_itemCache = new Dictionary<int, ItemData>();
|
|
foreach (var item in allItems)
|
|
{
|
|
if (item != null)
|
|
_itemCache[item.itemID] = item;
|
|
}
|
|
}
|
|
} |