コード例 #1
0
ファイル: ObjectPoolManager.cs プロジェクト: 37Sir/WarChess
        public void RegisteObject(string name, string resourcePath, int min, int max, float cleanDuration)
        {
            if (m_ObjectMap.ContainsKey(name))
            {
                //Debugger.LogWarning(name+" Has Registed! Ignore...");
                return;
            }
            if (min > max || max <= 0)
            {
                Debugger.LogError("ObjectPool Max must > 0!");
                return;
            }
            PoolObjectState pos = new PoolObjectState();

            pos.min           = min;
            pos.max           = max;
            pos.cleanDuration = cleanDuration;
            pos.resource      = resourcePath;
            pos.lastCleanTime = Time.time;
            pos.name          = name;
            m_ObjectMap.Add(name, pos);
            m_ObjectInstanceMap.Add(name, new List <PoolInstanceState>());
            App.ResourceManager.LoadPrefab(resourcePath, (GameObject obj) =>
            {
                if (obj != null)
                {
                    pos.gameObject = (GameObject)obj;
                    for (int i = 0; i < pos.min; i++)
                    {
                        GameObject goI = Instantiate <GameObject>(pos.gameObject);
                        goI.SetActive(false);
                        goI.transform.SetParent(m_ObjectPool.transform);
                        goI.transform.localScale    = Vector3.one;
                        goI.transform.localPosition = Vector3.one;
                        PoolInstanceState pis       = new PoolInstanceState();
                        goI.name       = pos.name;
                        pis.gameObject = goI;
                        pis.name       = pos.name;
                        pis.stateId    = -1;
                        pis.state      = PoolInstanceStateType.Sleep;
                        m_ObjectInstanceMap[name].Add(pis);
                    }
                }
            });
        }
コード例 #2
0
ファイル: ObjectPoolManager.cs プロジェクト: 37Sir/WarChess
        public IEnumerator OnInstantiate(string name, Action <GameObject> callback)
        {
            if (m_ObjectMap.ContainsKey(name))
            {
                PoolObjectState          pos   = m_ObjectMap[name];
                List <PoolInstanceState> poiss = m_ObjectInstanceMap[name];
                int activeCount            = 0;
                int maxId                  = 0;
                PoolInstanceState oldest   = null;
                PoolInstanceState sleepOne = null;
                for (int i = 0; i < poiss.Count; i++)
                {
                    if (poiss[i].state == PoolInstanceStateType.Sleep)
                    {
                        sleepOne = poiss[i];
                    }
                    else
                    {
                        activeCount++;
                        if (maxId < poiss[i].stateId)
                        {
                            maxId = poiss[i].stateId;
                        }
                    }
                }
                if (sleepOne != null)
                {
                    sleepOne.stateId         = maxId + 1;
                    sleepOne.state           = PoolInstanceStateType.Active;
                    sleepOne.gameObject.name = name;
                    if (callback != null)
                    {
                        callback(sleepOne.gameObject);
                    }
                }
                else
                {
                    if (activeCount >= pos.max)
                    {
                        sleepOne                 = oldest;
                        sleepOne.stateId         = maxId + 1;
                        sleepOne.state           = PoolInstanceStateType.Active;
                        sleepOne.gameObject.name = name;
                        if (callback != null)
                        {
                            callback(sleepOne.gameObject);
                        }
                    }
                    else
                    {
                        while (pos.gameObject == null)
                        {
                            yield return(new WaitForEndOfFrame());
                        }
                        GameObject goI = Instantiate <GameObject>(pos.gameObject);
                        goI.SetActive(false);
                        goI.transform.SetParent(m_ObjectPool.transform);
                        yield return(goI);

                        goI.transform.localScale    = Vector3.one;
                        goI.transform.localPosition = Vector3.one;
                        PoolInstanceState pis = new PoolInstanceState();
                        pis.gameObject = goI;
                        pis.name       = pos.name;
                        pis.stateId    = maxId + 1;
                        pis.state      = PoolInstanceStateType.Active;
                        if (m_ObjectInstanceMap.ContainsKey(name))
                        {
                            m_ObjectInstanceMap[name].Add(pis);
                            goI.name = name;
                            if (callback != null)
                            {
                                callback(goI);
                            }
                        }
                        else
                        {//场景被切换
                            Destroy(goI);
                        }
                    }
                }
            }
            else
            {
                Debugger.LogError("ObjectPool No Registe:" + name);
            }
        }