반응형
SMALL

- 인벤토리 추가 구현 사항
- 인벤토리를 세부적으로 관리할수 있는 메서드들의 추가
- 장비 cell 프리팹 추가기능 메서드
- 콘텐츠 그리드 활성화 체크 메서드
- 인벤토리 정렬 메서드 (GIF 에서도 보다시피 정렬이 조금 원하던 대로 되지 않아 수정이 필요)
- 장비 버리기 메서드 (실제로장비를 버릴시 유저의 능력치 하락 (일시적인 능력치 하락)
- 장비 추가 메서드 (실제로 장비 장착시 일시적인 유저의 능력치 증가)
- 그외의 장비 가 있는 셀을 터치시 팝업 구현 (* GIF 에서는 보이지 않는다)
- 인벤토리를 세부적으로 관리할수 있는 메서드들의 추가
코드
UIInventory 스크립트 ( 아직 App 스크립트를 만들지 않아 인포 매니저의 유저의 정보 로드를 대신 하고 있다)
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using System.IO;
using UnityEngine.UI;
public class UIInventory : MonoBehaviour
{
[SerializeField]
private ScrollRect contentGridRect;
[SerializeField]
private Transform content;
[SerializeField]
private GameObject equipmentCell;
private List<GameObject> inventory;
private EquipmentFactory equipmentFactory;
[SerializeField]
private UIPlayerStats uiPlayerStats;
[SerializeField]
private UIInventoryDetailPopup uIInventoryDetailPopup;
public void Init()
{
//초기 설정
string path = string.Format("{0}/Inventory_Info.json", Application.persistentDataPath);
if (File.Exists(path))
{
InfoManager.instance.LoadInventoryInfo();
}
else
{
InfoManager.instance.SaveInventoryInfo();
}
this.inventory = new List<GameObject>();
this.equipmentFactory = this.GetComponent<EquipmentFactory>();
var count = InfoManager.instance.inventoryInfo.InventoryCount;
this.InitInventoryCells(count);
this.CheckContentGrid(count);
this.uIInventoryDetailPopup.Init();
}
public void InitInventoryCells(int count)
{
for (int i = 0; i < count; i++)
{
var equipmentCell = GameObject.Instantiate(this.equipmentCell, this.content);
this.inventory.Add(equipmentCell);
}
}
private void CheckContentGrid(int count)
{
if (count < 16)
{
this.contentGridRect.enabled = false;
}
else
{
this.contentGridRect.enabled = true;
}
}
public void AddInventoryCells()
{
var count = InfoManager.instance.IncreaseInventoryCount();
var equipmentCell = GameObject.Instantiate(this.equipmentCell, this.content);
this.inventory.Add(equipmentCell);
this.CheckContentGrid(count);
}
public void SorthInventoryCells()
{
var count = InfoManager.instance.inventoryInfo.InventoryCount;
for (int i = 0; i < count; i++)
{
if (this.content.GetChild(i).transform.childCount == 1)
{
this.content.GetChild(i).SetAsLastSibling();
}
}
}
public void DiscardEquipment(int cellHash)
{
var count = InfoManager.instance.inventoryInfo.InventoryCount;
for (int i = 0; i < count; i++)
{
if (this.inventory[i].GetHashCode() == cellHash)
{
var equipment = this.inventory[i].transform.GetChild(1).GetChild(1);
var euipmentComp = equipment.GetComponent<Equipment>();
euipmentComp.UnSetEquipmentStat();
this.uiPlayerStats.UpdatePlayerStatUI();
Destroy(this.inventory[i].transform.GetChild(1).gameObject);
break;
}
}
this.SorthInventoryCells();
}
public void AddEquipment(string name)
{
var count = InfoManager.instance.inventoryInfo.InventoryCount;
GameObject cell = null;
for (int i = 0; i < count; i++)
{
if (this.content.GetChild(i).childCount == 1)
{
cell = this.content.GetChild(i).gameObject;
break;
}
}
if (cell != null)
{
var go = this.equipmentFactory.MakeEquipment(name, cell);
this.uiPlayerStats.UpdatePlayerStatUI();
}
else
{
Debug.Log("장비를 더 이상 지닐수 없습니다.");
}
}
// 테스트용
public void Update()
{
if (Input.GetKeyDown(KeyCode.Q))
{
this.AddEquipment("Iron_Sword");
}
else if (Input.GetKeyDown(KeyCode.W))
{
this.AddEquipment("Diamond_Wand");
}
else if (Input.GetKeyDown(KeyCode.E))
{
this.AddEquipment("Gold_Arrow");
}
else if (Input.GetKeyDown(KeyCode.R))
{
this.AddEquipment("Wood_Axe");
}
else if (Input.GetKeyDown(KeyCode.T))
{
for(int i = 0; i < 1000; i++) {
var index = Random.Range(0, this.inventory.Count);
if (this.inventory[index].transform.childCount == 2)
{
var hesh = this.inventory[index].GetHashCode();
this.DiscardEquipment(hesh);
break;
}
else continue;
}
}
else if (Input.GetKeyDown(KeyCode.Y))
{
this.AddInventoryCells();
}
}
}
세세한 조정이 필요한 부분이 있지만 전체적인 기능 구현은 문제가 없기에 자세한 마무리는 다음 포스팅에 올리는 걸로...
반응형
LIST
'프로젝트 > 건즈앤 레이첼스' 카테고리의 다른 글
| [유니티 프로젝트] 로그라이크 허브 마을 (기억의 성소) 제작 .1 (1) | 2023.04.15 |
|---|---|
| [유니티 프로젝트] 인벤토리 스탯창 구현 5 - 완료 (1) | 2023.04.13 |
| [GPGS] 구글 개발자 등록 방법 (1) | 2023.04.11 |
| [유니티 프로젝트] 인벤토리 스탯창 구현 .2 - 유저 스탯창 마무리 (0) | 2023.04.10 |
| [유니티 프로젝트] 인벤토리 스탯창 구현 .1 (1) | 2023.04.09 |