示例#1
0
    // 从池中获取一个能用的对象
    public GameObject getObjFromPool(PoolType type)
    {
        if (!hashTable.ContainsKey(type))
        {
            throw new Exception("Have not create " + type.ToString() + " Pool");
        }

        PoolObj    poolObj = hashTable[type] as PoolObj;
        GameObject t;

        for (int i = 0; i < poolObj.objList.Count; i++)
        {
            t = poolObj.objList[(poolObj.index + i) % poolObj.objList.Count];
            // Active 为False 即为可以拿出来使用的对象,否则寻找下一个
            if (!t.activeSelf)
            {
                poolObj.index = (poolObj.index + i) % poolObj.objList.Count;
                t.SetActive(true);
                return(t);
            }
        }
        // 所有的都正在使用……只能添加新的了
        t = addToPool(poolObj);
        t.SetActive(true);
        return(t);
    }
示例#2
0
    private GameObject getInsObj()
    {
        GameObject go = null;

        if (objLst.Count <= 0)
        {
            if (tempObj.Obj == null)
            {
                Debug.LogError("<color=yellow> tempObj.Obj == null</color>");
            }
            go = GameObject.Instantiate(tempObj.Obj);
            PoolObj po = go.GetComponent <PoolObj>();
            if (po == null)
            {
                po = go.AddComponent <PoolObj>();
            }
            po.pType   = pType;
            po.url     = url;
            po.depends = this.depends;
            po.onInit();
        }
        else
        {
            go = objLst[0].Obj;
            objLst.RemoveAt(0);
        }
        go.transform.SetParent(null);
        go.transform.position         = Vector3.zero;
        go.transform.localEulerAngles = Vector3.zero;
        go.transform.localScale       = Vector3.one;
        usingTime = TimerUtils.getSecTimer();
        return(go);
    }
示例#3
0
    public void saveObj(PoolObj po)
    {
        GameObject go = po.Obj;

        go.transform.SetParent(root);
        go.transform.localPosition = Vector3.zero;
        objLst.Add(po);
    }
示例#4
0
    /// <summary>
    /// This function returns a object from the pool and increase the pool size when needed.
    /// </summary>
    /// <returns>PoolObj</returns>
    public PoolObj GetFromPool()
    {
        if (m_ObjectsAliveInPool < m_ObjectsInPool)
        {
            m_ObjectsAliveInPool++;
            PoolObj returnObject = m_DeadList.Dequeue();
            returnObject.gameObject.SetActive(true);
            return(returnObject);
        }
        else
        {
            for (int i = 0; i < m_IncreaseIncrement; i++)
            {
                GameObject NewPoolGameObj = Instantiate(ObjectPrefab, transform);
                PoolObj    poolObj        = NewPoolGameObj.GetComponent <PoolObj>();
                switch (m_objEnum)
                {
                case PooledSubObject.Default:
                    poolObj.GenericObj = null;
                    break;

                case PooledSubObject.GameObject:
                    poolObj.GenericObj = poolObj.gameObject;
                    break;

                case PooledSubObject.VisualEffect:
                    poolObj.GenericObj = poolObj.gameObject.GetComponent <VisualEffect>();
                    break;

                case PooledSubObject.Enemy:
                    poolObj.GenericObj = poolObj.gameObject.GetComponent <Enemy>();
                    break;

                case PooledSubObject.Rigidbody:
                    poolObj.GenericObj = poolObj.gameObject.GetComponent <Rigidbody>();
                    break;

                case PooledSubObject.TowerProjectile:
                    poolObj.GenericObj = poolObj.gameObject.GetComponent <TowerProjectile>();
                    break;

                default:
                    //m_GenericObj = null;
                    break;
                }
                m_DeadList.Enqueue(poolObj);

                m_ObjectsInPool++;
                NewPoolGameObj.SetActive(false);
            }

            m_ObjectsAliveInPool++;
            PoolObj returnObject = m_DeadList.Dequeue();
            returnObject.gameObject.SetActive(true);
            return(returnObject);
        }
    }
示例#5
0
    // 往某个池中添加新对象
    private GameObject addToPool(PoolObj poolObj)
    {
        GameObject t = GameObject.Instantiate(poolObj.prefab) as GameObject;

        t.SetActive(false);
        poolObj.objList.Add(t);
        t.transform.parent = poolObj.contaienr.transform;
        return(t);
    }
