纳金网
标题:
Unity委托简单例子
[打印本页]
作者:
may
时间:
2015-11-30 00:59
标题:
Unity委托简单例子
Unity委托简单例子
public class EventDispatcher : MonoBehaviour
{
public delegate void EventHandler(GameObject e);//定义委托
public event EventHandler MouseOver;
void Start()
{
OnMouseOver();
}
void OnMouseOver()
{
if (MouseOver != null)
{
MouseOver(this.gameObject);//传递参数
}
}
}
public class EventDis : MonoBehaviour {
public GameObject game;
EventDispatcher EventHand;
void Start () {
EventHand = game.GetComponent<EventDispatcher>();
}
void Listener(GameObject g)
{
Debug.Log(g.name);
}
void OnMouseDown()
{
EventHand.MouseOver += Listener;//实现委托
}
}
复制代码
欢迎光临 纳金网 (http://rs.narkii.com/club/)
Powered by Discuz! X2.5