美文网首页
Unity对象池封装

Unity对象池封装

作者: 白水SR | 来源:发表于2018-04-24 15:07 被阅读30次

    一共分为两个类,SubPool与ObjectPool,SubPool类为总对象池包含ObjectPool的子池子,ObjectPool包含需要入池的对象。

    调用方法:

    • 入池:ObjectPool.Instance.Spawn()
    • 出池:ObjectPool.Instance.UnSpawn()
    • 全部出池:ObjectPool.Instance.UnSpawnAll()
    • 销毁池子:ObjectPool.Instance.DisPool();

    SubPool类(继承至单例父类)

    using UnityEngine;
    using System.Collections;
    using System.Collections.Generic;
    
    public class SubPool
    {
        List<GameObject> Pool=new List<GameObject>();
    
        /// <summary>
        /// 入池
        /// </summary>
        public GameObject Spawn(string obj)
        {
                foreach (GameObject item in Pool)
                {
                    if (!item.activeSelf)
                    {
                        item.SetActive(true);
                        item.SendMessage("Spawn",SendMessageOptions.DontRequireReceiver);
                        return item;
                    }
                }
            GameObject go = CreateObj(obj);
            Pool.Add(go);
           go.SendMessage("Spawn",SendMessageOptions.DontRequireReceiver);
            return go;
        }
    
    /// <summary>
    /// 出池
    /// </summary>
    /// <param name="go"></param>
     public void UnSpawn(GameObject go=null,string goname="")
        {
            if (go!=null)
            {
                go.SetActive(false);
            }
            else
            {
                for (int i = 0; i < Pool.Count; i++)
                {
                    if (Pool[i].activeSelf)
                    {
                        Pool[i].SetActive(false);
                        return;
                    }
                }
            }
        }
    
    /// <summary>
    /// 全部出池
    /// </summary>
    public void UnSpawnAll()
        {
            foreach (GameObject item in Pool)
            {
                item.SetActive(false);
            }
        }
    
        /// <summary>
        /// 创建对象
        /// </summary>
        GameObject CreateObj(string obj)
        {
            GameObject go = GameObject.Instantiate(Resources.Load("Prefabs/Others/"+obj) as GameObject);
            go.transform.SetParent(GameObject.Find("Pool").transform, false);
            go.name = go.name.Replace("(Clone)", "");
            return go;
        }
    }
    
    

    ObjectPool类

    using UnityEngine;
    using System.Collections;
    using System.Collections.Generic;
    using Framework;
    
    public class ObjectPool:Manager<ObjectPool>
    {
        ObjectPool()
        {
        }
    
        Dictionary<string,SubPool> pools= new Dictionary<string, SubPool>();
        
        /// <summary>
        /// 入池
        /// </summary>
        /// <param name="obj">创建的对象名</param>
        public GameObject Spawn(string obj)
        {
                if (pools.ContainsKey(obj))
                {
                    return pools[obj].Spawn(obj);
                }
                else
                {
                    SubPool sp = new SubPool();
                   var go = sp.Spawn(obj);
                    pools.Add(obj, sp);
                    return go;
                }
            return null;
        }
    
        /// <summary>
        /// 出池
        /// </summary>
        /// <param name="go">需要出池对象</param>
        public void UnSpawn(GameObject go=null,string goName="")
        {
            if (!go)
            {
                if (pools.ContainsKey(goName))
                {
                    pools[goName].UnSpawn(null,goName);
                }
            }
            else
            {
                if (pools.ContainsKey(go.name))
                {
                    pools[go.name].UnSpawn(go);
                }
            }
    
        }
    
        /// <summary>
        /// 全部出池
        /// </summary>
        /// <param name="pool">出池的名称</param>
        public void UnSpawnAll(string pool)
        {
            if (pools.ContainsKey(pool))
            {
                pools[pool].UnSpawnAll();
            }
        }
    
        /// <summary>
        /// 销毁池子
        /// </summary>
        /// <param name="pool">销毁池的名称</param>
        public void DisPool(string pool)
        {
            if (pools.ContainsKey(pool))
            {
                pools.Remove(pool);
            }
        }
    
    
    }
    
    

    相关文章

      网友评论

          本文标题:Unity对象池封装

          本文链接:https://www.haomeiwen.com/subject/diadlftx.html