Hello, I’m hoping for some help with optimization.
I’m building an iPhone game with a moving object that leaves a trail. I do this through an instantiated list of simple 2-poly planes with a tiny textured material on them. This works ok for short paths, but once the trail starts getting several hundred points on it, things get laggy. I have a couple of options in terms of changing the design to draw less, but I’d really rather not. Are there amazing tricks to optimize this sort of thing? The ‘trail’ objects never mover (although the camera can move in relation to them) and are only ever using 1 or 2 materials.
- Don’t generate a new copy of the mesh vertices tris array, keep a copy to alter and reassign - you otherwise generate trash arrays on each point add / remove which will flood the cpu - if you enable the in app profiler in Appcontroller.mm you should see that
- Ensure the material does not use blend nor alpha so you don’t get fillrate hit (problem on ipad1, iphone4, itouch4 otherwise)
- Just to be on the sure side, but if you use pre 3GS hardware, keep in mind that it is significantly weaker on cpu and drawcalls and that you are severely limited
generally: unless you are 100% sure what the root is, go with the above mentioned profiler to even be sure to what it is related 
Ensure the trails are optimised thus:
- make sure sharedMaterial is set for the trail quads or it will not batch.
- make sure the trail quads all have a scale of 1,1,1 or it will not batch.
Providing you’re instantiating only a couple per frame it shouldn’t get markedly slower. What you aren’t getting though, is dynamic batching I suspect. Turn on the stats window on the Game window and tell me how many draw calls you’re experiencing. This is probably the real issue.
But if they’re not batching it doesn’t matter. They will crap all over your CPU in no time at all.
for a trail you would modify the mesh, not spawn more and more objects. That definitely would kill the performance as even dynamic batching has limits on how much you can throw at it without paying for the hundreds of array combines a frame for the dynamic batch …
if it uses the unity trail component, it won’t batch at all, just as a side note on the matter.
Thanks guys! I suspect that the procedural solutions are what I should be aiming for, but while I’m trying to figure those things out I’m also going to try to get batching working properly. I checked my stats window and there’s definitely no batching happening. I’m not sure why.
This is the code that creates a new point on the trail:
public void CreateRed(Vector3 pos){
pos = new Vector3(pos.x, pos.y, pos.z+10);
Quaternion rot = Quaternion.identity;
trail.Add((GameObject)Instantiate(_redTemplate, pos, rot));
trail[_trailSize].transform.Rotate(new Vector3(0,180,0));
_trailSize++;
}
And I’m attaching an image of the _redTemplate GameObject. If there’s some apparent reason why batching isn’t happening, please let me know.
I’ve already told you why.
I fixed the scaling issue you mentioned. I’m trying to figure out the sharedMaterial aspect. I added:
public Material _red;
//in CreateRed()
trail[_trailSize].renderer.sharedMaterial = _red;
But that isn’t changing anything. Could you point me towards a reference on how to implement sharedMaterial?