Example #1
0
        private void Start()
        {
            #region SimpleObjectPool
            var pool = new SimpleObjectPool <Fish>(() => new Fish(), initCount: 50);

            pool.CurCount.LogInfo();

            var fish = pool.Allocate();

            pool.CurCount.LogInfo();

            pool.Recycle(fish);

            pool.CurCount.LogInfo();
            #endregion



            #region SafeObjectPool

            SafeObjectPool <Bullet> .Instance.Init(50, 25);

            var bullet = Bullet.Allocate();

            SafeObjectPool <Bullet> .Instance.CurCount.LogInfo();

            bullet.Recycle2Cache();

            SafeObjectPool <Bullet> .Instance.CurCount.LogInfo();

            #endregion
        }
Example #2
0
        public SimpleObjectPool <T> GetObjectPool <T>() where T : new()
        {
            object objectPool;
            var    type = typeof(T);

            if (!mObjectPools.TryGetValue(type, out objectPool))
            {
                objectPool = new SimpleObjectPool <T>(() => new T());
                mObjectPools.Add(type, objectPool);
            }

            return((SimpleObjectPool <T>)objectPool);
        }
Example #3
0
        private static void MenuClicked()
        {
            var fishPool = new SimpleObjectPool <Fish>(() => new Fish(), null, 100);

            Debug.LogFormat("fishPool.CurCount : {0}", fishPool.CurCount);

            var fishOne = fishPool.Allocate();

            Debug.LogFormat("fishPool.CurCount : {0}", fishPool.CurCount);

            fishPool.Recycle(fishOne);
            Debug.LogFormat("fishPool.CurCount : {0}", fishPool.CurCount);

            for (int i = 0; i < 10; i++)
            {
                fishPool.Allocate();
            }
            Debug.LogFormat("fishPool.CurCount : {0}", fishPool.CurCount);
        }
Example #4
0
        /// The prefered way to create a context is to use the generated methods
        /// from the code generator, e.g. var context = new GameContext();
        public Context(int totalComponents, int startCreationIndex, ContextInfo contextInfo,
                       Func <IEntity, IRefCounter> aercFactory)
        {
            mTotalComponents = totalComponents;
            _creationIndex   = startCreationIndex;

            if (contextInfo != null)
            {
                _contextInfo = contextInfo;
                if (contextInfo.ComponentNames.Length != totalComponents)
                {
                    throw new ContextInfoException(this, contextInfo);
                }
            }
            else
            {
                _contextInfo = createDefaultContextInfo();
            }

            _aercFactory = aercFactory == null
                ? (entity) => new SafeARC(entity)
                : aercFactory;

            _groupsForIndex = new List <IGroup <TEntity> > [totalComponents];
            mComponentPools = new Stack <IComponent> [totalComponents];
            _entityIndices  = new Dictionary <string, IEntityIndex>();
            mGroupChangedListSimpleObjectPool = new SimpleObjectPool <List <GroupChanged <TEntity> > >(
                () => new List <GroupChanged <TEntity> >(),
                list => list.Clear()
                );

            // Cache delegates to avoid gc allocations
            _cachedEntityChanged     = updateGroupsComponentAddedOrRemoved;
            _cachedComponentReplaced = updateGroupsComponentReplaced;
            _cachedEntityReleased    = onEntityReleased;
            _cachedDestroyEntity     = onDestroyEntity;
        }
Example #5
0
 public void RegisterCustomObjectPool <T>(SimpleObjectPool <T> simpleObjectPool)
 {
     mObjectPools.Add(typeof(T), simpleObjectPool);
 }