I’ve been tasked with implementing asset pooling in our project, and after my first implementation have found some strange results which I’ve replicated in a test project.
I wrote a test script (at the end of this message) which takes a test object as parameter and then either creates/destroys copies of it every frame, or activates/deactivates the copies (using SetActiveRecursively).
Using a standard cube as my test object, 2000 copies, the profiler shows the following:
Instantiating/Destroying the objects on the left, activating and deactivating on the right. Creating/destroying the objects take about 115ms on the CPU, Activating/Deactivating take about 315ms.
Why does activating and deactivating take more time than creating and destroying the same amount of objects? Using a blank object as test case does not impact the results of the test.
It would be great if someone else could take my script and try to reproduce my findings. If needed I can upload a test project, but it is simple enough using my script below: Create a blank project, add 2 gameobjects to the scene, add the script to one and assign the other object to TestObject, then run and press F7 to create/destroy or F8 to activate/deactivate the copies.
Bonus question: The purple spikes in GPU usage are all due to RenderTexture.SetActive. What could be causing that in an absolutely blank project?
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class ActiveTest : MonoBehaviour {
bool toggleActive = false;
bool createDestroy = false;
public GameObject TestObject;
public int amount = 2000;
private List<GameObject> TestObjects;
void Start()
{
TestObjects = new List<GameObject>();
for (int i = 0; i < amount; i++)
TestObjects.Add (Instantiate(TestObject,TestObject.transform.position + (Vector3.right) * i,Quaternion.identity) as GameObject);
}
void Update ()
{
if (Input.GetKeyDown(KeyCode.F8))
{
toggleActive = !toggleActive;
if (toggleActive)
createDestroy = false;
if (!createDestroy TestObjects.Count == 0)
{
for (int i = 0; i < amount; i++)
TestObjects.Add (Instantiate(TestObject,TestObject.transform.position + (Vector3.right) * i,Quaternion.identity) as GameObject);
}
}
if (Input.GetKeyDown(KeyCode.F7))
{
createDestroy = !createDestroy;
if (createDestroy)
toggleActive = false;
else
{
if (TestObjects.Count == 0)
{
for (int i = 0; i < amount; i++)
TestObjects.Add (Instantiate(TestObject,TestObject.transform.position + (Vector3.right) * i,Quaternion.identity) as GameObject);
}
}
}
if (toggleActive)
{
TestObject.SetActiveRecursively(!TestObject.active);
foreach (var TO in TestObjects)
TO.SetActiveRecursively(TestObject.active);
}
else if (createDestroy)
{
if (TestObjects.Count == 0)
{
for (int i = 0; i < amount; i++)
TestObjects.Add (Instantiate(TestObject,TestObject.transform.position + (Vector3.right) * i,Quaternion.identity) as GameObject);
}
else
{
foreach (var to in TestObjects.ToArray())
{
TestObjects.Remove(to);
Destroy(to.gameObject);
}
}
}
}
}
