UnityEvent本质上是继承自UnityEventBase的类,它的AddListener()方法能够注册UnityAction,RemoveListener能够取消注册UnityAction,还有Invoke()方法能够一次性调用所有注册了的UnityAction。
对话框代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
public class DialogBoxMgr : MonoBehaviour {
public UnityEvent onEvOk =new UnityEvent() ;
public UnityEvent onEvCancel =new UnityEvent() ;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
public void OnBtnOk()
{
onEvOk.Invoke();
}
public void OnBtnNot()
{
onEvCancel.Invoke();
}
}
如何调用
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DialogTest : MonoBehaviour {
public DialogBoxMgr m_dialog;
// Use this for initialization
void Start () {
m_dialog.onEvOk.AddListener(() => { LogMsg("Ok"); });
m_dialog.onEvCancel.AddListener(() => { LogMsg("Cancel"); });
}
// Update is called once per frame
void Update () {
}
void LogMsg(string msg)
{
Debug.Log(msg);
}
}
下载地址 http://download.csdn.net/download/luoyikun/10030879