0420 UI Inventory

2021. 4. 20. 14:16unity

 

Atlas: 아이템 이미지들을 한 장으로 통합한다.

objects for Packing에다 스프라이트 전체를 끌어와 넣은 후, Pack Preview에서 확인하면 된다.
Allow Rotation 항목은 체크 해제

using UnityEngine;
using UnityEngine.UI;


public class UIItem : MonoBehaviour
{
    public int id;
    public Image icon;
    public Button btn;
    public Text txtCount;

    public void Init(int id,Sprite icon, int amount) 
    {
        this.id = id;
        this.icon.sprite = icon;
        this.txtCount.text = amount.ToString();
    }
}
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.U2D;

public class Test : MonoBehaviour
{
    public GameObject[] arrEmpty;
    private List<UIItem> items;

    private GameObject prefab;

    public Transform empty;
    public SpriteAtlas atlas;

    // Start is called before the first frame update
    void Start()
    {
        this.prefab = Resources.Load<GameObject>("UIItem");
        this.items = new List<UIItem>();

        this.AddItem(100);
        this.AddItem(200);
        //this.GetItem(100);
        

    }

    public void AddItem(int id) 
    {
        //배열에 해당하는 아이템의 스프라이트를 보여준다
        var parent = this.arrEmpty[this.items.Count];
        var go = Instantiate<GameObject>(this.prefab, parent.transform);
        var uiItem = go.GetComponent<UIItem>();
        this.items.Add(uiItem);
        if (id == 100)
        {
            Sprite sp = this.atlas.GetSprite("inventory_item_clover");
            uiItem.Init(id, sp, 4);
        }
        else if (id == 200) 
        {
            Sprite sp = this.atlas.GetSprite("inventory_item_gift");
            uiItem.Init(id, sp, 3);
        }
        
    }
    public void GetItem(int id) 
    {
        var uiItem = this.items.Find(x => x.id == id);
        if (uiItem != null) 
        {
            for (int i = 0; i < this.arrEmpty.Length; i++) 
            {
                var go = this.arrEmpty[i];
                if (go.transform.childCount > 0) 
                {
                    var child = go.transform.GetChild(0);
                    if (child != null) 
                    {
                        var target = child.GetComponent<UIItem>();
                        if (target.id == id) 
                        {
                            Debug.Log(target.id);
                            Destroy(target.gameObject);
                            this.items.Remove(uiItem);
                            break;
                        }
                    }
                }
                
            }
        }
    }
}

using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.U2D;

public class UIInventory : MonoBehaviour
{
    public GameObject[] arrEmpty;
    private List<UIItem> items;
    private GameObject prefab;

    public Transform empty;
    public SpriteAtlas atlas;

    // Start is called before the first frame update
    void Start()
    {
        this.prefab = Resources.Load<GameObject>("UIITem");
        this.items = new List<UIItem>();
        this.AddItem(100);
        this.AddItem(101);
    }

    public void AddItem(int id) 
    {
        var parent = this.arrEmpty[this.items.Count];
        var go = Instantiate<GameObject>(this.prefab, parent.transform);
        var uiItem = go.GetComponent<UIItem>();
        var data = DataManager.GetInstance().dicItemData[id];

        uiItem.btn.onClick.AddListener(() =>
        {
            Debug.LogFormat("{0}, {1}", data.name, data.desc);
        });
        this.items.Add(uiItem);
        string spName = data.sprite_name;
        Sprite sp = this.atlas.GetSprite(spName);
        uiItem.Init(id, sp, 3);
    }

    private void GetItem(int id)
    {
        var uiItem = this.items.Find(x => x.id == id);
        if (uiItem != null)
        {
            for (int i = 0; i < this.arrEmpty.Length; i++)
            {
                var go = this.arrEmpty[i];
                if (go.transform.childCount > 0)
                {
                    var child = go.transform.GetChild(0);
                    if (child != null)
                    {
                        var target = child.GetComponent<UIItem>();
                        if (target.id == id)
                        {
                            Debug.Log(target.id);

                            Destroy(target.gameObject);
                            this.items.Remove(uiItem);

                            break;
                        }
                    }
                }
            }
        }
    }

}
using System.Collections.Generic;
using UnityEngine;
using Newtonsoft.Json;
using System.Linq;

