当前位置: 首页>>代码示例>>C#>>正文


C# ObjectPool.Initialize方法代码示例

本文整理汇总了C#中ObjectPool.Initialize方法的典型用法代码示例。如果您正苦于以下问题:C# ObjectPool.Initialize方法的具体用法?C# ObjectPool.Initialize怎么用?C# ObjectPool.Initialize使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在ObjectPool的用法示例。


在下文中一共展示了ObjectPool.Initialize方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: TestAllocateAndFree

        public void TestAllocateAndFree()
        {
            var pool = new ObjectPool<TestElement>();
            pool.Initialize();

            Assert.IsTrue(pool.UsedListSize == 0 && pool.FreeListSize == 0);

            // allocate some instances
            var e1 = pool.Allocate();
            var e2 = pool.Allocate();

            Assert.IsTrue(pool.UsedListSize == 2 && pool.FreeListSize == 0);
            Assert.IsTrue(e1 != e2);
            Assert.IsTrue(e1.value == 0);

            // free both and then allocate two more again - we should get both back

            pool.Free(e1);
            pool.Free(e2);
            Assert.IsTrue(pool.UsedListSize == 0 && pool.FreeListSize == 2);

            var e3 = pool.Allocate();
            var e4 = pool.Allocate();
            Assert.IsTrue(pool.UsedListSize == 2 && pool.FreeListSize == 0);
            Assert.IsTrue(e3 == e2);
            Assert.IsTrue(e4 == e1);
        }
开发者ID:rzubek,项目名称:UnityGameTools,代码行数:27,代码来源:ObjectPoolTest.cs

示例2: TestFactoryAndReset

        public void TestFactoryAndReset()
        {
            var pool = new ObjectPool<TestElement>();
            pool.Initialize(() => { var e = new TestElement(); e.value = 42; return e; });

            // allocate a new instance using this custom factory, make sure the factory code got called
            var e1 = pool.Allocate();
            Assert.IsTrue(e1.value == 42);

            // free it, and make sure it got reset
            pool.Free(e1);
            Assert.IsTrue(e1.value == 0);

            // now allocate again, this time since it's coming from the object pool,
            // the factory code won't be called
            var e2 = pool.Allocate();
            Assert.IsTrue(e1 == e2);
            Assert.IsTrue(e2.value == 0); // because this is coming from the pool, not factory
        }
开发者ID:rzubek,项目名称:UnityGameTools,代码行数:19,代码来源:ObjectPoolTest.cs

示例3: TestPoolRelease

        public void TestPoolRelease()
        {
            var pool = new ObjectPool<TestElement>();
            pool.Initialize(() => { var e = new TestElement(); e.value = 42; return e; });

            // allocate a new instance using this custom factory, make sure the factory code got called
            var e1 = pool.Allocate();
            Assert.IsTrue(e1.value == 42);
            Assert.IsTrue(pool.UsedListSize == 1);
            Assert.IsTrue(pool.FreeListSize == 0);

            // allocate and free an instance. this will grow the pool to size one
            pool.Free(pool.Allocate());
            Assert.IsTrue(pool.UsedListSize == 1);
            Assert.IsTrue(pool.FreeListSize == 1);

            // now release the entire object pool. this will clear out the free list, but will not
            // affect the object already in circulation, since we're not tracking them.
            pool.Release();
            Assert.IsTrue(e1.value == 42);
            Assert.IsTrue(pool.UsedListSize == 1);
            Assert.IsTrue(pool.FreeListSize == -1);
        }
开发者ID:rzubek,项目名称:UnityGameTools,代码行数:23,代码来源:ObjectPoolTest.cs


注:本文中的ObjectPool.Initialize方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。