Resolved the obstacle spawning issue
This commit is contained in:
@@ -100,7 +100,7 @@ namespace Pooling
|
||||
/// <returns>An object ready to use</returns>
|
||||
public virtual T Get(int prefabIndex)
|
||||
{
|
||||
T obj;
|
||||
T obj = null;
|
||||
|
||||
// Track usage frequency
|
||||
if (prefabUsageCount.ContainsKey(prefabIndex))
|
||||
@@ -112,27 +112,71 @@ namespace Pooling
|
||||
prefabUsageCount[prefabIndex] = 1;
|
||||
}
|
||||
|
||||
// Try to get a valid object from the pool, cleaning up any destroyed objects
|
||||
if (pooledObjects.ContainsKey(prefabIndex) && pooledObjects[prefabIndex].Count > 0)
|
||||
{
|
||||
obj = pooledObjects[prefabIndex].Pop();
|
||||
totalPooledCount--;
|
||||
Debug.Log($"[{GetType().Name}] Found {pooledObjects[prefabIndex].Count} objects in pool for prefab index {prefabIndex}");
|
||||
|
||||
// Keep trying until we find a valid object or the pool is empty
|
||||
while (pooledObjects[prefabIndex].Count > 0)
|
||||
{
|
||||
obj = pooledObjects[prefabIndex].Pop();
|
||||
totalPooledCount--;
|
||||
|
||||
// Check if the object is still valid (not destroyed)
|
||||
if (obj != null && obj.gameObject != null)
|
||||
{
|
||||
Debug.Log($"[{GetType().Name}] Retrieved valid object {obj.name} from pool, current active state: {obj.gameObject.activeInHierarchy}");
|
||||
break; // Found a valid object
|
||||
}
|
||||
else
|
||||
{
|
||||
// Object was destroyed, continue looking
|
||||
Debug.LogWarning($"[{GetType().Name}] Found destroyed object in pool, removing it");
|
||||
obj = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// Create new object without adding to pool
|
||||
T prefab = prefabs[prefabIndex];
|
||||
obj = Instantiate(prefab, transform);
|
||||
Debug.Log($"[{GetType().Name}] No objects in pool for prefab index {prefabIndex}, creating new one");
|
||||
}
|
||||
|
||||
// If we couldn't find a valid object in the pool, create a new one
|
||||
if (obj == null)
|
||||
{
|
||||
T prefab = prefabs[prefabIndex];
|
||||
obj = Instantiate(prefab, transform);
|
||||
Debug.Log($"[{GetType().Name}] Created new object {obj.name} from prefab, active state: {obj.gameObject.activeInHierarchy}");
|
||||
}
|
||||
|
||||
// Ensure the object is valid before proceeding
|
||||
if (obj == null || obj.gameObject == null)
|
||||
{
|
||||
Debug.LogError($"[{GetType().Name}] Failed to create valid object for prefab index {prefabIndex}");
|
||||
return null;
|
||||
}
|
||||
|
||||
// CRITICAL FIX: Reset position to safe location BEFORE activation
|
||||
// This prevents off-screen checks from triggering during spawn process
|
||||
Vector3 originalPosition = obj.transform.position;
|
||||
obj.transform.position = new Vector3(0f, -1000f, 0f);
|
||||
Debug.Log($"[{GetType().Name}] Moved object {obj.name} from {originalPosition} to safe position before activation");
|
||||
|
||||
Debug.Log($"[{GetType().Name}] About to activate object {obj.name}, current state: {obj.gameObject.activeInHierarchy}");
|
||||
obj.gameObject.SetActive(true);
|
||||
Debug.Log($"[{GetType().Name}] After SetActive(true), object {obj.name} state: {obj.gameObject.activeInHierarchy}");
|
||||
|
||||
// Call OnSpawn for IPoolable components
|
||||
IPoolable poolable = obj.GetComponent<IPoolable>();
|
||||
if (poolable != null)
|
||||
{
|
||||
Debug.Log($"[{GetType().Name}] Calling OnSpawn for object {obj.name}");
|
||||
poolable.OnSpawn();
|
||||
Debug.Log($"[{GetType().Name}] After OnSpawn, object {obj.name} state: {obj.gameObject.activeInHierarchy}");
|
||||
}
|
||||
|
||||
Debug.Log($"[{GetType().Name}] Returning object {obj.name} with final state: {obj.gameObject.activeInHierarchy}");
|
||||
return obj;
|
||||
}
|
||||
|
||||
@@ -152,33 +196,61 @@ namespace Pooling
|
||||
poolable.OnDespawn();
|
||||
}
|
||||
|
||||
// Check if we're under the maximum pool size for this prefab type
|
||||
bool keepObject = totalPooledCount < totalMaxPoolSize;
|
||||
// Always deactivate and parent the object
|
||||
obj.gameObject.SetActive(false);
|
||||
obj.transform.SetParent(transform);
|
||||
|
||||
// Additional constraint: don't keep too many of any single prefab type
|
||||
if (pooledObjects.ContainsKey(prefabIndex) &&
|
||||
pooledObjects[prefabIndex].Count >= maxPerPrefabPoolSize)
|
||||
// Initialize stack if it doesn't exist
|
||||
if (!pooledObjects.ContainsKey(prefabIndex))
|
||||
{
|
||||
keepObject = false;
|
||||
pooledObjects[prefabIndex] = new Stack<T>();
|
||||
}
|
||||
|
||||
if (keepObject)
|
||||
// Check if we need to trim this specific prefab type's pool
|
||||
if (pooledObjects[prefabIndex].Count >= maxPerPrefabPoolSize)
|
||||
{
|
||||
obj.gameObject.SetActive(false);
|
||||
obj.transform.SetParent(transform);
|
||||
|
||||
if (!pooledObjects.ContainsKey(prefabIndex))
|
||||
// Remove the oldest object from this prefab's pool to make room
|
||||
if (pooledObjects[prefabIndex].Count > 0)
|
||||
{
|
||||
pooledObjects[prefabIndex] = new Stack<T>();
|
||||
T oldestObj = pooledObjects[prefabIndex].Pop();
|
||||
if (oldestObj != null && oldestObj.gameObject != null)
|
||||
{
|
||||
Destroy(oldestObj.gameObject);
|
||||
totalPooledCount--;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Check global pool limit
|
||||
if (totalPooledCount >= totalMaxPoolSize)
|
||||
{
|
||||
// Find the prefab type with the most pooled objects and remove one
|
||||
int maxCount = 0;
|
||||
int prefabToTrim = -1;
|
||||
|
||||
foreach (var kvp in pooledObjects)
|
||||
{
|
||||
if (kvp.Value.Count > maxCount)
|
||||
{
|
||||
maxCount = kvp.Value.Count;
|
||||
prefabToTrim = kvp.Key;
|
||||
}
|
||||
}
|
||||
|
||||
pooledObjects[prefabIndex].Push(obj);
|
||||
totalPooledCount++;
|
||||
}
|
||||
else
|
||||
{
|
||||
Destroy(obj.gameObject);
|
||||
if (prefabToTrim >= 0 && pooledObjects[prefabToTrim].Count > 0)
|
||||
{
|
||||
T oldestObj = pooledObjects[prefabToTrim].Pop();
|
||||
if (oldestObj != null && oldestObj.gameObject != null)
|
||||
{
|
||||
Destroy(oldestObj.gameObject);
|
||||
totalPooledCount--;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Now add the current object to the pool
|
||||
pooledObjects[prefabIndex].Push(obj);
|
||||
totalPooledCount++;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
Reference in New Issue
Block a user