Beispiel #1
0
    //回收一个对象到对象池
    public bool Recycle(T obj)
    {
        if (obj == null)
        {
            return(false);
        }

        m_nNoRecycleCount--;
#if UNITY_EDITOR && OBJ_POOL_TRACE_STACK
        m_NoRecycleItem.Remove(obj);
#endif

        if (mPool.Count >= miMaxCount && miMaxCount > 0)
        {
            obj = null;
            return(false);
        }

#if UNITY_EDITOR && OBJ_POOL_TRACE_STACK
        if (mPool.Contains(obj))
        {
            GDebugger.LogError("同一个对象被回收了多次,请检查");
        }
        else
#endif
        {
            mPool.Push(obj);
        }
        return(true);
    }
Beispiel #2
0
    //打印日志
    protected override void PrintLog()
    {
        if (m_nNoRecycleCount > 0)
        {
            string msg = "从类对象池申请的对象没有全部回收,有泄漏可能性,请检查:" + GetType().ToString() + ",未回收个数" + m_nNoRecycleCount + ",申请调用堆栈如下:\r\n";
            Dictionary <T, string> .Enumerator it = m_NoRecycleItem.GetEnumerator();
            int i = 1;
            while (it.MoveNext())
            {
                msg += "第" + (i++) + "次:\r\n" + it.Current.Value;
                if (i > 10)
                {
                    break;
                }
            }

            GDebugger.LogError(msg);
        }
    }
Beispiel #3
0
    //产生一个新对象
    public T Spawn(bool bCreateIfPoolEmpty = true)    //bCreateIfPoolEmpty如果pool为空时是否new新的出来
    {
        if (mPool.Count > 0)
        {
            T rtn = mPool.Pop();
            if (rtn == null)
            {
#if UNITY_EDITOR && OBJ_POOL_TRACE_STACK
                GDebugger.LogError("Spawn is null");
#endif
                if (bCreateIfPoolEmpty)
                {
                    rtn = new T();
                }
            }
            m_nNoRecycleCount++;
#if UNITY_EDITOR && OBJ_POOL_TRACE_STACK
            m_NoRecycleItem.Add(rtn, StackTraceUtility.ExtractStackTrace());
#endif
            return(rtn);
        }

#if UNITY_EDITOR && OBJ_POOL_TRACE_STACK
        if (miMaxCount > 0 && !m_bHavePromptPoolIsEmpty)
        {
            m_bHavePromptPoolIsEmpty = true;
            GDebugger.LogError("对象池已经空了,是否初始池太小或者没有完全回收?当前设定大小为:" + miMaxCount + "," + GetType().ToString());
        }
#endif

        if (bCreateIfPoolEmpty)
        {
            T rtn = new T();
            m_nNoRecycleCount++;
#if UNITY_EDITOR && OBJ_POOL_TRACE_STACK
            m_NoRecycleItem.Add(rtn, StackTraceUtility.ExtractStackTrace());
#endif
            return(rtn);
        }

        return(null);
    }