[代码]
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
/// <summary>
/// 调用 -- 示例
/// </summary>
public class EventExample : MonoBehaviour
{
void Start ()
{
//定义一个禽兽
TheBeast beast = new TheBeast("李雷");
//定义三个美女
new Beauty("韩梅梅", new Vector3(90.31F, 63.75F, 91.64F), beast);
new Beauty("凤姐", new Vector3(94.07F, 66.40F, 95.45F), beast);
new Beauty("AngelBaby", new Vector3(81.13F, 58.09F, 86.21F), beast);
//禽兽大喊"美女在哪?!"后执行的内容.
beast.Shouting();
}
}
/// <summary>
/// 禽兽
/// </summary>
public class TheBeast
{
//定义一个事件.
public event Action BeastShouted;
private string name; //禽兽名字
//构造
public TheBeast(string name)
{
this.name = name;
}
//禽兽行为
public void Shouting()
{
Debug.LogFormat("名为{0}的禽兽大喝一声: \"美女在哪?!\" ", name);
//触发事件
BeastShouted();
}
}
/// <summary>
/// 美女
/// </summary>
public class Beauty
{
private string name; //美女名字
private Vector3 BWH; //美女三围
//构造 -- 构造时顺便注册"禽兽大吼"的事件.
public Beauty(string name, Vector3 BWH, TheBeast qs)
{
this.name = name;
this.BWH = BWH;
//注册事件
qs.BeastShouted += Scampering;
}
public void Scampering()
{
Debug.LogFormat("三围({0})的美女{1} 边喊救命,边消失在视野中...", BWH, name);
}
}
[输出结果]