示例#6
0
 public void Create()
 {
     for (int i = 0; i < initial_cant * 2; i++)
     {
         PoolObj <T> obj = new PoolObj <T>(factory.IntanciateObject(), OnActive, OnDisable, ReleaseObject);
         obj.Deactivate();
         pool.Add(obj);
     }
 }
示例#7
0
    /// <summary>
    /// This fuction sets the params for the object pool that will be created.
    /// </summary>
    /// <param name="GenericObj">Choose from the pooledSubject enum what the secondary "generic" object is</param>
    /// <param name="PoolStartSize">the amount of objects the pool starts with</param>
    /// <param name="IncreaseIncrement">the amount of objects that will be added when the pool hits its current limit</param>
    /// <param name="ManagerTicksBeforeClean">the amount of ticks needed before the pool wil check for a clean</param>
    /// <param name="CleanThreshold">the amount of unused objects needed for a clean to happen example</param>
    public void Init(PooledSubObject GenericObj, int PoolStartSize = 20, int IncreaseIncrement = 5, int ManagerTicksBeforeClean = 5, int CleanThreshold = 20)
    {
        m_ObjectsAliveInPool = 0;


        m_objEnum = GenericObj;
        PoolObj TempData = ObjectPrefab.GetComponent <PoolObj>();

        TempData.Pool                 = this;
        this.m_CleanThreshold         = CleanThreshold;
        this.m_IncreaseIncrement      = IncreaseIncrement;
        this.m_anagerTicksBeforeClean = ManagerTicksBeforeClean;

        //m_DeadList = new Queue<GameObject>();
        m_DeadList = new Queue <PoolObj>();

        for (int i = 0; i < PoolStartSize; i++)
        {
            GameObject NewPoolGameObj = Instantiate(ObjectPrefab, transform);
            PoolObj    poolObj        = NewPoolGameObj.GetComponent <PoolObj>();
            switch (GenericObj)
            {
            case PooledSubObject.Default:
                poolObj.GenericObj = null;
                break;

            case PooledSubObject.GameObject:
                poolObj.GenericObj = poolObj.gameObject;
                break;

            case PooledSubObject.VisualEffect:
                poolObj.GenericObj = poolObj.gameObject.GetComponent <VisualEffect>();
                break;

            case PooledSubObject.Enemy:
                poolObj.GenericObj = poolObj.gameObject.GetComponent <Enemy>();
                break;

            case PooledSubObject.Rigidbody:
                poolObj.GenericObj = poolObj.gameObject.GetComponent <Rigidbody>();
                break;

            case PooledSubObject.TowerProjectile:
                poolObj.GenericObj = poolObj.gameObject.GetComponent <TowerProjectile>();
                break;

            default:
                //m_GenericObj = null;
                break;
            }
            m_DeadList.Enqueue(poolObj);

            m_ObjectsInPool++;
            NewPoolGameObj.SetActive(false);
        }
    }
示例#8
0
    public void AddObject(PoolObj obj)
    {
        string key = obj.GetType().FullName;

        if (!Ary.ContainsKey(key))
        {
            Ary.Add(key, new Queue <PoolObj>());
        }
        Ary[key].Enqueue(obj);
    }
示例#9
0
    private void AddObj(Pool pool, string poolId)
    {
        GameObject newObj = Instantiate(pool.obj);

        newObj.gameObject.SetActive(false);
        newObj.transform.SetParent(this.transform, true);
        pool.availableObjects.Enqueue(newObj);
        pool.size++;

        PoolObj po = newObj.AddComponent <PoolObj>();

        po.poolId = poolId;
    }
示例#10
0
    public void Recycle(GameObject obj)
    {
        PoolObj po = obj.GetComponent <PoolObj>();

        if (po == null)
        {
            Debug.LogError(string.Format("Game object {0} does not have a PoolObj component.", obj.name));
            return;
        }

        obj.gameObject.transform.SetParent(this.transform, true);
        obj.gameObject.SetActive(false);
        pools[po.poolId].availableObjects.Enqueue(obj.gameObject);
    }
