观察者模式

委托事件声明写在被观察者类(脚本)上

事件调用写在被观察者类(脚本)的被观察行为方法内部

再在观察者类(脚本)中的Update方法中写(因为观察是每帧判断执行的)调用被观察者的事件+=观察者观察到之后触发的方法;

//烧水类  被观察者
public delegate void shuikaile();

public event shuikaile shuikaileevent;

public void Heat()
{
    if (temperate < 100)
    {
        temperate += 10;
    }
        else
    {
        shuikaileevent();
        temperate = 100;
    }
}
//人类  观察者(观察水是否烧开)
public 被观察者脚本 bgcz;
bgcz.shuikaileevent += Notify;
public void Notify()
{
    Debug.Log("水开了,我来上水了!");
}

 

posted @ 2018-06-03 16:18  方是源  阅读(135)  评论(0编辑  收藏  举报
回顶部