事件
public class Shijian : MonoBehaviour
{
// Start is called before the first frame update
public delegate void click();//定义委托
public static event click cl;//定义事件
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.F))
{
cl();//执行事件
}
}
}
**********
public class Yidong : MonoBehaviour
{
// Start is called before the first frame update
private void OnEnable()
{
Shijian.cl += Teleport;//将方法订阅到事件
}
void Start()
{
}
private void OnDisable()
{
Shijian.cl -= Teleport;
}
// Update is called once per frame
void Update()
{
}
void Teleport()
{
Vector3 pos = transform.position;
pos.y = Random.Range(1.0f, 3.0f);//随机值1到3之间
transform.position = pos;
}
}