动态注册按钮点击事件传参方法
public void Init(){
	string[] panelNames = Enum.GetNames (typeof(UIPanelType));

	//根据名字查找打开Panel的按钮
	for(int i = 0;i<panelNames.Length;i++){
		string ButtonName = panelNames[i]+"Btn";
		string typeName = panelNames [i];
		Button btn = GameObject.Find (ButtonName).GetComponent<Button> ();
		btn.onClick.AddListener(()=>{
			OnClick(typeName);//主要在这,用方法调用方法
		});
	}
}

public void OnClick(string type){
	UIPanelType eType =(UIPanelType) Enum.Parse (typeof(UIPanelType), type);
	UIManager.Instance.PushPanel (eType);
}

OnClick(typeName) 这里的typeName是一个确定的值,应该添加事件委托时就保存下来了

参数中出现了i值就不行了,可能说明这个值是不确定的,委托机制会在用的时候再取出,此时已经没有这些变量了,错误


首页 我的博客
粤ICP备17103704号