Пример #1
0
    /// <summary>
    /// 从对象池中获取对象
    /// </summary>
    /// <param name="poolName"></param>
    /// <returns></returns>
    public GameObject GetObject(string poolName, Transform parent = null)
    {
        if (parent == null)
        {
            parent = transform;
        }
        GameObject result;

        if (ObjPool.ContainsKey(poolName))
        {
            if (ObjPool[poolName].Count > 0)
            {
                result = ObjPool[poolName][0];
                result.transform.SetParent(parent);
                ObjPool[poolName].RemoveAt(0);
                return(result);
            }
        }
        if (PrefabDict.ContainsKey(poolName))
        {
            result      = Instantiate(PrefabDict[poolName], parent);
            result.name = poolName;
            return(result);
        }
        else
        {
            Debug.LogError("[ObjectPool]:   prefab is null");
            return(null);
            //prefab = Resources.Load<GameObject>("Prefabs/" + objName);
            //_prefabDict.Add(objName, prefab);
        }
    }
Пример #2
0
 /// <summary>
 /// 回收对象到对象池
 /// </summary>
 /// <param name="poolName"></param>
 /// <param name="obj"></param>
 public void RecycleObj(string poolName, GameObject obj)
 {
     obj.SetActive(false);
     if (ObjPool.ContainsKey(poolName))
     {
         ObjPool[poolName].Add(obj);
     }
     else
     {
         ObjPool.Add(poolName, new List <GameObject>()
         {
             obj
         });
     }
 }
Пример #3
0
 /// <summary>
 /// 回收对象到对象池
 /// </summary>
 /// <param name="objName"></param>
 public void RecycleObj(GameObject obj, Transform parent = null)
 {
     //TODO  统一
     if (parent == null)
     {
         parent = transform;
     }
     obj.SetActive(false);
     obj.transform.SetParent(parent);
     if (ObjPool.ContainsKey(obj.name))
     {
         ObjPool[obj.name].Add(obj);
     }
     else
     {
         ObjPool.Add(obj.name, new List <GameObject>()
         {
             obj
         });
     }
 }