1.全部显示背包的所有格子及所有物品
2.分类显示该类物品及该类物品个数的格子
3.不管是哪个面板的装备进行了更新,其余面板都应更新
4.数量显示当前格子数量和总数量
例如
从装备Tab装备一个装备至角色身上
装备分页该装备要消失
并且在全部分页中也应该对应消失
1.有两个背包容器,一个为全部的物品,格子数量初始化固定,另一个为部分的物品,格子数量动态变化,切换标签页时对应隐藏背包容器
2.新建一个PartItemList用来存放分页的物品
3.每次切换标签的时候,进行计算,从ItemList里面进行筛选出对应分类的Item,添加至PartItemList
4.遍历PartItemList添加格子和物品,更新数量
5.当ItemList的Item发生变化时,即添加或移除物品,也要执行3和4步骤
1.复制一份背包容器的UI,并且重新命名为pnlScrollPartView
2.新建存放分页Item的List
private Dictionary<int, ItemStatus> m_ItemList; //背包中的Item
private List m_PartItemList; //背包分页中的Item
3.新建一个枚举值,存放当前所点击的标签,默认为All
private enum TabType
{
All,
Equip,
Potion,
Other
}
private TabType m_TabType = TabType.All;
4.添加向分页添加物品及刷新的方法
///
/// 向分页添加物品
///
private void AddItemToPartGrid(List itemList)
{
//初始化分页格子数量为PartItemList的数量
InitGrid(this.m_PnlPartGrid, itemList.Count, this.m_PartGridList);
for (int i = 0; i < itemList.Count; i++)
{
GameObject go = GameObject.Instantiate(this.m_ItemPrefab);
go.transform.SetParent(this.m_PartGridList[i]);
go.GetComponent().SetInfo(itemList[i]);
go.transform.localPosition = Vector3.zero;
go.transform.localScale = Vector3.one;
go.GetComponent().PointerClick += OnUIItemPointerClick;
}
}
///
/// 刷新分页
///
private void RefreshPartGrid()
{
switch (this.m_TabType)
{
case TabType.Equip:
this.m_PartItemList = m_ItemList
.Select(i => i.Value).Cast()
.Where(d =>
d.WhereType == WhereType.Knapsack
&& (d.ItemType == ItemType.Weapon
|| d.ItemType == ItemType.Armor
|| d.ItemType == ItemType.Ring
|| d.ItemType == ItemType.Necklace)
).ToList();
break;
case TabType.Potion:
m_PartItemList = m_ItemList
.Select(i => i.Value).Cast()
.Where(d =>
d.WhereType == WhereType.Knapsack
&& d.ItemType == ItemType.Potion
).ToList();
break;
case TabType.Other:
m_PartItemList = m_ItemList
.Select(i => i.Value).Cast()
.Where(d =>
d.WhereType == WhereType.Knapsack
&& d.ItemType == ItemType.Other
).ToList();
break;
}
if(this.m_PartItemList != null)
AddItemToPartGrid(this.m_PartItemList);
UpdateCount();
}
private void UpdateCount()
{
string countStr;
if (this.m_TabType == TabType.All)
{
countStr = string.Format("{0} / {1}", this.m_ItemList.Count, gridCount);
}
else
{
countStr = string.Format("{0} / {1}", this.m_PartItemList.Count, gridCount);
}
this.m_txtCount.text = countStr;
}
5.在标签点击事件里更改m_TapType属性值,并进行取值刷新
private void BtnEquipPointerClick(PointerEventData obj)
{
this.m_PnlGrid.parent.gameObject.SetActive(false);
this.m_PnlPartGrid.parent.gameObject.SetActive(true);
this.m_TabType = TabType.Equip;
RefreshPartGrid();
}
private void BtnPotionPointerClick(PointerEventData obj)
{
this.m_PnlGrid.parent.gameObject.SetActive(false);
this.m_PnlPartGrid.parent.gameObject.SetActive(true);
this.m_TabType = TabType.Potion;
RefreshPartGrid();
}
private void BtnOtherPointerClick(PointerEventData obj)
{
this.m_PnlGrid.parent.gameObject.SetActive(false);
this.m_PnlPartGrid.parent.gameObject.SetActive(true);
this.m_TabType = TabType.Other;
RefreshPartGrid();
}
private void BtnAllPointerClick(PointerEventData obj)
{
this.m_PnlGrid.parent.gameObject.SetActive(true);
this.m_PnlPartGrid.parent.gameObject.SetActive(false);
this.m_TabType = TabType.All;
UpdateCount();
}
6.在AddItem和RemoveItem中也添加刷新方法
public void AddItem(UserItemEntity item)
{
Transform grid = GetEnmptyGrid();
if (grid == null) //背包已满
return;
if (!m_ItemEntityList.ContainsKey(item.ID))
return;
GameObject go = GameObject.Instantiate(this.m_ItemPrefab);
go.transform.SetParent(grid);
ItemStatus itemStatus = go.GetComponent().SetInfo(item);
go.transform.localPosition = Vector3.zero;
go.transform.localScale = Vector3.one;
go.GetComponent().PointerClick += OnUIItemPointerClick;
if (m_ItemList.ContainsKey(itemStatus.ItemID))
{
m_ItemList[itemStatus.ItemID] = itemStatus;
}
else
{
m_ItemList.Add(itemStatus.ItemID, itemStatus);
}
RefreshPartGrid();
}
public void RemoveItem(ItemStatus itemStatus)
{
itemStatus.Count -= 1;
if (itemStatus.Count == 0)
{//销毁物品
this.m_ItemList.Remove(itemStatus.ItemID);
DestroyImmediate(itemStatus.gameObject);
}
else
{//更新数量显示
itemStatus.gameObject.GetComponent().UpdateCount();
}
RefreshPartGrid();
}
就是重新初始化背包
private void BtnRestorePointerClick(PointerEventData obj)
{
InitGrid(this.m_PnlGrid, gridCount, m_GridList);
LoadData();
}