示例#11
0
 public void onDispose()
 {
     loadHandler.Clear();
     loadHandler = null;
     for (int i = 0; i < objLst.Count; i++)
     {
         GameObject.Destroy(objLst[i].Obj);
     }
     objLst.Clear();
     objLst = null;
     GameObject.Destroy(tempObj.Obj);
     tempObj = null;
     GameObject.Destroy(this.root.gameObject);
     Debug.Log("CS basePool 释放");
 }
示例#12
0
    private void insObj(Action <GameObject> callBack)
    {
        GameObject go = GameObject.Instantiate(this.obj) as GameObject;

        GameObject.DontDestroyOnLoad(go);
        PoolObj po = go.AddComponent <PoolObj>();

        po.resName = resName;
        for (int i = 0; i < deps.Count; i++)
        {
            AssetMgr.addRef(deps[i]);
        }
        AssetMgr.addRef(resName);
        callBack.Invoke(go);
    }
示例#13
0
文件: LImage.cs 项目: tianjiuwan/demo
 private void findPoolRoot(Transform trans)
 {
     if (trans == null)
     {
         return;
     }
     if (trans.GetComponent <PoolObj>() == null)
     {
         findPoolRoot(trans.parent);
     }
     else
     {
         this.rootObj = trans.GetComponent <PoolObj>();
     }
 }
示例#14
0
    /// <summary>
    /// 回收策略
    /// 不是从池子出来的obj 直接销毁
    /// 如果池子已经被销毁了 创建池子 丢池子里面去(为了统一释放ab引用)
    /// </summary>
    /// <param name="go"></param>
    public void recyleObj(GameObject go)
    {
        PoolObj obj = go.GetComponent <PoolObj>();

        if (obj != null)
        {
            if (!pools.ContainsKey(obj.resName))
            {
                string resName = obj.resName;
                createPool(resName, E_PoolMode.Level, E_PoolType.None, 60);
            }
            pools[obj.resName].recyle(go);
        }
        else
        {
            GameObject.Destroy(go);
        }
    }
示例#15
0
    // 创建一种对象的池
    public void createObjPool(GameObject prefab, PoolType type, int size = 10)
    {
        PoolObj newPoolObj = new PoolObj();

        newPoolObj.prefab = prefab;

        GameObject container = new GameObject(prefab.name + "Container");

        container.transform.parent = poolGO.transform;
        newPoolObj.contaienr       = container;

        for (int i = 0; i < size; i++)
        {
            addToPool(newPoolObj);
        }

        newPoolObj.index = 0;
        hashTable.Add(type, newPoolObj);
    }
示例#16
0
 //销毁池
 public virtual void onDispose()
 {
     LoadItemMgr.remove(resName, loadFinish);
     if (cacheLst != null)
     {
         int count = cacheLst.Count;
         //释放静态引用
         if (count > 0)
         {
             for (int i = 0; i < deps.Count; i++)
             {
                 AssetMgr.subRef(deps[i], count);
             }
             AssetMgr.subRef(resName, count);
         }
         //释放动态引用
         for (int i = 0; i < cacheLst.Count; i++)
         {
             PoolObj po = cacheLst[i].GetComponent <PoolObj>();
             if (po.getDepsNum() > 0)
             {
                 List <string> dyDeps = po.getDeps();
                 for (int j = 0; j < dyDeps.Count; j++)
                 {
                     AssetMgr.subRef(dyDeps[i]);
                 }
             }
         }
         //销毁gameobject
         for (int i = 0; i < cacheLst.Count; i++)
         {
             GameObject.Destroy(cacheLst[i]);
         }
         cacheLst.Clear();
         cacheLst = null;
     }
     if (deps != null)
     {
         deps.Clear();
         deps = null;
     }
     GameObject.Destroy(this.poolRoot.gameObject);
 }
    public PoolObj GetCachePoolObj()
    {
        PoolObj poolObj = null;
        var     isGo    = IsGameObject();

        if (isGo)
        {
            //有 asset 直接取 obj
            foreach (var item in objectDic)
            {
                if (item.Value.IsUsing())
                {
                    continue;
                }

                poolObj = item.Value;
                poolObj.Use();

                break;
            }
            if (null == poolObj)
            {
                //如果都在使用 那么创建新的 Object(目前没有上限 之后会有上限)
                poolObj = CreateNew();
            }
        }
        else
        {
            //由于是引用 所以第一个就是
            foreach (var item in objectDic)
            {
                poolObj = item.Value;
            }

            if (null == poolObj)
            {
                //如果都在使用 那么创建新的 Object(目前没有上限 之后会有上限)
                poolObj = CreateNew();
            }
        }
        return(poolObj);
    }