public class DataManager
{
    private static DataManager instance;
    public Dictionary<int, ItemData> dicItemData;

    //생성자
    private DataManager() 
    {
    }

    public static DataManager GetInstance() 
    {
        if (DataManager.instance == null) 
        {
            DataManager.instance = new DataManager();
        }
        return DataManager.instance;
    }

    public void LoadData() 
    {
        var json = Resources.Load<TextAsset>("itemData").text;
        var arrItemDatas = JsonConvert.DeserializeObject<ItemData[]>(json);
        foreach (var data in arrItemDatas) 
        {
            Debug.LogFormat("{0}, {1}, {2}", data.id, data.name, data.desc);
        }
        this.dicItemData = arrItemDatas.ToDictionary(x => x.id);
    }
}
using UnityEngine;
using UnityEngine.UI;


public class UIItem : MonoBehaviour
{
    public int id;
    public Image icon;
    public Button btn;
    public Text txtCount;

    public void Init(int id,Sprite icon, int amount) 
    {
        this.id = id;
        this.icon.sprite = icon;
        this.txtCount.text = amount.ToString();
    }
}

 

 

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

public class Test : MonoBehaviour
{
    public GameObject[] arrEmpty;
    private List<UIItem> items;

    private GameObject prefab;

    public Transform empty;
    public SpriteAtlas atlas;

    // Start is called before the first frame update
    void Start()
    {
        this.prefab = Resources.Load<GameObject>("UIItem");
        this.items = new List<UIItem>();

        this.AddItem(100);
        this.AddItem(200);
        this.GetItem(100);
        

    }

    public void AddItem(int id) 
    {
        //배열에 해당하는 아이템의 스프라이트를 보여준다
        var parent = this.arrEmpty[this.items.Count];
        //parent.transform으로 위치를 지정해야 프리팹이 empty슬롯 크기에 맞게 들어간다.
        var go = Instantiate<GameObject>(this.prefab, parent.transform);
        var uiItem = go.GetComponent<UIItem>();
        this.items.Add(uiItem);
        if (id == 100)
        {
            Sprite sp = this.atlas.GetSprite("inventory_item_clover");
            uiItem.Init(id, sp, 4);
        }
        else if (id == 200) 
        {
            Sprite sp = this.atlas.GetSprite("inventory_item_gift");
            uiItem.Init(id, sp, 3);
        }
        
    }
    public void GetItem(int id) 
    {
        var uiItem = this.items.Find(x => x.id == id);
        if (uiItem != null) 
        {
            for (int i = 0; i < this.arrEmpty.Length; i++) 
            {
                var go = this.arrEmpty[i];
                if (go.transform.childCount > 0) 
                {
                    var child = go.transform.GetChild(0);
                    if (child != null) 
                    {
                        var target = child.GetComponent<UIItem>();
                        if (target.id == id) 
                        {
                            Debug.Log(target.id);
                            Destroy(target.gameObject);
                            this.items.Remove(uiItem); 
                            break;
                        }
                    }
                }
            }
            for (int i = 0; i < this.arrEmpty.Length; i++)
            {
                if (this.arrEmpty[i] == null)
                {
                    this.items.Add(uiItem);
                    return;
                }
            }
        }
    }
}

drive.google.com/file/d/1xqB8UPaIPUY-C9e8h5N0pHru2_IWLY1F/view?usp=sharing

 

'unity' 카테고리의 다른 글

0421 UI Stage  (0) 2021.04.21
0420 Item Pick up+인벤토리 연동  (0) 2021.04.20
0419 UI  (0) 2021.04.19
0416 페이드 인으로 씬 전환  (0) 2021.04.16
0416 Saving Data in Unity  (0) 2021.04.16