Example #1
0
        public T Spawn <T, TState>(TState state, System.Func <TState, T> constructor, System.Action <T> destructor = null) where T : class
        {
            if (Pools.isActive == false)
            {
                var instance = constructor.Invoke(state);
                PoolInternalBaseThread.CallOnSpawn(instance, null);
                return(instance);
            }

            var type = typeof(T);

            if (this.pool.TryGetValue(type, out var pool) == true)
            {
                return((T)pool.Spawn());
            }

            pool = new PoolInternalThread <T, TState>(type, state, constructor, destructor);
            if (this.pool.TryAdd(type, pool) == true)
            {
                return((T)pool.Spawn());
            }

            if (this.pool.TryGetValue(type, out pool) == true)
            {
                return((T)pool.Spawn());
            }

            throw new System.Exception("Spawn failed");
        }