using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Inventory : MonoBehaviour
{
ย ย public static Inventory instance;
ย ย [SerializeField]
ย ย public List<Item> items = new List<Item>();
ย ย private void Awake()
ย ย {
ย ย ย ย instance = this;
ย ย }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
[System.Serializable]
public enum ItemType
{
ย ย Equipment,
ย ย Consumables,
ย ย Etc
}
[System.Serializable]
public class Item
{
ย ย public ItemType itemType;
ย ย public string itemName;
ย ย public Sprite itemImage;
ย ย public int itemNumber;
ย ย public bool Use()
ย ย {
ย ย ย ย return false;
ย ย }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
public class savedata : MonoBehaviour
{
ย ย public Item item = new Item();
ย ย public void SaveInventory()
ย ย {
ย ย ย ย string json = JsonUtility.ToJson(Inventory.instance.items);
ย ย ย ย string filePath = Application.persistentDataPath + "/savetest.json";
ย ย ย ย File.WriteAllText(filePath, json);
ย ย ย ย Debug.Log("saved");
ย ย }
}
items ์ ์์๋ก ๊ฐ์ ๋ฃ์ด๋ ์ด๋ ๊ฒ ์ ์ฅ์ด ๋๋๋ฐ ์ด๊ฑฐ ์ด์ ๋ ํด๊ฒฐ๋ฐฉ๋ฒ ์์๋ ๋ถ ์๋์ ์ด์ ๋ง์ด๋ผ๋ ์ ๋ฐ..
ํผ์ ์ ํ๋ธ ๋ณด๋ฉด์ ๋ฐฐ์ธ๋ ค๋๊น ํ๋๋ค์
JSONUtility.ToJson์ List๋ฅผ ์ง์ serializeํ๋๊ฑธ ์ง์ ์ํจ. wrapper class ํ๋ ๋ง๋ค์ด์ ๊ทธ๊ฑธ serialize์ํค๋ฉด๋จ. [System.Serializable] public class SomeClass { public List someItems; } ๋ญ ์ด๋ฐ์์ด๊ฒ ์ง
๋์จ๊ฐ < ๊ดํธ๋ค ๋ค ๋ ๋ ค๋ฒ๋ ธ์ผ๋ ๊ฐ์ํด์ ์ฝ์ผ์
[System.Serializable] public class test { public List items = new List(); } ์ด๋ฐ์์ผ๋ก ํ๋๊ฑฐ ๋ง์์? ์ด๋ ๊ฒ ํด๋ ํ์ผ ๊ฐ๋ณด๋ฉด {"items":[]} ์ด๊ฑฐ๋ฐ์ ์ ๋จ๋๋ฐ ..๋ญ๊ฐ ์๊ธฐ๊ธดํ๋๋ฐ..
๋ด ๊ธฐ์ต์ enum์ ๋ฐ๋ก ์ง๋ ฌํ๋ฅผ ๋ค๋ฅด๊ฒ ํด์ค์ผํ์์ด enum ๋นผ๋ณด์ค?
์ ๋ ๊ทธ ๋ฌธ์ ์ธ๊ฐ ์ถ์ด์ ๋นผ๋ณด๊ธฐ๋ ํ๊ณ int๋ก ํด๋ดค์๋๋ฐ ๋๊ฐ์์ด์
์ ์ด์ ๋ญ๊ฐ ์๋ค์ด๊ฐ.. Int๋ string ๋ง ๋จ๊ฒจ๋๋ ๊ฐ์์
๋ฆฌ์คํธ ๋นผ๋ ๊ทธ๋ ๊ฒ ๋ค์ด๊ฐ? ์ผ๋จ ์์ธ๊ฐ์ ๊ฑด ํ๋์ฉ ์ ๊ฑฐํด๋ณด๊ธฐ ใฑใฑ