Example #1
0
        public void Return(SimplePoolItem item)
        {
            item.OnBeforeReturn();
            Cache cache = cacheByItem[item];

            cache.activeItems.Remove(item);
            cache.inactiveItems.Add(item);
            item.gameObject.active = false;
        }
Example #2
0
        public SimplePoolItem Obtain(string path)
        {
            Definition definition = null;
            bool       found      = definitionByPath.TryGetValue(path, out definition);

            if (!found)
            {
                throw new Exception(string.Format(name + ": Cannot find definition for path: '{0}'", path));
            }

            Cache cache        = null;
            bool  isCacheFound = cacheByPath.TryGetValue(path, out cache);

            if (!isCacheFound)
            {
                cache             = new Cache();
                cacheByPath[path] = cache;
                SimplePoolItem item = InstantiateItem(definition.Prefab);
                cache.items.Add(item);
                cache.activeItems.Add(item);
                cacheByItem[item] = cache;
#if UNITY_EDITOR
                display.AddItem(item);
                display.AddCache(cache);
#endif
                return(item);
            }
            else
            {
                SimplePoolItem item = cache.FindInactiveItem();
                if (item == null)
                {
                    item = InstantiateItem(definition.Prefab);
                    cache.items.Add(item);
                    cache.activeItems.Add(item);
                    cacheByItem[item] = cache;
#if UNITY_EDITOR
                    display.AddItem(item);
#endif
                }
                else
                {
                    cache.inactiveItems.Remove(item);
                    cache.activeItems.Add(item);
                    item.gameObject.active = true;
                }

                return(item);
            }
        }
Example #3
0
        private SimplePoolItem InstantiateItem(GameObject prefab)
        {
            GameObject     go  = GameObject.Instantiate(prefab);
            SimplePoolItem spi = go.AddComponent <SimplePoolItem>();
            Transform      t   = go.transform;
            Quaternion     originalRotation = t.rotation;
            Vector3        originalScale    = t.localScale;

            spi.AddDefaultCleanupProcedures(() => {
                t.rotation   = originalRotation;
                t.localScale = originalScale;
            });
            return(spi);
        }
        public void ObtainAlreadyDefinedShouldReturnItem()
        {
            SimplePool sp     = new SimplePool();
            string     path   = "path";
            GameObject prefab = new GameObject("Prefab");

            sp.AddDefinition(new SimplePool.Definition(path, prefab));

            SimplePoolItem item = sp.Obtain(path);

            if (item != null && item.gameObject.activeInHierarchy)
            {
                DLog.Log("already defined passed");
            }
        }
        public void Obtain2TimesShouldReturnDifferentItems()
        {
            SimplePool sp     = new SimplePool();
            string     path   = "path";
            GameObject prefab = new GameObject("Prefab");

            sp.AddDefinition(new SimplePool.Definition(path, prefab));

            SimplePoolItem item1 = sp.Obtain(path);
            SimplePoolItem item2 = sp.Obtain(path);

            if (item1 != item2 && item1.gameObject != item2.gameObject)
            {
                DLog.Log("Obtain2TimesShouldReturnDifferentItems passed");
            }
        }
        public void ObtainAndReturn()
        {
            SimplePool sp     = new SimplePool();
            string     path   = "path";
            GameObject prefab = new GameObject("Prefab");

            sp.AddDefinition(new SimplePool.Definition(path, prefab));

            SimplePoolItem item = sp.Obtain(path);

            sp.Return(item);
            if (item.gameObject.activeInHierarchy == false)
            {
                DLog.Log("ObtainAndReturn passed");
            }
        }
        public void ReObtainShouldBeTheSame()
        {
            SimplePool sp     = new SimplePool();
            string     path   = "path";
            GameObject prefab = new GameObject("Prefab");

            sp.AddDefinition(new SimplePool.Definition(path, prefab));

            SimplePoolItem item = sp.Obtain(path);

            sp.Return(item);
            SimplePoolItem reobtainItem = sp.Obtain(path);

            if (item == reobtainItem && item.gameObject == reobtainItem.gameObject)
            {
                DLog.Log("ReObtainShouldBeTheSame passed");
            }
        }
Example #8
0
 public void AddItem(SimplePoolItem item)
 {
     pool.Add(item.gameObject);
 }