针对这种情况,给出一个解法,假设我们已经编辑好了n个Button对象:
public class UISkillPanel : MonoBehaviour
{
// Use this for initialization
void Start()
{
// 取得的玩家技能数据
List<SpellInfo> spellList = LocalPlayerData.Inst.SpellList;
for (int i = 0; i < spellList.Count; i++)
{
SpellInfo spell = spellList[i];
GameObject btnGO = GameObject.Find(string.Format("SkillButton{0}", i));
//-- 使用匿名delegate处理按钮事件
Button btn = btnGO.GetComponent<Button>();
btn.onClick.AddListener(
delegate()
{
this.onSkillButtonClick(spell);
}
);
}
}
void onSkillButtonClick(SpellInfo spell)
{
Debug.Log(string.Format("Spell Button Clicked : {0}.", spell.Name));
}
}此写法动态传入spell参数,也解决了按钮与技能对应的关系问题。:)
Unity 4.6的使用匿名delegate处理uGUI控件事件绑定
原文地址:http://blog.csdn.net/neil3d/article/details/41463671