源码:

 public class PoolObj
    {
        List<GameObject> pooledObjects = null;
        private string path = string.Empty;

        public PoolObj(string path)
        {
            pooledObjects = new List<GameObject>();
            this.path = path;
        }

        public GameObject GetObj()
        {
            for (int i = 0; i < pooledObjects.Count; i++)
            {
                GameObject tempObj = pooledObjects[i];
                if (!tempObj.activeInHierarchy)
                {
                    tempObj.SetActive(true);
                    return tempObj.gameObject;
                }
            }
            if (!String.IsNullOrEmpty(path))
            {
                GameObject newObj = MonoBehaviour.Instantiate(Resources.Load<GameObject>(path));
                newObj.SetActive(false);
                pooledObjects.Add(newObj);//添加
                return GetObj();
            }
            Debug.LogError("PoolObj Resources path == null ");
            return null;
        }
    }

使用方法:

PoolObj poolObj = new PoolObj("Cube");

//Cube 为Resources.Load加载资源地址

 poolObj.GetObj(); 为使用对象实例化物体,可以重复调用,自动添加资源,自动利用未被使用的资源

 poolObj.GetObj(); 不使用的资源,隐藏即可