加载中...

Unity3D___自己做的对象池类

使用方法

//使用方法
MyPool myPool;
//定义一下
myPool = new MyPool(bullet, 5);//以子弹为例子,bullet为子弹预制体,5为初始子弹数量
//使用
  GameObject g = myPool.Pop(firePos.position, Quaternion.identity);  //从对象池中获取一个子弹对象
  g.GetComponent<Bullet>().onRecycleEvent += myPool.Push;            //添加一个可以回收的监听事件
  g.GetComponent<Bullet>().Init();                                   //在Bullet类中设置初始化

Bullet子弹代码

public interface IEnableRecycled            //接口,包含能实现一些回收的声明
{   
    public event System.Action<GameObject> onRecycleEvent;    //回收事件
    public void OnStartRecycle();                             //事件,广播
}
public class Bullet : MonoBehaviour,IEnableRecycled
{
    public event Action< GameObject> onRecycleEvent;
    public  void Init() {                            
        Invoke("IEnableRecycled.OnStartRecycle",2);        //设置两秒后回收
    }
    private void OnTriggerEnter2D(Collider2D other) {      //触发器检测,碰撞到物体后回收
        Debug.Log(other.gameObject.layer.ToString()+","+layerMask.value);
        if(1<<other.gameObject.layer==layerMask.value){
            Debug.Log("撞击后回收");
             OnStartRecycle();
        }
    }
    void IEnableRecycled.OnStartRecycle()                  //回收方法
    { 
        if(onRecycleEvent!=null){     
            Debug.Log("开始回收资源");
            onRecycleEvent(this.gameObject); 
            onRecycleEvent=null;
        }
        else{
            Debug.Log("没有需要回收的资源");
        }
    }

}

资源池源代码

public class MyPool
{
    public string poolName;
    public Queue<GameObject> queue;
    public Transform poolHolder;
    public GameObject poolPrefab;
    public MyPool(GameObject Prefab, int iniSize = 0, string Name = "", Transform Holder = null)
    {
        this.poolPrefab = Prefab;
        this.poolHolder = Holder;
        this.poolName = Name;
        if (poolPrefab == null)
        {
            Debug.LogWarning("pool");
        }
        queue = new Queue<GameObject>();
        if (poolHolder == null)                                 //生成所有资源的父节点
        {
           if(this.poolName.Equals("")) poolName = Random.Range(0, 101).ToString();
            this.poolHolder = (new GameObject("Pool-" + this.poolName)).transform;
        }
        if (iniSize > 0)                                        //预加载一些资源,备用
        {
            for (int i = 0; i < iniSize; i++)
            {
                GameObject obj = GameObject.Instantiate(this.poolPrefab, this.poolHolder);
                this.Push(obj);
            }
        }
    }
    //从队列头部取出一个资源
    public GameObject Pop()                                     
    {
        if (queue.Count == 0)
        {
            GameObject obj = GameObject.Instantiate(poolPrefab, poolHolder);
            return obj;
        }
        else
        {
            queue.Peek().SetActive(true);
            return queue.Dequeue();
        }
    }
    //从队列头部取出一个资源,并为它设置位置和旋转
    public GameObject Pop(Vector3 position, Quaternion rotation)
    {
        if (queue.Count == 0)
        {
            GameObject obj = GameObject.Instantiate(poolPrefab, position, rotation);
            obj.transform.SetParent(poolHolder);
            return obj;
        }
        else
        {
            GameObject obj = queue.Dequeue();
            obj.SetActive(true);
            obj.transform.SetPositionAndRotation(position, rotation);
            return obj;
        }
    }
    //从队尾添加一个资源,并设置active=false;
    public void Push(GameObject item)
    {
        if (item == null)
        {
            Debug.LogWarning("push空对象");
            return;
        }
        item.SetActive(false);
        queue.Enqueue(item.gameObject);
    }
}

posted @ 2023-07-15 02:01  lxp_blog  阅读(20)  评论(0编辑  收藏  举报