示例#18
0
 /// <summary>
 /// Check if cleaning is necessary and if it is it cleans up the pool.
 /// </summary>
 private void Clean()
 {
     if (m_CurrentTick >= m_anagerTicksBeforeClean)
     {
         if (m_ObjectsInPool - m_ObjectsAliveInPool > m_CleanThreshold)
         {
             for (int i = 0; i < m_CleanThreshold; i++)
             {
                 PoolObj returnObject = m_DeadList.Dequeue();
                 Destroy(returnObject.gameObject);
                 m_ObjectsInPool--;
             }
             m_CurrentTick = m_anagerTicksBeforeClean;
         }
         else
         {
             m_CurrentTick = 0;
         }
     }
 }
示例#19
0
    //放回
    public void saveObj(GameObject go)
    {
        PoolObj po        = go.GetComponent <PoolObj>();
        bool    isDestroy = po == null || !pools.ContainsKey(po.url);

        if (isDestroy)
        {
            GameObject.Destroy(go);
        }
        else
        {
            if (pools.ContainsKey(po.url))
            {
                pools[po.url].saveObj(po);
            }
            else
            {
                GameObject.Destroy(go);
            }
        }
    }
    public PoolObj CreateNew()
    {
        //path
        UnityEngine.Object obj = null;
        var isGo = IsGameObject();

        PoolObj newPoolObj = null;

        if (isGo)
        {
            obj        = GameObject.Instantiate(this.assetObj) as GameObject;
            newPoolObj = new PoolObj();
            newPoolObj.SetInfo(obj.GetInstanceID(), obj);
            //newPoolObj.id = obj.GetInstanceID();
            //newPoolObj.obj = obj;
            newPoolObj.Use();
            objectDic.Add(newPoolObj.id, newPoolObj);
        }
        else
        {
            obj = this.assetObj;
            if (!objectDic.ContainsKey(obj.GetInstanceID()))
            {
                newPoolObj = new PoolObj();
                newPoolObj.SetInfo(obj.GetInstanceID(), obj);
                //newPoolObj.id = obj.GetInstanceID();
                //newPoolObj.obj = obj;
                newPoolObj.Use();
                objectDic.Add(newPoolObj.id, newPoolObj);
            }
            else
            {
                newPoolObj = objectDic[obj.GetInstanceID()];
            }
        }

        return(newPoolObj);
    }
示例#21
0
 private void onLoaderFinish(string result, bool isSucc, TBundle tb)
 {
     if (isSucc)
     {
         UnityEngine.Object obj  = tb.Ab.LoadAsset(getAbName(url));
         GameObject         temp = GameObject.Instantiate(obj) as GameObject;
         temp.name = temp.name.Replace("(Clone)", "");
         temp.transform.SetParent(root);
         temp.transform.localPosition = new Vector3(2000, 2000, 2000);
         tempObj         = temp.AddComponent <PoolObj>();
         tempObj.Obj     = temp;
         tempObj.pType   = pType;
         tempObj.url     = url;
         tempObj.depends = this.depends;
         tempObj.onInit();
         for (int i = 0; i < loadHandler.Count; i++)
         {
             GameObject go = getInsObj();
             loadHandler[i](go);
         }
         loadHandler.Clear();
     }
 }
示例#22
0
 public static void SetPoolObj(PoolObj <Bullet> pool_obj, Transform pointtoshoot, float _speed, int damage)
 {
     pool_obj.GetObj.Set(pointtoshoot, _speed, damage);
 }
示例#23
0
 /// <summary>
 /// put the given object back into the pool
 /// </summary>
 /// <param name="obj">this object will be put back in the pool</param>
 public void AddObjectToPool(PoolObj obj)
 {
     obj.gameObject.SetActive(false);
     m_DeadList.Enqueue(obj);
     m_ObjectsAliveInPool--;
 }