Have you tried the Detonator Explosion Framework, you should it’s great fun.
But being a bit of a performance nut I had to see if I could tweak out a bit more performance from it!
Note: Detonator dynamically sets the components and materials it needs during the construction of the Detonator so for best results ensure all materials are present in your Detonator prefab!
Unity profiler is cool!
To Optimise code simply setup a test scene, where you profile the performance of an aspect of your game on it’s own (preferably one that shows up in the game as a performance hog Rule 1).
Now run it and see what tweaks you can make to reduce it’s impact e.g: in the case of detonator you explosions detail parameter.
The lists elements by ms timings, now to get the best detail you need to use Deep profiling.
And this will show up where your scripts are taking time.
-
In most cases inner loops are areas where lot of calls to the same code can have a massive impact.
-
Things that I have noticed getting a game objects transform is slow if your using it more than once set it to a local variable, ditto for other object properties.
-
Vector3 maths is slow, you can unroll this into a vector constructor
-
Doing the same calculation more than once in a loop!
Transform trans = this.transform; //*2 Localise
Vector3 pos = trans.position; //*2 Localise
Quaternion rot = trans.rotation; //*2 Localise
float s = startingRadius * size; // *4 calc once use many
int detailCount = (int)(detail * count);
for (int i=0;i<detailCount;i++)
{
Vector3 randVec = Random.onUnitSphere; //* (startingRadius * size);
randVec = new Vector3(randVec.x * s , randVec.y * s , randVec.z * s );
Vector3 posVec = new Vector3(randVec.x + pos.x, randVec.y + pos.y, randVec.z + pos.z); //*3 Unroll Vector3 maths
Vector3 velocityVec = new Vector3((velocity.x*size),(velocity.y*size),(velocity.z*size));
GameObject chunk = Instantiate(sprayObject, posVec, rot) as GameObject;
Transform chunkTrans = chunk.transform; //*2 Localise
chunkTrans.parent = trans;
//calculate scale for this piece
_tmpScale = (minScale + (Random.value * (maxScale - minScale)));
_tmpScale = _tmpScale * size;
chunkTrans.localScale = new Vector3(_tmpScale,_tmpScale,_tmpScale);
Vector3 norm = randVec.normalized;
chunk.rigidbody.velocity = new Vector3(norm.x * velocityVec.x, norm.y * velocityVec.y, norm.z * velocityVec.z ); //Vector3.Scale(randVec.normalized,velocityVec);
Destroy(chunk, (duration * timeScale));
_delayedExplosionStarted = false;
_explodeDelay = 0f;
}
Here is the result: http://dl.dropbox.com/u/19148487/DetonatorOpt1.unitypackage
I’m also dropping a line to Ben Throop in the hope that this update will make it to the next Detonator build!
Well I had best update EIC with this!
Thank You
PS If you manage to get more performance from Detonator please share!