演示在按钮上挂载一个脚本,(当然一般在父级面板挂载)
一、Add Component ——LoginMgr脚本,在On Click()中添加按钮与方法,如下图
二、动态连接挂载方法
需要组件名对应相应脚本中的相应方法,添加相应脚本就可以了,如下图
代码内容如下:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using UnityEngine.EventSystems;
public class LoginMgr : MonoBehaviour {
// Use this for initialization
void Start () {
Debug.Log("game is start");
var LoginBtn = gameObject.GetComponent<Button>();
if (LoginBtn.name == "Btn_Login")
{
LoginBtn.ButtonClickEvent = LoginMethod;
}
}
public void LoginMethod(BaseEventData eventData)
{
Debug.Log("开始跳转场景");
Application.LoadLevelAsync("SelectLevelSence");
Debug.LogWarning("我成功啦");
}
void Awake() {
Debug.Log(" this is LoginMgr's Awake()");
}
// Update is called once per frame
void Update () {
Debug.Log(" this is LoginMgr's Update()");
}
}