using UnityEngine;
[CreateAssetMenu(fileName = "New Item", menuName = "Inventory/Item")]
[System.Serializable]
public class Item : ScriptableObject {
new public string name = "New Item";
public Sprite icon = null;
public bool isDefaultItem = false;
public int ID;
public int itemAmount = 0;
public virtual void Use ()
{
//Something Use
}
public void RemoveFromInventory()
{
Inventory.instance.Remove(this);
}
}
이게 스크립터블 아이템 오브젝트
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
[System.Serializable]
public class Inventory : MonoBehaviour
{
#region Singleton
public static Inventory instance;
private void Awake()
{
if (instance != null)
{
Debug.LogWarning("More than one instance of Inventory found!");
return;
}
instance = this;
}
#endregion
public delegate void OnItemChanged();
public OnItemChanged onItemChangedCallback;
public int space = 6;
public static List<Item> inventory = new List<Item>();
public List<Item> items = new List<Item>();
public void InventorySaved()
{
BinaryFormatter bf = new BinaryFormatter();
FileStream fs = File.Create(Application.persistentDataPath + "/Inventory.dat");
List<int> saveId = new List<int>();
saveId = items[0].ID; // 오류 부분
bf.Serialize(fs.saveId);
fs.Close();
if (onItemChangedCallback != null)
{
onItemChangedCallback.Invoke();
}
}
public void InventoryLoaded()
{
if(File.Exists(Application.persistentDataPath + "/Inventory.dat"))
{
BinaryFormatter bf = new BinaryFormatter();
FileStream fs = File.Open(Application.persistentDataPath + "/Inventory.dat", FileMode.Open);
List<int> loadId = (List<int>)bf.Deserialize(fs);
fs.Close();
}
if (onItemChangedCallback != null)
{
onItemChangedCallback.Invoke();
}
}
/* 세이브/로드와 무관
public void Add(Item item)
{
if (!item.isDefaultItem)
{
if (items.Count >= space)
{
Debug.Log("Not enough room.");
return;
}
items.Add(item);
item.itemAmount++;
if (onItemChangedCallback != null)
{
onItemChangedCallback.Invoke();
}
}
}
public void Remove(Item item)
{
items.Remove(item);
item.itemAmount--;
if (onItemChangedCallback != null)
{
onItemChangedCallback.Invoke();
}
}
public bool CheckForItem(Item item)
{
if (items.Contains(item))
{
return true;
}
else
{
return false;
}
}
public bool InventoryCheck(Item item)
{
if(items.Contains(item))
{
return true;
}
else
{
return false;
}
}
public void InventoryReset()
{
inventory.Clear();
}
public void ItemAmountReset()
{
for(int i = 0; i<items.Count; i++)
{
items[i].itemAmount = 0;
}
}
} */
나도 튜토리얼보고 따라하는 따라쟁이 수준이라서 굉장히 못해. 직접 코드 따는 프로그래머가 아닌 빼끼는 코더 수준이니까.
그래서 일주일째 인벤토리로 헤매고 있음. 안개속에서 헤엄치는 기분이랄까.
인벤토리 세이브/로드 기능이 제일 큰 난관이다 ㅠ..
다음 게임은 기본 클래스에 넣던가해야지..
foreach(Item element in items) { saveId.Add(element.ID); }
아이템의 데이터는 직렬화 못시키니까 대신 아이템들의 id를 빼와서 그걸로 리스트를 채워야겟지? 아이템은 id를 한개 가지고 있고 너가 채우고 싶은게 id의 리스트라면 아이템의 리스트를 쭉 돌아다니면서 id값만 빼와서 id리스트에 넣어야지.
근데 플밍을 배우는 단계면 사실 이렇게 복잡한 구조 짜려고 할 필요가 있을까 싶긴 하네. 계속 막히면 간단한 구조로 바꾸는걸 추천함.