• itemPool和MessageCenter


        为了方便,事先写了两个脚本,itemPool和MessageCenter。都是很常用的,一个管理废弃gameObj的利用,一个管理massege的订阅发送。贴代码:

      1 using UnityEngine;
      2 using System.Collections.Generic;
      3 
      4 //对象池 
      5 public class ItemPool{
      6 
      7     // 缓存对象池  
      8     public static Dictionary<string, ItemOfPool> m_itemList = new Dictionary<string, ItemOfPool>();
      9  
     10     // 添加一个对象组   
     11     public static void PushList(string name)
     12     {
     13         if (m_itemList != null)
     14             Debug.Log("添加对象组"+ name);
     15             m_itemList.Add(name, new ItemOfPool(name));
     16     }
     17 
     18     // 添加单个对象(首先寻找对象组->添加单个对象)  
     19     public static void PushObject(string name, GameObject gameObject)
     20     {
     21         if (m_itemList != null)
     22         {
     23             if (!m_itemList.ContainsKey(name))
     24                 PushList(name);
     25             m_itemList[name].PushObject(gameObject);
     26         }
     27     }
     28 
     29     // 移除单个对象,真正销毁  
     30     public static void RemoveObject(string name, GameObject gameObject)
     31     {
     32         if (m_itemList == null || !m_itemList.ContainsKey(name))
     33             return;
     34         m_itemList[name].RemoveObject(gameObject);
     35     }
     36 
     37     // 取出池中的某个对象   
     38     public static GameObject GetObject(string name)
     39     {
     40         name = name +"(Clone)";
     41         if (m_itemList == null || !m_itemList.ContainsKey(name))
     42         {
     43             Debug.Log(m_itemList == null);
     44             Debug.Log(m_itemList.ContainsKey(name));
     45             return null;
     46         }
     47         return m_itemList[name].GetObject();
     48     }
     49  
     50     // 销毁对象,没有真正销毁   
     51     public static void DestroyActiveObject(string name, GameObject gameObject)
     52     {
     53         if (m_itemList == null || !m_itemList.ContainsKey(name))
     54         {
     55             PushList(name);
     56         }
     57         m_itemList[name].DestoryObject(gameObject);
     58     } 
     59 
     60     // 销毁对象组,真正销毁  
     61     public static void Destroy()
     62     {
     63         if (m_itemList == null)
     64         {
     65             return;
     66         }
     67         foreach (ItemOfPool poolI in m_itemList.Values)
     68         {
     69             poolI.Destory();
     70         }
     71         m_itemList = null;
     72     }
     73 }
     74 
     75 
     76 //作为对象池的基本单位 被ItemPool引用
     77 public class ItemOfPool
     78 {
     79 
     80     public string m_name; //标识名称  
     81     public List<GameObject> m_objectList;// 对象列表
     82 
     83     //新建一种物品表
     84     public ItemOfPool(string name)
     85     {
     86         this.m_name = name;
     87         this.m_objectList = new List<GameObject>();
     88     }
     89 
     90     //添加某对象到同一种物品表里
     91     public void PushObject(GameObject gameObject)
     92     {
     93         if (!this.m_objectList.Contains(gameObject))
     94         {
     95             this.m_objectList.Add(gameObject);
     96             Debug.Log("将一个对象" + gameObject.name + "加入池中, 此类共有" + m_objectList.Count);
     97         }
     98         else
     99         {
    100             Debug.LogError("此物品已存在池中");
    101         }
    102     }
    103 
    104     // 销毁对象,没有真正销毁  
    105     public void DestoryObject(GameObject gameObject)
    106     {
    107         PushObject(gameObject);
    108         gameObject.SetActive(false);
    109         //gameObject.transform.position = Vector3.zero;
    110     }
    111 
    112     // 取出池中本类的第一个对象  
    113     public GameObject GetObject()
    114     {
    115         if (this.m_objectList == null || this.m_objectList.Count == 0)
    116         {
    117             return null;
    118         }
    119         GameObject obj = m_objectList[0];
    120         m_objectList.RemoveAt(0);
    121         obj.SetActive(true);
    122         return obj;
    123     }
    124 
    125     // 移除并销毁单个对象,真正的销毁  
    126     public void RemoveObject(GameObject gameObject)
    127     {
    128         if (m_objectList.Contains(gameObject))
    129         {
    130             m_objectList.Remove(gameObject);
    131             GameObject.Destroy(gameObject);
    132         }
    133     }
    134 
    135     // 销毁对象,把所有的同类对象全部删除,真正的销毁对象!!  
    136     public void Destory()
    137     {
    138         if (this.m_objectList == null || this.m_objectList.Count == 0)
    139             return;
    140         foreach (GameObject poolItem in this.m_objectList)
    141         {
    142             GameObject.Destroy(poolItem);
    143         }
    144         this.m_objectList = new List<GameObject>();
    145     }
    146 }

    还有messageCenter:

     1 using System.Collections.Generic;
     2 
     3 
     4 public class MessageCenter {
     5 
     6     public delegate void MessageDelivery(KeyValues keyValues);
     7 
     8     public static Dictionary<string, MessageDelivery> m_messageDic = new Dictionary<string, MessageDelivery>();
     9 
    10     //添加监听事件(和监听消息)
    11     public static void AddMessageListener(string messageType, MessageDelivery handler)
    12     {
    13         if (!m_messageDic.ContainsKey(messageType))
    14         {
    15             m_messageDic.Add(messageType, null);
    16         }
    17         m_messageDic[messageType] += handler;
    18     }
    19 
    20     //移除监听事件
    21     public static void RemoveMessageListener(string messageType, MessageDelivery handler)
    22     {
    23         if (m_messageDic.ContainsKey(messageType))
    24         {
    25             m_messageDic[messageType] -= handler;
    26         }
    27     }
    28 
    29     //移除监听消息
    30     public static void RemoveMessageType(string messageType)
    31     {
    32         if (m_messageDic.ContainsKey(messageType))
    33         {
    34             m_messageDic.Remove(messageType);
    35         }
    36     }
    37 
    38     //移除所有监听事件
    39     public static void ClearAllMessageListener()
    40     {
    41         if(m_messageDic != null)
    42             m_messageDic.Clear();
    43     }
    44 
    45     //发送消息
    46     public static void SendMessage(string messageType, KeyValues keyValues)
    47     {
    48         MessageDelivery delivery;
    49         if (m_messageDic.TryGetValue(messageType, out delivery))
    50         {
    51             if (delivery != null)
    52             {
    53                 delivery(keyValues);
    54             }
    55         }
    56     }
    57 }
    58 
    59 public class KeyValues {
    60     private string _key;
    61     private object _values;
    62 
    63     public string Key
    64     {
    65         get {return _key;}
    66     }
    67 
    68     public object Values
    69     {
    70         get {return _values;}
    71     }
    72 
    73     public KeyValues(string key, object values)
    74     {
    75         _key = key;
    76         _values = values;
    77     }
    78     public void UpdateKeyValues(string key,object values) {
    79         _key = key;
    80         _values = values;
    81     }
    82 }

    这里我的key是string, value是object  可能会改  看情况吧。

  • 相关阅读:
    Orcle数据库查询练习复习:二
    Orcle数据库查询练习复习:一
    Xamarin.Android 入门之:xamarin使用webserver和html交互
    Xamarin.Android 入门之:Listview和adapter
    Xamarin.Android 入门之:Android的生命周期
    Xamarin.Android 入门之:Android API版本设置
    Xamarin.Android 入门之:Xamarin快速入门
    阶段01Java基础day16集合框架02
    阶段01Java基础day13常见对象02
    阶段01Java基础day10面向对象05
  • 原文地址:https://www.cnblogs.com/charsoul/p/8696589.html
Copyright © 2020-2023  润新知