구현된 인벤토리

ItemDB.Json을 통하여 ItemSlot들을 생성하여 인벤토리를 구현하였고 , ItemSlot은 현재 생성/파괴를 하고있기에 , 오브젝트 풀링을 이용하여 리팩토링 해야된다. 

 

해당 구조도 MVP패턴을 이용하여 구현하였다.

 

 

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

public class InventoryController : UIController
{
    private InventoryModel inventoryModel;
    private InventoryView inventoryView;

    public InventoryModel Model { get => inventoryModel; set => inventoryModel = value; }
    public InventoryView View { get => inventoryView; set => inventoryView = value; }

    public override void Initialize(IUIBase view, UIModel model)
    {
        inventoryModel = model as InventoryModel;
        inventoryView = view as InventoryView;

        base.Initialize(inventoryView, inventoryModel);
    }

    public override void OnShow()
    {
        view.ShowUI();
        UpdateView();   // 초기 View 갱신
    }

    public override void OnHide()
    {
        view.HideUI();
    }

    public override void UpdateView()
    {
        // Model 데이터를 기반으로 View 갱신
        view.UpdateUI();
    }
}

 

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

public class InventoryModel : UIModel
{
    public List<Item> Items = new List<Item>(); // 소지하고있는 아이템 리스트 

    public void Initilaize()
    {
        foreach (ItemDB Data in DataManager.Instance.ItemDB.ItemsDict.Values)
        {
            Item itemObj = new Item();
            itemObj.Initialize(Data);
            Items.Add(itemObj);
        }
    }

    public void AddItem(string item)
    {

    }

    public void RemoveItem(string item)
    {

    }
}
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.InputSystem.XR;

public class InventoryView : MonoBehaviour, IUIBase
{
    [SerializeField] private Transform itemSlotParent;
    [SerializeField] private ItemSlot itemSlotPrefab;
    [SerializeField] private RectTransform itemSlotBoundary;
    public InventoryController Controller;

    private List<ItemSlot> itemSlots = new List<ItemSlot>();

    public void Initialize()
    {
        if (itemSlotPrefab == null)
        {
            itemSlotPrefab = Resources.Load<ItemSlot>("Prefabs/Item/ItemSlot");
        }

        for (int i = 0; i < Controller.Model.Items.Count; i++)
        {
            itemSlots.Add(Instantiate(itemSlotPrefab, itemSlotParent));

            itemSlots[i].Initiliaze(Controller.Model.Items[i]);
        }
    }

    public void ShowUI()
    {
        Vector2 size = itemSlotBoundary.sizeDelta;
        size.y = 135 * ((itemSlots.Count / 4) + 1);
        itemSlotBoundary.sizeDelta = size;
    }

    public void HideUI()
    {
    }

    public void UpdateUI()
    {
        Debug.LogAssertion("인벤토리 UI 업데이트");
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class UIInventory : MonoBehaviour
{
    public string UIKey;

    private InventoryModel model;
    [SerializeField] private InventoryView[] views;
    private InventoryController controller;

    private void Start()
    {
        //Model(Data) 초기화
        model = new InventoryModel();
        model.Initilaize();
        GameManager.Instance._player.Inventory = model;

        //컨트롤러  초기화 및 View 등록
        controller = new InventoryController();
        for (int i = 0; i < views.Length; i++) 
        {
            views[i].Controller = controller;
            controller.Initialize(views[i], model);
        }

        //UI매니저에 UI 등록
        UIManager.Instance.RegisterController(UIKey, controller);
        gameObject.SetActive(false);
    }

    public void UIShow()
    {
        controller.OnShow();
    }
    
}

 

현재 로직만 구현 중이고 , 아직 Player 캐릭터와는 연결이 되어있지 않다. ItemStatus UI와 연결후 PlayerStat과 연동해서 값을 변경 해야 될것 같다. 

+ Recent posts