Exemplo n.º 1
0
        public GameObjectPool AddPoolForObject(GameObject prefabObject, ObjectPoolFullStrategy fullStrategy)
        {
            GameObjectPool gameObjectPool = null;

            if (prefabObject != null && prefabObject.GetType() != typeof(GameObjectPool) && prefabObject.GetType() != typeof(GameObjectPoolManager))
            {
                GameObject gameObject = new GameObject();
                gameObject.transform.parent = base.gameObject.transform;
                gameObjectPool = gameObject.AddComponent <GameObjectPool>();
                if (gameObjectPool != null)
                {
                    gameObjectPool.PrefabToInstance = prefabObject;
                    gameObjectPool.UpdatePoolName();
                }
                if (fullStrategy != null)
                {
                    gameObjectPool.FullStrategy = fullStrategy;
                }
                else
                {
                    gameObjectPool.FullStrategy = new DropObject();
                }
                UpdatePoolMapping();
            }
            else if (prefabObject == null)
            {
                Logger.LogFatal(this, "Cannot create a pool for null prefab!", Logger.TagFlags.CORE | Logger.TagFlags.MEMORY);
            }
            else
            {
                Logger.LogFatal(this, $"Cannot create a pool of type: {prefabObject.GetType()}", Logger.TagFlags.CORE | Logger.TagFlags.MEMORY);
            }
            return(gameObjectPool);
        }
Exemplo n.º 2
0
        protected void Initialize()
        {
            if (m_fullStrategy == null)
            {
                GrowPool growPool = ScriptableObject.CreateInstance <GrowPool>();
                growPool.GrowthStrategy = ScriptableObject.CreateInstance <ExpandByAmount>();
                m_fullStrategy          = growPool;
            }
            int capacity = Capacity;

            if (m_objectPool != null)
            {
                Capacity     = 0;
                m_objectPool = null;
            }
            if (m_objectPool == null)
            {
                m_objectPool = new ObjectPool <GameObject>(capacity, m_fullStrategy, OnAllocateObject, OnObjectAllocated, OnObjectDeallocated, OnObjectUnspawned, OnReplaceObject);
                Capacity     = capacity;
            }
            UpdatePoolName();
        }
Exemplo n.º 3
0
 public ObjectPool(int initalCapacity, ObjectPoolFullStrategy fullStrategy, AllocateObject customAllocateObjectDelegate, ObjectAllocated customObjectAllocatedDelegate, ObjectDeallocated customObjectDeallocatedDelegate, ObjectUnspawned customObjectUnspawnedDelegate, ReplaceObject customReplaceObjectDelegate)
 {
     AllocateObjectDelegate = OnAllocateObject;
     if (customAllocateObjectDelegate != null)
     {
         AllocateObjectDelegate = customAllocateObjectDelegate;
     }
     ObjectAllocatedDelegate = null;
     if (customObjectAllocatedDelegate != null)
     {
         ObjectAllocatedDelegate = customObjectAllocatedDelegate;
     }
     ObjectDeallocatedDelegate = null;
     if (customObjectDeallocatedDelegate != null)
     {
         ObjectDeallocatedDelegate = customObjectDeallocatedDelegate;
     }
     ObjectUnspawnedDelegate = null;
     if (customObjectUnspawnedDelegate != null)
     {
         ObjectUnspawnedDelegate = customObjectUnspawnedDelegate;
     }
     ReplaceObjectDelegate = OnReplaceObject;
     if (customReplaceObjectDelegate != null)
     {
         ReplaceObjectDelegate = customReplaceObjectDelegate;
     }
     FullStrategy        = fullStrategy;
     m_inactiveInstances = new Stack <T>(initalCapacity);
     m_activeInstances   = new List <T>(initalCapacity);
     AllocateObjects(initalCapacity);
     if (FullStrategy == null)
     {
         FullStrategy = ScriptableObject.CreateInstance <ReplaceOldest>();
     }
 }
Exemplo n.º 4
0
 public ObjectPool(int initalCapacity, ObjectPoolFullStrategy fullStrategy)
     : this(initalCapacity, fullStrategy, (AllocateObject)null, (ObjectAllocated)null, (ObjectDeallocated)null, (ObjectUnspawned)null, (ReplaceObject)null)
 {
 }