
public class InventoryModel
{
// weapon정보들 키(int)는 슬롯의 index와 같다.
public Dictionary<int, WeaponInfo> weaponDatas { get; private set; } = new ();
// 장비가 아닌 아이템 인벤토리. 키(int)는 슬롯의 index와 같습니다.
public Dictionary<int, GenericItemInfo> itemDatas { get; private set; } = new ();
// 아이템 인벤토리의 아이템 개수입니다. 키(int)는 아이템 슬롯의 index와 같습니다.
public Dictionary<int, int> itemCounts { get; private set; } = new ();
}
List 형태로 데이터를 저장하므로 JsonUtility를 사용할 수 있습니다.using System.Collections.Generic;
[System.Serializable]
public class SerializableInventoryModel
{
public List<WeaponData> weaponDatas = new List<WeaponData>();
public List<ItemData> itemDatas = new List<ItemData>();
public int curSlotCounts;
[System.Serializable]
public class WeaponData
{
public int slotIndex;
public WeaponInfo weaponInfo;
}
[System.Serializable]
public class ItemData
{
public int slotIndex;
public GenericItemInfo itemInfo;
public int itemCount;
}
}
직렬화 과정
2.1) InventoryModel → SerializableInventoryModel 변환
ConvertToSerializable() 함수에서 InventoryModel 데이터를 SerializableInventoryModel로 변환.InventoryModel.weaponDatas의 데이터를 순회하며 slotIndex와 weaponInfo를 추출.weaponDatas 리스트에 추가.InventoryModel.itemDatas와 itemCounts의 데이터를 순회하며 slotIndex, itemInfo, itemCount를 추출.itemDatas 리스트에 추가.2.2) SerializableInventoryModel 저장
SaveInventoryModel()함수에서 SerializableInventoryModel을 JSON으로 저장.ConvertToSerializable()를 호출해 InventoryModel 데이터를 SerializableInventoryModel로 변환.JsonUtility.ToJson()을 사용해 JSON 문자열로 변환.InvenDataFilePath)로 저장. private InventoryModel ConvertToInventoryModel(SerializableInventoryModel serializableModel)
{
var inventoryModel = new InventoryModel();
inventoryModel.InitDic(serializableModel.curSlotCounts);
foreach (var weaponData in serializableModel.weaponDatas)
{
if (weaponData.weaponInfo.ID == 0)
continue;
inventoryModel.weaponDatas[weaponData.slotIndex] = weaponData.weaponInfo;
}
foreach (var itemData in serializableModel.itemDatas)
{
if (itemData.itemCount == 0)
continue;
inventoryModel.itemDatas[itemData.slotIndex] = ItemFactory.CreateItem(itemData.itemInfo.ID) as GenericItemInfo;
inventoryModel.itemCounts[itemData.slotIndex] = itemData.itemCount;
}
return inventoryModel;
}
역직렬화 과정
3.1) SerializableInventoryModel → InventoryModel 변환
ConvertToInventoryModel()함수에서 JSON 데이터를 InventoryModel로 변환.JsonUtility.FromJson<SerializableInventoryModel>()로 역직렬화.weaponDatas 리스트를 순회하며 slotIndex와 weaponInfo를 추출.InventoryModel.weaponDatas의 Dictionary에 추가.itemDatas 리스트를 순회하며 slotIndex, itemInfo, itemCount를 추출.InventoryModel.itemDatas와 itemCounts에 각각 추가.3.2) InventoryModel 로드
LoadInventoryModel()
함수에서 JSON 파일로부터 데이터를 읽어 InventoryModel로 변환.
InvenDataFilePath)이 존재하는지 확인.null 반환.SerializableInventoryModel로 역직렬화.ConvertToInventoryModel()로 변환하여 InventoryModel로 변환 후 반환.LoadInventoryModel()함수에서 JSON 파일로부터 데이터를 읽어 InventoryModel로 변환.InvenDataFilePath)이 존재하는지 확인.null 반환.SerializableInventoryModel로 역직렬화.ConvertToInventoryModel()로 변환하여 InventoryModel로 변환 후 반환. public InventoryModel LoadInventoryModel()
{
if (!System.IO.File.Exists(InvenDataFilePath))
{
Logger.Log("저장된 인벤토리가 없습니다.");
return null;
}
string json = System.IO.File.ReadAllText(InvenDataFilePath);
var serializableModel = JsonUtility.FromJson<SerializableInventoryModel>(json);
Logger.Log("InventoryModel 불러오기 완료");
return ConvertToInventoryModel(serializableModel);
}