对象池1(方法功能)PoolOption

2.对象池PoolOption(方法功能)

//单类型缓冲对象管理(单模池操作管理)功能: 激活、收回、预加载等。

namespace kernal

{

    [System.Serializable]

    public class PoolOption

    {

         public GameObject Prefab;                           //存储的“预设”

         public int IntPreLoadNumber = 0;                      //初始缓冲数量

         public int IntAutoDeactiveGameObjectByTime = 30;       //按时间自动禁用游戏对象

        [HideInInspector]

         //活动使用的游戏对象集合

        public List<GameObject> ActiveGameObjectArray = new List<GameObject>(); 

        [HideInInspector]

         //非活动状态(禁用)的游戏对象集合

        public List<GameObject> InactiveGameObjectArray = new List<GameObject>(); 

            private int _Index = 0;

    // 预加载

         internal GameObject PreLoad(GameObject prefab, Vector3 positon, Quaternion rotation)

        {

            GameObject obj = null;

            if (prefab)

            {

                obj = Object.Instantiate(prefab, positon, rotation) as GameObject;

                Rename(obj);

                obj.SetActive(false);          //设置非活动状态

                //加入到“非活动游戏对象”集合中。

                InactiveGameObjectArray.Add(obj);

            }

            return obj;

        }

        // 激活游戏对象

        internal GameObject Active(Vector3 pos, Quaternion rot)

        {

            GameObject obj;

            if (InactiveGameObjectArray.Count != 0)

            {

                //从“非活动游戏集合”容器中取出下标为0的游戏对象

                obj = InactiveGameObjectArray[0];

                //从“非活动游戏集合”容器中移除下标为0的游戏对象

                InactiveGameObjectArray.RemoveAt(0);

            }

            else

            {

                //“池”中没有多余的对象,则产生新的对象

                obj = Object.Instantiate(Prefab, pos, rot) as GameObject;

                //新的对象进行名称“格式化”处理

                Rename(obj);

            }

            //对象的方位处理

            obj.transform.position = pos;

            obj.transform.rotation = rot;

            //新对象正式加入“活动池”容器中。

            ActiveGameObjectArray.Add(obj);

            obj.SetActive(true);

            return obj;

        }

        // 禁用游戏对象

        internal void Deactive(GameObject obj)

        {

            ActiveGameObjectArray.Remove(obj);

            InactiveGameObjectArray.Add(obj);

            obj.SetActive(false);

        }

        // 统计两个“池”中所有对象的数量

        internal int totalCount

        {

            get

            {

                int count = 0;

                count += this.ActiveGameObjectArray.Count;

                count += this.InactiveGameObjectArray.Count;

                return count;

            }

        }

        // 全部清空集合(两个“池”)

        internal void ClearAllArray()

        {

            ActiveGameObjectArray.Clear();

            InactiveGameObjectArray.Clear();

        }

        // 彻底删除所有“非活动”集合容器中的游戏对象。

        internal void ClearUpUnused()

        {

            foreach (GameObject obj in InactiveGameObjectArray)

            {

                Object.Destroy(obj);

            }

            InactiveGameObjectArray.Clear();

        }

                 // 游戏对象重命名,对新产生的游戏对象做统一格式处理,目的是做“时间戳”处理。

        private void Rename(GameObject instance)

        {

            instance.name += (_Index + 1).ToString("#000");

            //游戏对象(自动禁用)时间戳  [Adding]

            instance.name = IntAutoDeactiveGameObjectByTime + "@" + instance.name;

            _Index++;

        }

        // 删除“非活动”容器集合中的一部分指定数量数据

        internal void DestoryCount(int count)

        {

            if (count > InactiveGameObjectArray.Count)

            {

                ClearUpUnused();

                return;

            }

                   for (int i = InactiveGameObjectArray.Count - 1; i >= InactiveGameObjectArray.Count - count; i--)

            {

                Object.Destroy(InactiveGameObjectArray[i]);

            }

                 InactiveGameObjectArray.RemoveRange(InactiveGameObjectArray.Count - count, count);

        }

        // 回调函数、时间管理、所有游戏对象进行时间倒计时管理,时间小于零则进行“非活动”容器集合中,即:按时间自动回收游戏对象。

        internal void AllActiveGameObjectTimeSubtraction()

        {

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

            {

                string strHead = null;

                string strTail = null;

                int intTimeInfo = 0;

                GameObject goActiveObj = null;

                goActiveObj = ActiveGameObjectArray[i];

                //得到每个对象的时间戳

                string[] strArray = goActiveObj.name.Split('@');

                strHead = strArray[0];

                strTail = strArray[1];

                //时间戳-10 处理

                intTimeInfo = System.Convert. (strHead);

                if (intTimeInfo >= 10)

                {

                    strHead = (intTimeInfo - 10).ToString();

                }

                else if (intTimeInfo <= 0)

                {

                    //游戏对象自动转入禁用

                         goActiveObj.name = IntAutoDeactiveGameObjectByTime.ToString() + "@" + strTail;

                    this.Deactive(goActiveObj);

                    continue;

                }

                //时间戳重新生成

                goActiveObj.name = strHead + '@' + strTail;

            }

        }

}

注:这段对象池代码是对象池的功能方法,基础的实现方式,加载、显示、隐藏、时间控制等

支持个人观看使用,如商用或转载,请告知! -----萧朗(QQ:453929789 Email:xiaolang_xl@sina.com)
原文地址:https://www.cnblogs.com/XiaoLang0/p/9626304.html