Exemplo n.º 1
0
    /// <summary>
    /// 取出一个物体
    /// </summary>
    public GameObject New()
    {
        PoolItem item = null;

        if (objQueue.Count > 0)
        {
            item = objQueue.Dequeue();
        }
        else
        {
            Count++;
            item          = Tools.CreateGameObjectByObj(goObj, parent).GetComponent <PoolItem>();
            item.callback = Recycle;
            item.Init();
        }
        item.ResetItem();
        return(item.gameObject);
    }
Exemplo n.º 2
0
 /// <summary>
 /// 初始化对象池,池内物体必须要继承PoolItem
 /// </summary>
 /// <param name="obj">要存入对象池的Gameobject</param>
 /// <param name="parent">存放的父物体</param>
 /// <param name="count">初始化数量</param>
 public ObjectPool(GameObject obj, Transform parent, int count)
 {
     if (obj.GetComponent <PoolItem>() == null)
     {
         Debug.LogError("没有找到PoolItem" + obj.name);
         return;
     }
     objQueue = new Queue <PoolItem>();
     goObj    = obj;
     Count    = count;
     for (int i = 0; i < Count; i++)
     {
         PoolItem item = Tools.CreateGameObjectByObj(goObj, parent).GetComponent <PoolItem>();
         objQueue.Enqueue(item);
         this.parent   = parent;
         item.callback = Recycle;
         item.Init();
     }
 }