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 goodsList = new List(); 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); } }); } /// /// 按钮点击改变按钮的颜色 /// /// /// 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().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().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; } }