U3D对象池

一个对象池类

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

public class ObjectPool {

    #region 单例
    private static ObjectPool instance;
    private ObjectPool()
    {
        Pool = new Dictionary<string, List<GameObject>>();
        Prefabs = new Dictionary<string, GameObject>();
    }
    public static ObjectPool GetInstance()
    {
        if (instance == null)
        {
            instance = new ObjectPool();
        }
         return instance;
    }
    #endregion

    /// <summary>
    /// 对象池,字典中与key值对应的是数组(arraylist)(例:key为手枪子弹  对应arraylist[手枪子弹,手枪子弹,手枪子弹。。。。。。 ])。
    /// </summary>
    private Dictionary<string, List<GameObject>> Pool;
    /// <summary>
    /// 预设体
    /// </summary>
    private Dictionary<string, GameObject> Prefabs;
    /// <summary>
    /// 从对象池中获取对象
    /// </summary>
    /// <param name="objName"></param>
    /// <returns></returns>
    public GameObject GetObj(string objName)
    {
        GameObject result = null;
        //判断是否有该名字的对象池      //对象池中有对象
        if (Pool.ContainsKey(objName)&& Pool[objName].Count > 0)
        {             
                //获取这个对象池中的第一个对象
                result = Pool[objName][0];
                //激活对象
                result.SetActive(true);
                //从对象池中移除对象
                Pool[objName].Remove(result);
                //返回结果
                return result;
        }
        //如果没有该名字的对象池或者该名字对象池没有对象
        GameObject Prefab = null;
        if (Prefabs.ContainsKey(objName)) //如果已经加载过预设体
        {
            Prefab = Prefabs[objName];
        }else //如果没有加载过预设体
        {
            //加载预设体
            Prefab = Resources.Load<GameObject>("Obj/"+objName);
            //更新预设体的字典
            Prefabs.Add(objName, Prefab);
        }
        //实例化物体
        result = UnityEngine.Object.Instantiate(Prefab);
        //改名 去除Clone
        result.name = objName;
        return result;
    }
    /// <summary>
    /// 回收对象到对象池
    /// </summary>
    public void RecycleObj(GameObject Obj)
    {
        Obj.SetActive(false);
        //如果有该对象的对象池,直接放在池子中
        if (Pool.ContainsKey(Obj.name))
        {
            Pool[Obj.name].Add(Obj);
        }else//如果没有该对象的对象池,创建一个该类型的池子,并将对象放入
        {
            Pool.Add(Obj.name, new List<GameObject>() { Obj });
        }
    }
}

一个挂在子弹物体上

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

public class Bullet : MonoBehaviour {

    // Use this for initialization
    void Start () {
        
    }
    
    // Update is called once per frame
    void Update () {
        
    }
    //3秒后自动回收到对象次
    IEnumerator AutoRecycle()
    {
        yield return new WaitForSeconds(3f);
        ObjectPool.GetInstance().RecycleObj(this.gameObject);
    }

    public void OnEnable()
    {
        StartCoroutine(AutoRecycle());
    }
}

一个用来操作

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

public class test : MonoBehaviour {

    // Use this for initialization
    void Start () {
        
    }
    
    // Update is called once per frame
    void Update () {
        if (Input.GetKeyDown(KeyCode.A))
        {
            ObjectPool.GetInstance().GetObj("Bullet");
        }
    }
}

猜你喜欢

转载自www.cnblogs.com/wuwenbo/p/8989353.html