//********************************************************************
// 文件名: DelegateTest.cs
// 描述: 玩家
// 作者: 李伟
// 创建时间: 2015-09-16
//
//********************************************************************
using UnityEngine;
using System.Collections;
using System;
public delegate void DoHurt();
/// <summary>
/// 委托事件模式,就是监听者模式的实现。
/// 一句话解释就是:“我对这个感兴趣,你变化的时候通知我一声,我就不实时关注了”。
/// </summary>
public class DelegateTest : MonoBehaviour
{
void Start()
{
AddDoHurtEvent(myHurt001);
AddDoHurtEvent(myhurt002);
AddDoHurtEvent(myhurt003);
OnDoHurtEvent();
RemoveDoHurtEvent(myhurt002);
OnDoHurtEvent();
}
/// <summary>
/// 定义一个DoHurt的事件
/// </summary>
private event DoHurt DoHurtEventHanlde;
/// <summary>
/// 执行事件
/// </summary>
void OnDoHurtEvent()
{
if (DoHurtEventHanlde != null) DoHurtEventHanlde();
}
/// <summary>
/// 添加事件
/// </summary>
/// <param name="doHurt"></param>
void AddDoHurtEvent(DoHurt doHurt)
{
DoHurtEventHanlde += doHurt;
}
/// <summary>
/// 移除事件
/// </summary>
/// <param name="doHurt"></param>
void RemoveDoHurtEvent(DoHurt doHurt)
{
DoHurtEventHanlde -= doHurt;
}
public void myHurt001()
{
Debug.Log("调用1");
}
public void myhurt002()
{
Debug.Log("调用2");
}
public void myhurt003()
{
Debug.Log("调用3");
}
}
Unity Delegate Event
最新推荐文章于 2024-03-06 11:02:24 发布