- Home /
iOS performance problem (iphone 4 on iOS6)
Dear all
I have a problem with the iOS performance of a simple 2D game.
Normally, it runs very smoothly showing stat A. Occasionally, I notice during collision detection, there is a noticeable delay .
I have tried to move a lot of the codes away from Update(), and put the codes in coroutine. this did improve the performance slightly but still the delay persists. Then I changed all my colliders from 3D to 2D. Also I replaced the default onTriggerEnter/Exit/Stay with custom-made functions using raycasting to detect trigger events.
Still I have a delay problem every now and then (see stat B)
Any clue how I could address this problem? thanks a lot for your help!
A
----------------------------------------
iPhone Unity internal profiler stats:
cpu-player> min: 3.8 max: 12.0 avg: 7.2
cpu-ogles-drv> min: 0.5 max: 2.6 avg: 0.9
cpu-present> min: 0.8 max: 162.9 avg: 6.8
cpu-waits-gpu> min: 0.8 max: 162.9 avg: 6.8
msaa-resolve> min: 0.0 max: 0.0 avg: 0.0
frametime> min: 12.2 max: 193.5 avg: 37.1
draw-call #> min: 7 max: 7 avg: 7 | batched: 2
tris #> min: 314 max: 314 avg: 314 | batched: 4
verts #> min: 628 max: 628 avg: 628 | batched: 8
player-detail> physx: 0.7 animation: 0.3 culling 0.0 skinning: 0.0 batching: 0.2 render: 3.5 fixed-update-count: 1 .. 9
mono-scripts> update: 2.4 fixedUpdate: 0.0 coroutines: 0.2
mono-memory> used heap: 585728 allocated heap: 786432 max number of collections: 0 collection total duration: 0.0
B
----------------------------------------
iPhone Unity internal profiler stats:
cpu-player> min: 3.0 max: 436.8 avg: 20.9
cpu-ogles-drv> min: 0.5 max: 3.5 avg: 1.1
cpu-present> min: 0.8 max: 6.0 avg: 1.9
frametime> min: 22.4 max: 470.1 avg: 46.9
draw-call #> min: 8 max: 10 avg: 8 | batched: 2
tris #> min: 218 max: 228 avg: 224 | batched: 4
verts #> min: 436 max: 448 avg: 442 | batched: 8
player-detail> physx: 0.9 animation: 0.1 culling 0.0 skinning: 0.0 batching: 0.1 render: 3.2 fixed-update-count: 1 .. 17
mono-scripts> update: 1.6 fixedUpdate: 0.2 coroutines: 14.5
mono-memory> used heap: 618496 allocated heap: 786432 max number of collections: 0 collection total duration: 0.0
How much garbage are you generating? Periodic hitching could be collision if it happens when a lot of objects collide, but if it's just kind of "random" then garbage collection could be the problem.
it's very hard without the profiler. Just FTR, commercially, I just don't know if iOS6 matters much anymore. 90%+ of income comes from people with normal phones, not ancient ones.
Dear all
thanks for your kind reply! it is very helpful. I have been doing some research online and going through my codes. I did use a lot of instantiate() and destroy(). For instance, when an enemy is destroyed, some coins are "instantiated" for collection (and destroyed once collected)
Unfortunately I don't have Unity Pro, but I am guessing I need to study a bit on Object Pooling to avoid frequent instantiation and object destruction. I hope this will solve my problem!
RayCasting will have slower performance than Collision/Trigger functions aside from Stay.
OnCollisionStay and OnTriggerStay will really chew up the CPU as it bangs out every frame ins$$anonymous$$d of just once. If you have just 1 CollisionStay it can really damage performance. Having many == Death to App.
Answer by Tarlius · May 23, 2014 at 05:23 PM
"Delay problems" usually points at the Garbage Collector. If you are using Unity Pro (I forgot if you need pro for that info) you can use the Profiler to look at the GC allocations which will eventually add up to the point the GC kicks in and kicks your performance in the balls for a frame.
If Unity Free.... Ummm... You're stuck checking the code yourself :/
Basically, try not to allocate "new" objects every frame. Pretty much any time you use the "new" keyword with a class (as opposed to a struct), you're asking for GC-managed memory and when it runs out it checks which objects don't have references and frees the memory for reuse which is rather slow.
I am a bit stuck now! I have implemented an object pool function that instantiates all needed objects in the game and activate and deactivate them during the game, thus removing the need of instantiate() and destory(). the problem still won't go away!!! I don't think it is the collision either as I only use collision to detect if the player touches the object....
what else could have caused this problem??
as it turns out, the main culprit in this problem is NGUI which I use for GUI. I display two text labels for scores and lives with NGUI and once I removed them, everything seems to be running smoothly! curses!
NGUI is generally pretty lightweight... I guess its possible you hit an edge case or something, but I think its more likely theres a different underlaying issue :/
Without access to the profiler its hard to help much, but gc still sounds likely.
What are you actually doing in the collision logic? How many objects do you have?
Hey @jim - here's my "famous" cough explanation of pools for new programmers (not us guys!) who were asking about it.
http://answers.unity3d.com/questions/321762/how-to-assign-variable-to-a-prefabs-child.html
Conceivably, you have some woe in your pool handling (maybe, looking up a game object too often or something). You may enjoy reading it. Please vote it up as I need moar points :)
i worry about using ngui. and i jus don't like unity's gui. (although it has no performance problems) do you have "a lot of gui"? we always just use 2dtoolkit and do the gui as totally normal, ordinary, stuff in the 3d scene, the "normal" way to do buttons etc. 2dtoolkit has button functions etc builtin and ready to go.
Performance is difficult, Jim. We recently did quite a big project for a client, with unity i mean, that had a lot of flat images. in fact - in the end it just wasn't possible to use unity. we re-did the whole project as a normal native project.
On the other hand very often there's just "some stupid thing" that 's causing the woe.
Your answer
Follow this Question
Related Questions
Unity iOS sdk question 1 Answer
iOS Shader - Emulator vs Device Inconsistency 0 Answers
WebCamTexture and VSync (iPhone 4) 3 Answers
Depth of Field Mobile 0 Answers
Making the switch to iPhone 1 Answer