// 获取用户英雄列表 public void UpdateHeroList(ArrayList list) { Debug.Log("UpdateHeroList: " + list.Count); // 有数据,默认选择第一个 if (list.Count > 0) { GameController.instance.crtHero = list[0] as UserHeroVO; SendNotification(NotificationList.UPDATE_HERO_DATA); } // 判断HeroPanel是否处于激活状态,如果不是则返回 if (gameObject.activeSelf == false) { return; } // 移除现有的HeroItem for (int i = container.childCount - 1; i >= 0; i--) { Destroy(container.GetChild(i).gameObject); } // 添加新的Item for (int i = 0; i < list.Count; i++) { HeroItem item = Instantiate(heroItemPrefab, container); item.data = list[i] as UserHeroVO; item.GetComponent <Button>().onClick.AddListener( delegate { Select(item); } ); if (i == 0) { Select(item); } } }
public void UpdateUserHero(ArrayList heroList) { Debug.Log("HeroPanel::UpdateUserHero " + heroList.Count); // 移除content现有HeroItem for (int i = content.childCount - 1; i >= 0; i--) { Destroy(content.GetChild(i).gameObject); } // 遍历list for (int i = 0; i < heroList.Count; i++) { // 实例化HeroItem HeroItem item = Instantiate(heroItemPrefab, content); item.SetData(heroList[i] as UserHeroVO); // 添加点击事件 item.GetComponent <Button>().onClick.AddListener( delegate { Select(item); }); // x,y item.GetComponent <RectTransform>().anchoredPosition = new Vector2(94, -54 - 105 * i); } // 设置scroll view content 的高度 content.GetComponent <RectTransform>().SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, 105 * heroList.Count + 20); }
public HeroItem crtItem = null; // 当前选中Item private void Select(HeroItem item) // 点击Item时 { if (crtItem != null) // 如果当前有选中的Item { crtItem.selected = false; // 则切换为不选中 } item.selected = true; // 点击的Item 选中 crtItem = item; // 保存当前点击的Item infoText.text = crtItem.heroData.ToString(); }
/// <summary> /// HeroItem点击处理 /// </summary> /// <param name="item"></param> private void Select(HeroItem item) { // 将现在选中Item的状态切换为不选中 if (crtItem != null) { crtItem.selected = false; } // 保存item crtItem = item.GetComponent <SelectableButton>(); // 将新的item状态切换为选中 crtItem.selected = true; // 显示当前英雄信息 infoText.text = item.data.ToString(); }