CultivateImmortal/Assets/Scripts/GameScene/UI/BagPanel.cs

177 lines
4.5 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class BagPanel : BasePanel
{
//按钮初始颜色是白色 点击后变成灰色
private string imgColor = "6A6A6A";
//按钮字体初始颜色 灰青
private string txtColor1 = "225555";
//按钮字体点击颜色 浅灰
private string txtColor2 = "CDDACF";
public Button btnAll;
public Button btnMater;
public Button btnPill;
public Button btnGong;
public Text txtAll;
public Text txtMater;
public Text txtPill;
public Text txtGong;
public Text txtStone;
public Text txtYu;
public Text txtInfo;
public ScrollRect sv;
//物品预设体
public GameObject btnMaterial;
//选择的按钮
private Button btnChoose;
//选择的文字
private Text txtChoose;
private UserGoodsInfo goods;
//装备集合,用于更新页面
private List<GameObject> goodsList = new List<GameObject>();
public override void Init()
{
btnChoose = btnAll;
txtChoose = txtAll;
goods=GameDataMgr.Instance.userGoods;
UpdatePanel();
//全部按钮
btnAll.onClick.AddListener(() =>
{
//不是当前按钮才更新
if (btnChoose != btnAll)
{
//更新界面
UpdateAllGoods();
}
});
//材料按钮
btnMater.onClick.AddListener(() =>
{
//不是当前按钮才更新
if (btnChoose != btnMater)
{
//更新界面
//设置按钮颜色
SetColor(btnMater, txtMater);
UpdateGoods(2);
}
});
//丹药按钮
btnPill.onClick.AddListener(() =>
{
//不是当前按钮才更新
if (btnChoose != btnPill)
{
//更新界面
//设置按钮颜色
SetColor(btnPill, txtPill);
UpdateGoods(1);
}
});
//功法按钮
btnGong.onClick.AddListener(() =>
{
//不是当前按钮才更新
if (btnChoose != btnGong)
{
//更新界面
//设置按钮颜色
SetColor(btnGong, txtGong);
UpdateGoods(3);
}
});
}
/// <summary>
/// 按钮点击改变按钮的颜色
/// </summary>
/// <param name="btn"></param>
/// <param name="txt"></param>
public void SetColor(Button btn, Text txt)
{
//设置颜色前先把上一次的按钮颜色重置
if (btnChoose != null && txtChoose != null)
{
btnChoose.image.color = Color.white;
txtChoose.color = GameMgr.Instance.HexToColor(txtColor1);
}
//然后设置此次点击的按钮颜色
btn.image.color = GameMgr.Instance.HexToColor(imgColor);
txt.color = GameMgr.Instance.HexToColor(txtColor2);
//设置为新的按钮,方便下一次重置颜色
btnChoose = btn;
txtChoose = txt;
}
//更新面板
public void UpdatePanel()
{
UpdateAllGoods();
SetColor(btnAll, txtAll);
txtStone.text = "灵石:"+GameMgr.Instance.SetNumber(GameDataMgr.Instance.player.stone);
txtYu.text = "仙玉:"+GameMgr.Instance.SetNumber(GameDataMgr.Instance.player.yu);
}
//全部物品
private void UpdateAllGoods()
{
//先清空物品列表
for (int i = 0; i < goodsList.Count; i++)
{
Destroy(goodsList[i]);
}
goodsList.Clear();
for (int i = 0; i < goods.goods.Count; i++)
{
GameObject go = Instantiate(btnMaterial);
go.GetComponent<BtnMaterial>().UpdatePanel(goods.goods[i]);
go.transform.SetParent(sv.content, false);
goodsList.Add(go);
}
txtInfo.text = "";
}
private void UpdateGoods(int goodsType)
{
//1丹药2材料
//先清空物品列表
for (int i = 0; i < goodsList.Count; i++)
{
Destroy(goodsList[i]);
}
goodsList.Clear();
for (int i = 0; i < goods.goods.Count; i++)
{
if (goods.goods[i].goodsType == goodsType)
{
GameObject go = Instantiate(btnMaterial);
go.GetComponent<BtnMaterial>().UpdatePanel(goods.goods[i]);
go.transform.SetParent(sv.content, false);
goodsList.Add(go);
}
}
txtInfo.text = "";
}
//文字描述
public void UpdateTxtInfo(GoodsData info)
{
txtInfo.text = "物品名称:"+info.name+"\n物品描述"+info.info;
}
}