U3D Object Pool

an object pool class

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> 
    /// The object pool, the key value in the dictionary corresponds to the array (arraylist) (for example: the key is the pistol bullet corresponding to the arraylist [pistol bullet, pistol bullet, pistol bullet... ]) .
    ///  </summary> 
    private Dictionary< string , List<GameObject>> Pool;
     ///  <summary> 
    /// Prefabs
     ///  </summary> 
    private Dictionary< string , GameObject> Prefabs;
     ///  <summary> 
    /// Get the object from the object pool
     ///  </summary> 
    ///  <param name="objName"></param> 
    ///  <returns></returns>
    objName)
    {
        GameObject result = null ;
         // Determine whether there is an object pool with this name       // There is an object in the object pool 
        if (Pool.ContainsKey(objName)&& Pool[objName].Count > 0 )
        {             
                // Get the first object in this object pool 
                result = Pool[objName][ 0 ];
                 // Activate the object 
                result.SetActive( true );
                 // Remove the object from the object pool 
                Pool[objName].Remove(result );
                 // return result 
                return result;
        }
        // If there is no object pool with the name or there is no object in the name object pool 
        GameObject Prefab = null ;
         if (Prefabs.ContainsKey(objName)) // If the preset body has been loaded 
        {
            Prefab = Prefabs[objName];
        } else  // If the preset body has not been loaded 
        {
             // Load the preset body 
            Prefab = Resources.Load<GameObject>( " Obj/ " + objName);
             // Update the dictionary of the preset body 
            Prefabs.Add(objName, Prefab );
        }
        // Instantiate the object 
        result = UnityEngine.Object.Instantiate(Prefab);
         // Rename to remove Clone 
        result.name = objName;
         return result;
    }
    ///  <summary> 
    /// Recycle the object to the object pool
     ///  </summary> 
    public  void RecycleObj(GameObject Obj)
    {
        Obj.SetActive( false );
         // If there is an object pool for the object, put it directly in the pool 
        if (Pool.ContainsKey(Obj.name))
        {
            Pool[Obj.name].Add(Obj);
        } else // If there is no object pool for this object, create a pool of this type and put the object in 
        {
            Pool.Add(Obj.name, new List<GameObject>() { Obj });
        }
    }
}

one hanging on a bullet object

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 () {
        
    }
    // After 3 seconds, the object is automatically recycled to 
    IEnumerator AutoRecycle()
    {
        yield return new WaitForSeconds(3f);
        ObjectPool.GetInstance().RecycleObj(this.gameObject);
    }

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

one to operate

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");
        }
    }
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325302233&siteId=291194637