Unity观察者模式-委托事件delegate使用

观察者模式概念:

观察者模式是“设计模式”中的一种模式,也就做“发布-订阅模式”。

和单例模式一样,都属于设计模式的范畴,设计模式是解决一类

固定问题的代码套路。

观察者模式Observer定义了一种一对多的依赖关系,让多个观察者对象

同时监听某一个主题对象。这个主题对象在状态发生改变时,会通知所有观察者

对象,使他们能够自动更新自己。

这样讲很模糊,我举个例子:

我在B站追了一部番《鬼灭之刃》, “追番” 的这个动作,就相当于 “绑定” 委托方法;

被观察者---B站:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

//定义委托方法类型.
public delegate void BLDelegage(string info);

/// <summary>
/// B站
/// </summary>
public class bilibiliDemo : MonoBehaviour
{
    //单例访问字段.
    public static bilibiliDemo Instance;

    //定义B站 追番按钮.
    public event BLDelegage blDel;

    private string info = "您追的《鬼灭之刃》更新啦";

    /// <summary>
    /// 单例化
    /// </summary>
    private void Awake()
    {
        Instance = this;
    }

    private void Update()
    {
        //空格 模拟 追番按钮.
        if (Input.GetKeyDown(KeyCode.Space))
        {
            //发送消息给追番的用户.
            blDel(info);
        }
    }
}

观察者---用户:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// B站用户---我
/// </summary>
public class Me : MonoBehaviour
{
    void Start()
    {
        //Lambda表达式写法.可以不用,但起码要看得懂.
        //bilibiliDemo.Instance.blDel += info => ZhuiFanInfo(info);

        //匿名类写法.
        bilibiliDemo.Instance.blDel += delegate (string info)
        {
            Debug.Log(gameObject.name + info);
        };
    }

    /// <summary>
    /// 接收B站追番消息
    /// </summary>
    private void ZhuiFanInfo(string info)
    {
        Debug.Log(gameObject.name + info);
    }
}

    //观察者 将自身的方法 添加到 被观察者的事件中,
    //当 被观察者使用时,将调用消息 发送给 观察者,通知观察者调用自身方法.

    //我的理解:
    //“总控制” 需要调用另一个脚本的 "方法A",
    //就创建一个委托事件,用于存储 另一个脚本的 "方法A".
    //另一个脚本把 "方法A" 传递(绑定) 给“总控制”,方便“总控制”调用 "方法A".

其实这样的例子有很多,比如我最近做的一个项目有这样一个需求,

当敌人死亡后,需要 通知 角色 调用自身的 “停止攻击”方法,

如果用 获取对象身上的脚本,然后再调用对方的公开方法,这样就很麻烦,

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

//定义委托类型.
public delegate void DeathTimeDelegate();

/// <summary>
/// 敌人
/// </summary>
public class Enemy : MonoBehaviour
{
    public static Enemy Instance;

    /// <summary>
    /// 定义委托方法
    /// 告知对方本体已死亡
    /// 调用对方的方法
    /// </summary>
    public event DeathTimeDelegate DeathTimeDel;

    private void Awake()
    {
        Instance = this;
    }
    
    /// <summary>
    /// 本体死亡
    /// </summary>
    public void Die()
    {
        if (HP <= 0)
        {
            DeathTimeDel();
            Destroy(gameObject, 0.2f);
            Debug.Log(transform.name + "已死亡");
        }
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 角色
/// </summary>
public class Player : Piece
{
    private void Awake()
    {
        //绑定到委托方法中.
        Enemy.Instance.DoingThingsDel += () => StopAtk();
    }

    /// <summary>
    /// 停止攻击
    /// </summary>
    private void StopAtk()
    {
        Debug.Log("停止攻击");
    }
}

又或者 子物体 调用 父物体的方法SendMessageUpwards(string parentName);

用这个方法也可以,用委托也可以。

如果想知道Lambda表达式的语法或者委托事件更详细的内容,可以看我上一篇帖:

https://blog.csdn.net/weixin_55532142/article/details/124389259?spm=1001.2014.3001.5502

猜你喜欢

转载自blog.csdn.net/weixin_55532142/article/details/124390719