对Unity3D中对象池的尝试

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

//*******************************
//脚本功能:对象池
//挂载对象:
//*******************************
namespace ObjPool
{
    public class Pool : MonoBehaviour
    {
        [System.Serializable]//序列化
        public struct PreFabsInfor
        {
            //预制体
            public GameObject Prefab;
            //对象数量
            public int PrefabsCount;
            //构造
            public PreFabsInfor(GameObject Prefab, int PrefabsCount)
            {
                this.Prefab = Prefab;
                this.PrefabsCount = PrefabsCount;
            }
        }
        public List<PreFabsInfor> prefabsInfor = new List<PreFabsInfor>();
        //每个结构体对应一个父节点
        private List<Transform> PrefabsParentNote = new List<Transform>();


        void Awake()
        {
            this.name = "GameObjectPool";

            //实例化所有父节点
            for (int i = 0; i < prefabsInfor.Count; ++i)
            {
                //实例化父节点
                GameObject obj = new GameObject(string.Format("{0}_ParentNote", prefabsInfor[i].Prefab.name));
                //将父节点添加到链表中
                PrefabsParentNote.Add(obj.transform);
                //将这个空物体作为所有父节点的父节点
                obj.transform.SetParent(transform);
            }
        }

        void Start()
        {
            //GameObject obj = Resources.Load("Prefabs/Capsule") as GameObject;
            //GameObject omae = Instantiate(obj);

            CreatGameobject(prefabsInfor, PrefabsParentNote);
        }

        void CreatGameobject(List<PreFabsInfor> prefabsInfo, List<Transform> prefabsParentNote)
        {
            for (int i = 0; i < prefabsInfo.Count; ++i)
            {
                CreatGameobjectByCount(prefabsInfo[i].Prefab, prefabsParentNote[i], prefabsInfo[i].PrefabsCount);
            }
        }

        //根据输入的数量实例化预制体并且将他们都绑定到对应父节点上
        void CreatGameobjectByCount(GameObject prefabs, Transform prefabsParentNote, int prefabCount)
        {
            for (int i = 0; i < prefabCount; ++i)
            {
                GameObject sample = null;
                sample = Instantiate(prefabs) as GameObject;
                sample.name = prefabs.name;
                sample.transform.SetParent(prefabsParentNote);
                //让所有实例化的预制体都不要显示
                sample.SetActive(false);
            }
        }

        //根据某个对象名,找到相应父节点并且取出其一个子节点,子节点显示后就可以使用
        public static GameObject GetOneGameobjectInPool(string ObjectName)
        {
            GameObject father = null;
            father = GameObject.Find(string.Format("{0}_ParentNote", ObjectName));
            GameObject theChild = null;
            //如果没有子节点则从预制体里创建10个推入内存,并且作为子节点挂到这个父节点
            if (father.transform.childCount == 0)
            {
                Pool pool = new Pool();
                //静态方法不要去利用类内变量,所以这里从文件夹里找,相对路劲在Resources文件夹下
                pool.CreatGameobjectByCount(Resources.Load(string.Format("Prefabs/{0}", ObjectName)) as GameObject, father.transform, 10);
            }
            theChild = father.transform.GetChild(0).gameObject;
            return theChild;
        }

        //将一个节点隐藏
        public static void RecoveryGameobject(GameObject obj)
        {
            GameObject father = null;
            father = GameObject.Find(string.Format(string.Format("{0}_ParentNote", obj.name)));
            obj.transform.SetParent(father.transform);
            if (obj.activeSelf)
                obj.SetActive(false);
        }
    }

}


Unity 3D是有自带的对象池的,而且好像功能超强大。

但毕竟现在是在学习阶段,我觉得什么工具类都用别人的= =那和咸鱼有什么区别?

根据别人的指导自己写了个。

上面那个脚本挂在空物体上就OK了,运行前记得在图形界面给结构体赋值。

PS.写完后记得在Edit->project settings->Script Execution Order调整脚本运行时间,对象池最好相对普通的脚本运行时间要早一些。

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值