using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class InventoryInfo
{
    public List<ItemInfo> itemInfos; 


    /// <summary>
    /// called only for new users
    /// </summary>
    public void Init()
    {
        this.itemInfos = new List<ItemInfo>();
    }
}​
[
  {
    "id": 100,
    "name": "axe",
    "type": 0,
    "sprite_name": "equip_axe_0",
    "sell_price": 1000
  },
  {
    "id": 101,
    "name": "bracelet",
    "type": 2,
    "sprite_name": "equip_bracelet",
    "sell_price": 1100
  },
  {
    "id": 102,
    "name": "cyristal",
    "type": 4,
    "sprite_name": "equip_cyristal",
    "sell_price": 500
  },
  {
    "id": 103,
    "name": "dragon_tooth",
    "type": 4,
    "sprite_name": "equip_dragon_tooth",
    "sell_price": 300
  },
  {
    "id": 104,
    "name": "eye_blue",
    "type": 4,
    "sprite_name": "equip_eye_blue",
    "sell_price": 2000
  },
  {
    "id": 105,
    "name": "fish",
    "type": 4,
    "sprite_name": "equip_fish",
    "sell_price": 50
  },
  {
    "id": 106,
    "name": "gem_red",
    "type": 4,
    "sprite_name": "equip_gem_red",
    "sell_price": 800
  },
  {
    "id": 107,
    "name": "hammer",
    "type": 0,
    "sprite_name": "equip_hammer_0",
    "sell_price": 1000
  },
  {
    "id": 108,
    "name": "hard_tooth",
    "type": 4,
    "sprite_name": "equip_hard_tooth",
    "sell_price": 300
  },
  {
    "id": 109,
    "name": "leaf",
    "type": 4,
    "sprite_name": "equip_leaf",
    "sell_price": 60
  },
  {
    "id": 110,
    "name": "posion_tooth",
    "type": 4,
    "sprite_name": "equip_posion_tooth",
    "sell_price": 500
  },
  {
    "id": 111,
    "name": "potion_blue",
    "type": 6,
    "sprite_name": "equip_potion_blue_0",
    "sell_price": 100
  },
  {
    "id": 112,
    "name": "potion_green",
    "type": 6,
    "sprite_name": "equip_potion_green_3",
    "sell_price": 400
  },
  {
    "id": 113,
    "name": "potion_red",
    "type": 6,
    "sprite_name": "equip_potion_red_4",
    "sell_price": 500
  },
  {
    "id": 114,
    "name": "ring_blue",
    "type": 3,
    "sprite_name": "equip_ring_blue",
    "sell_price": 2000
  },
  {
    "id": 115,
    "name": "ring_gold",
    "type": 3,
    "sprite_name": "equip_ring_gold",
    "sell_price": 3000
  },
  {
    "id": 116,
    "name": "shield_blue",
    "type": 1,
    "sprite_name": "equip_shield_blue",
    "sell_price": 1800
  },
  {
    "id": 117,
    "name": "shield_wood",
    "type": 1,
    "sprite_name": "equip_shield_wood",
    "sell_price": 900
  },
  {
    "id": 118,
    "name": "stone",
    "type": 4,
    "sprite_name": "equip_stone",
    "sell_price": 100
  },
  {
    "id": 119,
    "name": "tooth_red",
    "type": 4,
    "sprite_name": "equip_tooth_red",
    "sell_price": 800
  },
  {
    "id": 120,
    "name": "key_bronze",
    "type": 5,
    "sprite_name": "icon_itemicon_key_bronze",
    "sell_price": -1
  },
  {
    "id": 121,
    "name": "key_gold",
    "type": 5,
    "sprite_name": "icon_itemicon_key_gold",
    "sell_price": -1
  },
  {
    "id": 122,
    "name": "key_silver",
    "type": 5,
    "sprite_name": "icon_itemicon_key_silver",
    "sell_price": -1
  }
]
{"itemInfos":[{"id":121,"amount":2},{"id":120,"amount":4},{"id":101,"amount":1},{"id":115,"amount":1},{"id":107,"amount":1},{"id":108,"amount":2},{"id":104,"amount":2},{"id":105,"amount":1},{"id":122,"amount":1},{"id":119,"amount":1}]}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ItemData
{
    public int id;
    public string name;
    public int type;
    public string sprite_name;
    public int sell_price;
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ItemInfo
{
    public int id;
    public int amount;

    public ItemInfo(int id, int amount = 1)
    {
        this.id = id;
        this.amount = amount;
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Newtonsoft.Json;
using System.Linq;

public class DataManager
{
    public static readonly DataManager instance = new DataManager();

    private Dictionary<int, ShopData> dicShopData;// = new Dictionary<int, ShopData>();
    private Dictionary<int, ItemData> dicItemData;

    private DataManager()
    {

    }

    public ShopData GetShopData(int id)
    {
        return this.dicShopData[id];
    }

    public void LoadShopData()
    {
        TextAsset asset = Resources.Load<TextAsset>("Data/shop_data");  //경로를 포함한 확장자를 뺀 이름 

        var json = asset.text;

        ShopData[] arrShopDatas = JsonConvert.DeserializeObject<ShopData[]>(json);       

        this.dicShopData = arrShopDatas.ToDictionary(x => x.id);    //새로운 사전 객체가 생성 반환 
    }

    public List<ShopData> GetShopDatas()
    {
        foreach(var data in dicShopData)
        {
            Debug.LogFormat("!!!{0}", data.Value.sprite_name);
        }
        return this.dicShopData.Values.ToList();
    }

    public void LoadItemData()
    {
        TextAsset asset = Resources.Load<TextAsset>("Data/item_data");
        string json = asset.text;

        Debug.Log(json);

        ItemData[] arr = JsonConvert.DeserializeObject<ItemData[]>(json);

        this.dicItemData = arr.ToDictionary(x => x.id);
    }
    
    public ItemData GetItemData(int id)
    {
        if (this.dicItemData.ContainsKey(id))
        {
            return this.dicItemData[id];
        }

        return null;
    }

    public ItemData GetRandomItemData()
    {
        var randId = Random.Range(0, this.dicItemData.Count) + 100;
        return GetItemData(randId);
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Newtonsoft.Json;
using System.IO;

public class InfoManager
{
    public static readonly InfoManager instance = new InfoManager();

    public InventoryInfo InventoryInfo { get; set; }

    private InfoManager()
    {

    }
    
    public void SaveInventoryInfo()
    {
        var path = string.Format("{0}/inventory_info.json", Application.persistentDataPath);

        var json = JsonConvert.SerializeObject(this.InventoryInfo);

        File.WriteAllText(path, json);
    }

    public void LoadInventoryInfo()
    {
        var path = string.Format("{0}/inventory_info.json", Application.persistentDataPath);
        var json = File.ReadAllText(path);

        this.InventoryInfo = JsonConvert.DeserializeObject<InventoryInfo>(json);
        Debug.Log("<color=yellow>[load success] inventory_info.json</color>");
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class UIEnums
{
   public enum eItemType
   {
        Weapon, Shield, Armor, Accessory, Material, Quest, Potion
   }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using Newtonsoft.Json;

public class GridScrollViewMain : MonoBehaviour
{
    public UIGridScrollViewDirector director;

    void Start()
    {
        DataManager.instance.LoadItemData();

        string path = string.Format("{0}/inventory_info.json", Application.persistentDataPath);

        if (File.Exists(path))
        {
            string json = File.ReadAllText(path);
            var inventoryInfo = JsonConvert.DeserializeObject<InventoryInfo>(json);

            InfoManager.instance.InventoryInfo = inventoryInfo;
        }
        else
        {
            var inventoryInfo = new InventoryInfo();
            inventoryInfo.Init();

            string json = JsonConvert.SerializeObject(inventoryInfo);
            Debug.Log(json);

            File.WriteAllText(path, json);

            InfoManager.instance.InventoryInfo = inventoryInfo;
        }

        this.director.Init();
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class UIGridScrollViewDirector : MonoBehaviour
{
    public UIGridScrollView scrollView;
    public Button btnTestGetitem;

    public UIPopupItemDetail popupDetail;

    public void Init()
    {
        this.popupDetail.onSell = (id) => {
            //Debug.LogFormat("<color=magenta>Sell Item : {0}</color>", id);

            this.SellItem(id);
        };

        this.scrollView.onFocus = (id) => {

            var foundInfo = InfoManager.instance.InventoryInfo.itemInfos.Find(x => x.id == id);

            //Debug.LogFormat("<color=yellow>[UIGridScrollViewDirector] onFocus : \t id :{0}, amount: {1}</color>", id, foundInfo.amount);

            this.popupDetail.Init(id).Open();
        };

        this.popupDetail.btnClose.onClick.AddListener(() => {
            this.popupDetail.Close();
        });

        this.btnTestGetitem.onClick.AddListener(() =>
        {
            var data = DataManager.instance.GetRandomItemData();

            var id = data.id;
            var foundInfo = InfoManager.instance.InventoryInfo.itemInfos.Find(x => x.id == id);

            if(foundInfo == null)
            {
                ItemInfo info = new ItemInfo(id);
                InfoManager.instance.InventoryInfo.itemInfos.Add(info);
            }
            else
            {
                foundInfo.amount++;
            }

            InfoManager.instance.SaveInventoryInfo();

            this.scrollView.Refresh();
        });

        this.scrollView.Init();
    }

    private void SellItem(int id)
    {
        var info = InfoManager.instance.InventoryInfo.itemInfos.Find(x => x.id == id);

        //Debug.LogFormat("팔려고 하는 아이템 id : {0}, amount: {1}", info.id, info.amount);   //118, 3

        if (info.amount > 1)
        {
            --info.amount;
        }
        else
        {
            //지우고 
            InfoManager.instance.InventoryInfo.itemInfos.Remove(info);
            //close 
            this.popupDetail.Close();
        }

        //저장 
        InfoManager.instance.SaveInventoryInfo();

        //리스트 새로 고침 
        this.scrollView.Refresh();
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class UIGridScrollView : MonoBehaviour
{
    public Transform content;
    public GameObject cellviewPrefab;
    public GameObject txtNoItemsGo;

    private const int THRESHOLD = 1;

    private UIGridCellView currentFocusCellView;
    public System.Action<int> onFocus;

    public void Init()
    {
        int myItemAmount = InfoManager.instance.InventoryInfo.itemInfos.Count;

        this.CreateCellViews();

        this.GetComponent<ScrollRect>().vertical = myItemAmount > THRESHOLD;
    }

    public void Refresh()
    {
        foreach (Transform child in this.content)
        {
            Destroy(child.gameObject);
        }           

        this.CreateCellViews();

        this.txtNoItemsGo.SetActive(InfoManager.instance.InventoryInfo.itemInfos.Count == 0);

        this.GetComponent<ScrollRect>().vertical = InfoManager.instance.InventoryInfo.itemInfos.Count > THRESHOLD;
    }

    private void CreateCellViews()
    {
        for (int i = 0; i < InfoManager.instance.InventoryInfo.itemInfos.Count; i++)
        {
            var go = Instantiate(this.cellviewPrefab, this.content);
            var cellview = go.GetComponent<UIGridCellView>();
            var btn = go.GetComponent<Button>();
            btn.onClick.AddListener(() =>
            {
                Debug.Log(cellview.id);
                if(this.currentFocusCellView != null)
                {
                    this.currentFocusCellView.Focus(false);
                }

                cellview.Focus(true);
                this.currentFocusCellView = cellview;

                this.onFocus(this.currentFocusCellView.id);
            });

            var info = InfoManager.instance.InventoryInfo.itemInfos[i];
            var data = DataManager.instance.GetItemData(info.id);
            var atlas = AtlasManager.instance.GetAtlasByName("UIItemIcon");
            var sprite = atlas.GetSprite(data.sprite_name);
            var amount = info.amount;
            cellview.Init(info.id, sprite, amount);
        }
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;


public class UIGridCellView : MonoBehaviour
{
    public Image imgIcon;
    public GameObject foucsGo;
    public TMP_Text txtAmount;
    public int id;
    
    public void Init(int id, Sprite spirte, int amount)
    {
        this.id = id;
        this.imgIcon.sprite = spirte;
        this.imgIcon.SetNativeSize();
        this.txtAmount.text = amount.ToString();
        this.txtAmount.gameObject.SetActive(amount > 1);  
    }

    public void Focus(bool active)
    {
        this.foucsGo.SetActive(active);
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;

public class UIPopupItemDetail : MonoBehaviour
{
    public TMP_Text txtItemType;
    public Image imgItemIcon;
    public TMP_Text txtItemName;
    public Button btnSell;
    public TMP_Text txtSellPrice;
    public Button btnClose;

    public System.Action<int> onSell;
    private int id;

    /// <summary>
    /// Must be called before opening
    /// </summary>
    public UIPopupItemDetail Init(int id)
    {
        this.id = id;
        var data = DataManager.instance.GetItemData(id);
        var type = (UIEnums.eItemType)data.type;
        this.txtItemType.text = type.ToString();

        var atlas = AtlasManager.instance.GetAtlasByName("UIItemIcon");
        var sprite = atlas.GetSprite(data.sprite_name);
        this.imgItemIcon.sprite = sprite;

        this.txtItemName.text = data.name;

        if(data.sell_price != -1)
        {
            this.btnSell.onClick.AddListener(OnSellActionHandler);
            this.txtSellPrice.text = string.Format("{0}", data.sell_price);
            this.btnSell.gameObject.SetActive(true);
        }        
        else
        {
            this.btnSell.gameObject.SetActive(false);
        }

        return this;
    }

    private void OnSellActionHandler()
    {
        this.onSell(this.id);
    }

    public void Open()
    {
        this.gameObject.SetActive(true);       
    }

    public void Close()
    {
        this.gameObject.SetActive(false);

        this.btnSell.onClick.RemoveListener(OnSellActionHandler);
    }
}

+ Recent posts