- Home /
Object pooling question.
Currently using the object pooling script of the above link. and added the following method to handle the activated object.
// GameObjectPooler.cs
public void handleActivatedPooledObjects(Action<GameObject> callbackHandle)
{
for (int i = 0; i < pooledObjects.Count; ++i)
{
if (pooledObjects[i].activeSelf)
{
callbackHandle(pooledObjects[i]);
}
}
}
// use
aaaPooler.handleActivatedPooledObjects((pooledObject) =>
{
pooledObject.GetComponentInChildren<aaa>().deactivate();
});
Is it okay? And at first I tried to pass the active object to the list, this would cause garbage in C#?
If you know for a fact that the children will never change and always be the same for each object you should cache the childrenComponents for optimal performance like so:
private Dictionary<GameObject, aaa> childrenComponentCache = new Dictionary<GameObject, aaa>();
private void DeactivateChildrenComponents()
{
var childrenComponent;
if (!childrenComponentCache.TryGetValue(pooledObject, out childrenComponent))
{
childrenComponent = pooledObject.GetComponentInChildren<aaa>();
childrenComponentCache.Add(pooledObject, childrenComponent);
}
foreach (var comp in childrenComponent)
{
comp.deactivate();
}
}
Thanks for reply. I have some questions.
Never change mean that the child is still attached without being deleted or moved?
Do not call GetComponentInChildren and look for the aaa instance directly in the Dictionary?
Declare only one dictionary variable and use it publicly?
handle the same for GetComponent?
GetComponent is less extensive than others, I believe it is already internally cached. $$anonymous$$y method gets the aaa component of the children of the pooledObject if it not already has it in the dictionary and puts it in, next time it will take the aaa object from the dictionary. aslong as new children don't get added it should be fine. And you can have multiple dictionaries for multiple caching.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
enemy pooling 1 Answer
Flip over an object (smooth transition) 3 Answers
Pooling GameObjects with TextMesh ? 0 Answers