public override GameObject GetObjectToReplace(GameObjectPool pool, List <GameObject> activeInstances, Stack <GameObject> inactiveInstances) { int num = int.MaxValue; GameObject gameObject = null; for (int i = 0; i < activeInstances.Count; i++) { PriorityComponent component = activeInstances[i].GetComponent <PriorityComponent>(); if (component != null) { int priority = component.Priority; if (priority < num) { num = priority; gameObject = activeInstances[i]; } } else { Logger.LogFatal(activeInstances[i], "Attempting to replace pooled instance based on priority, however the game object or prefab is missing the required PooledPriorityComponent!", Logger.TagFlags.CORE | Logger.TagFlags.MEMORY); } } if (gameObject != null) { gameObject.SendMessage("OnStolenFromPool", SendMessageOptions.DontRequireReceiver); PriorityComponent component = gameObject.GetComponent <PriorityComponent>(); if (component != null) { component.Reset(); } } return(gameObject); }
public void TestSpawnWithPositionAndRotationAndPriorityAndLifetime() { GameObject gameObject = new GameObject(); Assert.IsNotNull(gameObject); gameObject.name = "LifetimeObject"; gameObject.AddComponent <LifetimeComponent>(); LifetimeComponent component = gameObject.GetComponent <LifetimeComponent>(); Assert.IsNotNull(component); component.SetLifetime(1f); gameObject.AddComponent <PriorityComponent>(); PriorityComponent component2 = gameObject.GetComponent <PriorityComponent>(); Assert.IsNotNull(component2); component2.Priority = 1; m_objectPool.PrefabToInstance = Object.Instantiate(gameObject); Assert.IsNotNull(m_objectPool.PrefabToInstance); m_objectPool.OnEnable(); Vector3 position = new Vector3(0f, 0f, 0f); Quaternion rotation = Quaternion.Euler(new Vector3(0f, 0f, 0f)); for (int i = 0; i < InitialPoolSize; i++) { Assert.IsNotNull(m_objectPool.Spawn(position, rotation, 4, 1f)); } Object.DestroyImmediate(m_objectPool.PrefabToInstance); Object.DestroyImmediate(gameObject); }
public void TestReplacementStrategyLowestPriority() { GameObject gameObject = new GameObject(); Assert.IsNotNull(gameObject); gameObject.name = "PriorityObject"; gameObject.AddComponent <PriorityComponent>(); PriorityComponent component = gameObject.GetComponent <PriorityComponent>(); Assert.IsNotNull(component); component.Priority = 1; m_objectPool.PrefabToInstance = Object.Instantiate(gameObject); Assert.IsNotNull(m_objectPool.PrefabToInstance); m_objectPool.FullStrategy = ScriptableObject.CreateInstance <ReplaceLowestPriority>(); m_objectPool.OnEnable(); GameObject gameObject2 = null; for (int i = 0; i < InitialPoolSize; i++) { if (i == InitialPoolSize / 2) { gameObject2 = m_objectPool.Spawn(1); Assert.IsNotNull(gameObject2); } else { Assert.IsNotNull(m_objectPool.Spawn(5)); } } GameObject actual = m_objectPool.Spawn(5); Assert.AreEqual(gameObject2, actual); Object.DestroyImmediate(m_objectPool.PrefabToInstance); Object.DestroyImmediate(gameObject); }
public void TestSpawnWithTrandformAndPriorityAndLifetime() { GameObject gameObject = new GameObject(); Assert.IsNotNull(gameObject); gameObject.name = "LifetimeObject"; gameObject.AddComponent <LifetimeComponent>(); LifetimeComponent component = gameObject.GetComponent <LifetimeComponent>(); Assert.IsNotNull(component); component.SetLifetime(1f); gameObject.AddComponent <PriorityComponent>(); PriorityComponent component2 = gameObject.GetComponent <PriorityComponent>(); Assert.IsNotNull(component2); component2.Priority = 1; m_objectPool.PrefabToInstance = Object.Instantiate(gameObject); Assert.IsNotNull(m_objectPool.PrefabToInstance); m_objectPool.OnEnable(); for (int i = 0; i < InitialPoolSize; i++) { Assert.IsNotNull(m_objectPool.Spawn(m_objectPool.transform, 4, 1f)); } Object.DestroyImmediate(m_objectPool.PrefabToInstance); Object.DestroyImmediate(gameObject); }
public void TestSpawnWithPriority() { GameObject gameObject = new GameObject(); Assert.IsNotNull(gameObject); gameObject.name = "PriorityObject"; gameObject.AddComponent <PriorityComponent>(); PriorityComponent component = gameObject.GetComponent <PriorityComponent>(); Assert.IsNotNull(component); component.Priority = 1; m_objectPool.PrefabToInstance = Object.Instantiate(gameObject); Assert.IsNotNull(m_objectPool.PrefabToInstance); m_objectPool.OnEnable(); for (int i = 0; i < InitialPoolSize; i++) { Assert.IsNotNull(m_objectPool.Spawn(5)); } Object.DestroyImmediate(m_objectPool.PrefabToInstance); Object.DestroyImmediate(gameObject); }