public Button[] skillButtons;// 技能按钮 private void Start() { Debug.Log( "SkillPanel::Start " + skills.Length ); // 移除content现有HeroItem for (int i = content.childCount - 1 ; i >= 0 ; i--) { Destroy( content.GetChild( i ).gameObject ); } // 遍历list for (int i = 0 ; i < skills.Length ; i++) { // 实例化HeroItem SkillItem item = Instantiate( skillItemPrefab, content ); item.SetData( skills[i] as SkillInfo ); // 添加点击事件 item.GetComponent<Button>().onClick.AddListener( delegate { Select( item ); } ); // x,y item.GetComponent<RectTransform>().anchoredPosition = new Vector2( 94, -54 - 105 * i ); // 自动选中第一个 if (i == 0) Select( item ); } // 设置scroll view content 的高度 content.GetComponent<RectTransform>().SetSizeWithCurrentAnchors( RectTransform.Axis.Vertical, 105 * skills.Length + 20 ); // 设置技能 for (int i = 0 ; i < skillButtons.Length ; i++) // 所有技能按钮 { Button b = skillButtons[i]; b.name = i + ""; b.onClick.AddListener( delegate () { SetSkill( b ); // 点击时设置技能 } ); } }
/// <summary> /// 初始化技能 /// </summary> private void InitSkill() { infoText = Find <Text>("infoText"); skillSelect = GetComponent <SkillSelect>(); for (int i = 0; i < skills.Length; i++) { SkillItem tempSkillItem = Instantiate(skillItemPrefab, container); tempSkillItem.data = skills[i] as SkillInfo; tempSkillItem.GetComponent <Button>().onClick.AddListener( delegate { Select(tempSkillItem); }); //默认选中第一个技能 if (i == 0) { Select(tempSkillItem); } } }
/// <summary> /// SkillItem点击处理 /// </summary> /// <param name="item"></param> private void Select(SkillItem item) { // 将现在选中Item的状态切换为不选中 if (crtItem != null) { crtItem.selected = false; //将上一个技能信息清空 skillSelect.SkillData = null; } // 保存item crtItem = item.GetComponent <SelectableButton>(); // 将新的item状态切换为选中 crtItem.selected = true; //将当前技能的信息保存到skillData中 skillSelect.SkillData = item.data; // 显示当前英雄信息 infoText.text = item.data.ToString(); }