首先,引用要有如下三个
using UnityEngine;
using System.Collections;
using System;
特别是第三个,在后面的EventArgs便是第三个命名空间的。
然后,在第一个脚本中,声明两个变量
public event EventHandler GameOver;
public event EventHandler OtherAdd;事件和委托感觉用法很像,都是通过邦定然后触发。
void Update () {
if (Input.GetKeyDown(KeyCode.Q))
{
if (GameOver != null)
{
GameOver(this.gameObject, EventArgs.Empty);
}
}
if (Input.GetKeyDown(KeyCode.W))
{
if (OtherAdd != null)
{
OtherAdd(this.gameObject, EventArgs.Empty);
}
}
}然后是这个脚本中的触发事件,这个时候如果事件不为空,则事件被触发。
然后在第二个脚本中,先声明两个需要和事件绑定的函数,然后绑定
void OnGameOver(object sender, EventArgs s)
{
GameObject temp = (GameObject)sender;
print(temp.name+"i

最低0.47元/天 解锁文章
2181

被折叠的 条评论
为什么被折叠?



