观察者模式:

观察者模式:



using System.Collections;

using System.Collections.Generic;

using UnityEngine;


//委托就是那方法作为参数传递

public delegate void OnActionHandler(params object[] parm);

public class guanchazhemoshi : MonoBehaviour

{

       public static guanchazhemoshi instance;

       public Dictionary<ushort, List<OnActionHandler>> dict = new Dictionary<ushort, List<OnActionHandler>>();

       private void Start()

       {

              instance = this;

       }

       /// <summary>

       /// 添加

       /// </summary>

       /// <param name="shortID"></param>

       /// <param name="handhelder"></param>

       public void AddeventListener(ushort shortID, OnActionHandler handhelder)

       {//一个键对应一个委托集合,集合里都对应的键,   有这个键的话,往他的委托集合里再添加一个委托方法

              if (dict.ContainsKey(shortID))

              {//给这个键添加在委托集合里对应的委托

                     dict[shortID].Add(handhelder);

              }

              else

              {//new出来添加进去

                     List<OnActionHandler> handhelderlist = new List<OnActionHandler>();

                     handhelderlist.Add(handhelder);

                     //键等于集合里的handhelder

                     dict[shortID] = handhelderlist;//???

              }

       }

       // 删掉这个键对应的一个方法    一个键可能对应很多的委托方法

       public void RemoveAction(ushort shortID, OnActionHandler handhelder)

       {

              if (dict.ContainsKey(shortID))

              {//跟据键找到   在集合委托中对应方法的集合所有的

                     List<OnActionHandler> handhelderlist = dict[shortID];

                     //删掉这个委托方法      删掉这个键对应的一个方法

                     handhelderlist.Remove(handhelder);

                     //在集合委托中对应方法的集合所有的==0  这个键就没有存在的必要

                     if (handhelderlist.Count==0)

                     {

                           dict.Remove(shortID);

                     }

              }

              

       }

       public void DisPath(ushort shortID, params object[] parm)

       {

              if (dict.ContainsKey(shortID))

              {

                     //获取该id所对应的所有方法列表

                     List<OnActionHandler> handlerlist = dict[shortID];

                           if (handlerlist != null&& handlerlist.Count>0)

                           {

                                  for (int i = 0; i < handlerlist.Count; i++)

                                  {

                                         if (handlerlist[i]!=null)

                                         {

                                         //调用  键对应的委托方法  传入参数

                                                handlerlist[i](parm);

                                         }

                                  }

                           }

              }

       }

}

被观察类



using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class xiaoming : MonoBehaviour {

       public static xiaoming instance;

       

       void Start () {

              instance = this;

       }

       

       

       void Update () {

              if (Input.GetKeyUp(KeyCode.Q))

              {//           

                     guanchazhemoshi.instance.DisPath(1, 9);

              }

              if (Input.GetKeyUp(KeyCode.W))

              {

                     guanchazhemoshi.instance.DisPath(2,17);

              }

       }

}


观察类



using System;

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class Boss : MonoBehaviour {

       // 老板观察

       void Start () {

              guanchazhemoshi.instance.AddeventListener(1, qq);

       }

       private void qq(object[] parm)

       {//参数传递的第0 个

              Debug.Log(parm[0].ToString()+"点就到了公司了");

       }

}

猜你喜欢

转载自blog.csdn.net/Pandora12138/article/details/80997871