Unity 按鈕Button的非拖拽標準寫法
不要在界面上拖拽。太繁瑣。
這么寫
public class ClickExample : MonoBehaviour {?
private void Avake(){
? ? private void Awake()
? ? {
? ? ? ? Button btn = this.GetComponent<Button>().GetComponent<Button>();
? ? ? ? btn.onClick.AddListener(TaskOnClick);
? ? }
? ? void TaskOnClick()
? ? {
? ? ? ? Debug.Log("You have clicked the button!");
? ? }
}
標簽: