Performance issue in a very small scene

Hi there,

I have a pretty bad problem with my game. The performance is very low for what is onscreen.

At the moment it is a 2D game using planes with transparent vertex color shaders. At the very highest the vertex count hits about 120 (I am using a 2 triangle plane to hold up the images) and 7 draw calls. This should be running like butter, right? Because right now on the first launch I only get about 20 fps and the thing stutters like crazy every 10 seconds. What am I missing here?

I am guessing this has something to do with the garbage collector, but I don’t see too much reason for it to be stuttering anyway. There are no spawned/destroyed objects and very few variables. The memory should be pretty empty.

Thanks in advance. This whole iPhone dev. thing is new to me :slight_smile:

With 7 draw calls and 120 vertices, you shouldn’t be seeing slowdowns.

Are you doing any scaling? Because that can hurt performance.

If not, take a good look at your scripts, there might be something wrong there.

Try disabling them one by one while the game is running and see if you can narrow down which one is killing performance.

Here is the code for the player…

private var tiltForce : Vector3 = Vector3.zero;
private var tiltTorque : Vector3 = Vector3.zero;
private var torque = 5000.0;

var force : float;
var jumpSpeed = 0.0;

var grounded = false;

@script RequireComponent(Rigidbody)

function FixedUpdate() {

transform.position.z = 6.770766;
transform.rotation.y = 0.0;
transform.rotation.x = 0.0;
// keep the player on a 2d plane

	tiltForce.x = -iPhoneInput.acceleration.y;
	tiltTorque.z = iPhoneInput.acceleration.y;
	
	// add force to the player
	rigidbody.AddForce(tiltForce * force * Time.deltaTime);
	rigidbody.AddTorque(tiltTorque * torque * Time.deltaTime);
	
	// jump when the user touches the screen
	if (iPhoneInput.touchCount > 0  iPhoneInput.GetTouch(0).phase == iPhoneTouchPhase.Began) {
    if (grounded) {
	rigidbody.velocity.y = jumpSpeed;
	}
}
}

function Start() {
	var tmp = new System.Object[1024];
	// make allocations in smaller blocks to avoid them to be treated in a special way, which is designed for large blocks
        for (var i : int = 0; i < 1024; i++)
		tmp[i] = new byte[1024];
	// release reference
        tmp = null;
}

The bottom function is some memory allocation for the garbage collector.

EDIT: wow just found out that it was an object with a badly written script… :stuck_out_tongue:

Can you give your opinion on the player script though? Is it efficient enough?

Your script looks fine to me, but I haven’t really used physics yet, so hopefully someone with more experience will chime in and give their opinion on the efficiency of your code…

You should cache component lookups that you’re doing over and over again, such as rigidbody and transform. Look in the Unity iPhone docs about script optimization.

–Eric

Mmmmmk… Im getting 60fps now, albeit with some small little stutters every now and then… I cached the transforms - is there anything else or should I just leave it at that?

Avoid Native calls at all cost, try to cache everything.

These small shutters could be GC, but also could be iPhone OS background tasks. Enable interal profiler and check if them are caused by GC.

This is also my biggest trouble with Unity Iphone. Stutters everywhere… :cry:

The game suddenly pauses for some milliseconds and the objects “jump” when it resumes. This happens every 3-15 seconds, randomly. It doesn’t seem to be GC problem because I dont have an increasing heap. It seems to be the sensitivity of Unity to background OS processes. However this is disappointing for such simple scenes! It makes physics/animation useless…

This is a huge problem for a game which involves constant movement of objects for the whole gameplay session. Any stutter is very visible (especially with 60fps).

Below I am attaching a simple scene with 3 bouncing balls (meshes). I have tried every possible optimization I could find in the forums, with no success.

Unity team please help…

Some info:

On “Time” settings in player: have tried everything, left it with default (0.02, .0333, 1)

On appcontroller.mm: Loop=NSTimer with FPS=60 and throttle=1. Accelerometer frequency=1. (also tried other setups, NS seems the best here)

On device (iPhone 3G): Wifi, push notifications, bluetooth, location services, 3g = OFF
I have tested my app on a second device too (same results).

Typical Profile Stats:

iPhone Unity internal profiler stats:
cpu-player>    min:  2.3   max:  7.4   avg:  3.7
cpu-ogles-drv> min:  0.7   max:  1.0   avg:  0.9
cpu-present>   min:  6.4   max: 10.0   avg:  7.9
frametime>     min: 14.4   max: 19.0   avg: 16.7
draw-call #>   min:   2    max:   2    avg:   2     | batched:     7
tris #>        min:  1612  max:  1612  avg:  1612   | batched:  1612
verts #>       min:   918  max:   918  avg:   918   | batched:   918
player-detail> physx:  0.5 animation:  0.0 culling  0.3 skinning:  0.0 batching:  0.4 render:  1.6 fixed-update-count: 0 .. 1
mono-scripts>  update:  0.0   fixedUpdate:  0.3 coroutines:  0.0 
mono-memory>   used heap: 126976 allocated heap: 1089536  max number of collections: 0 collection total duration:  0.0
----------------------------------------
iPhone Unity internal profiler stats:
cpu-player>    min:  0.7   max:  8.6   avg:  4.6
cpu-ogles-drv> min:  0.4   max:  4.2   avg:  1.2
cpu-present>   min:  1.0   max:  7.8   avg:  1.5
frametime>     min: 13.2   max: 35.3   avg: 17.5
draw-call #>   min:   2    max:   2    avg:   2     | batched:     7
tris #>        min:  1612  max:  1612  avg:  1612   | batched:  1612
verts #>       min:   918  max:   918  avg:   918   | batched:   918
player-detail> physx:  0.4 animation:  0.0 culling  0.5 skinning:  0.0 batching:  0.6 render:  2.2 fixed-update-count: 0 .. 1
mono-scripts>  update:  0.0   fixedUpdate:  0.3 coroutines:  0.0 
mono-memory>   used heap: 126976 allocated heap: 1089536  max number of collections: 0 collection total duration:  0.0
----------------------------------------
iPhone Unity internal profiler stats:
cpu-player>    min: -1.4   max:  9.0   avg:  4.5
cpu-ogles-drv> min:  0.6   max:  4.8   avg:  1.3
cpu-present>   min:  1.2   max:  3.4   avg:  1.3
frametime>     min: 14.4   max: 18.2   avg: 16.7
draw-call #>   min:   2    max:   2    avg:   2     | batched:     7
tris #>        min:  1612  max:  1612  avg:  1612   | batched:  1612
verts #>       min:   918  max:   918  avg:   918   | batched:   918
player-detail> physx:  0.3 animation:  0.0 culling  0.5 skinning:  0.0 batching:  0.7 render:  2.4 fixed-update-count: 0 .. 1
mono-scripts>  update:  0.0   fixedUpdate:  0.2 coroutines:  0.0 
mono-memory>   used heap: 126976 allocated heap: 1089536  max number of collections: 0 collection total duration:  0.0

and other times…

iPhone Unity internal profiler stats:
cpu-player>    min:  0.4   max:  6.7   avg:  3.8
cpu-ogles-drv> min:  0.7   max:  2.8   avg:  1.0
cpu-present>   min:  1.3   max:  6.1   avg:  3.8
frametime>     min: 14.4   max: 18.5   avg: 16.7
draw-call #>   min:   2    max:   2    avg:   2     | batched:     7
tris #>        min:  1612  max:  1612  avg:  1612   | batched:  1612
verts #>       min:   918  max:   918  avg:   918   | batched:   918
player-detail> physx:  0.3 animation:  0.0 culling  0.4 skinning:  0.0 batching:  0.5 render:  1.7 fixed-update-count: 0 .. 1
mono-scripts>  update:  0.0   fixedUpdate:  0.2 coroutines:  0.0 
mono-memory>   used heap: 126976 allocated heap: 1089536  max number of collections: 0 collection total duration:  0.0
----------------------------------------
iPhone Unity internal profiler stats:
cpu-player>    min:  2.5   max: 11.9   avg:  4.4
cpu-ogles-drv> min:  0.4   max:  1.1   avg:  0.8
cpu-present>   min:  1.1   max:  5.4   avg:  3.6
frametime>     min: 14.0   max: 87.4   avg: 19.5
draw-call #>   min:   2    max:   2    avg:   2     | batched:     7
tris #>        min:  1612  max:  1612  avg:  1612   | batched:  1612
verts #>       min:   918  max:   918  avg:   918   | batched:   918
player-detail> physx:  0.5 animation:  0.0 culling  0.5 skinning:  0.0 batching:  0.5 render:  1.9 fixed-update-count: 0 .. 2
mono-scripts>  update:  0.0   fixedUpdate:  0.5 coroutines:  0.0 
mono-memory>   used heap: 126976 allocated heap: 1089536  max number of collections: 0 collection total duration:  0.0
----------------------------------------
iPhone Unity internal profiler stats:
cpu-player>    min:  2.2   max:  6.8   avg:  3.6
cpu-ogles-drv> min:  0.7   max:  1.1   avg:  0.8
cpu-present>   min:  2.8   max:  6.4   avg:  4.4
frametime>     min: 14.4   max: 18.9   avg: 16.6
draw-call #>   min:   2    max:   2    avg:   2     | batched:     7
tris #>        min:  1612  max:  1612  avg:  1612   | batched:  1612
verts #>       min:   918  max:   918  avg:   918   | batched:   918
player-detail> physx:  0.3 animation:  0.0 culling  0.4 skinning:  0.0 batching:  0.4 render:  1.8 fixed-update-count: 0 .. 1
mono-scripts>  update:  0.0   fixedUpdate:  0.2 coroutines:  0.0 
mono-memory>   used heap: 126976 allocated heap: 1089536  max number of collections: 0 collection total duration:  0.0

I have found that 3 things can help a bit, but are not enough to eliminate the stutter:

  1. Run the game with the device unplugged!
  2. After a Unity “Build Run” go to XCode, change something minor in appcontroller.mm , save the file and then just “Go” (click continue to the error message). Seems to be pure voodoo, but it partially works for a reason.
  3. Set device to Airport mode. This makes the biggest difference (-60% of stutters).

However, all the above are things the developer cannot ask from the player to do…

I am attaching the scene that gave the results above. However, the same problem exists even for the simplest scene (even with just a camera).

Please help…

279126–9999–$stutters_100.unitypackage (35.2 KB)

Yeah, my 2D platformer suffers from this as well. I’ve gone through a lot of steps to eliminate runtime allocations so its not garbage collection. My draw call and vert counts are low, most everything that is cached can be.

I also think its a background sensitivity issue, but I don’t have the technical background required to back up my claims with data.

Although this thread (which is old) seems indicate that the web version has the same issue. Maybe its just an issue with in the engine in general?

Thats funny because my settings are almost exactly the same. At least when the spikes occur, the game slows down instead of skips.

To double check that the reason for the stutters is Unity and not XCode or my device I compiled a simple game made with cocos2d iphone which also involves constant moving of an object at 60hz. It is actually a tutorial project the code of which you can find here.
Result: it runs smoothly at 59-60hz without any stutter, even with excessive touching and high velocity bouncing.

This confirms that it is Unity that breaks the performance and not the device or xcode.

However, I can also see that well-known Unity games such as Zombieville and Skee-Ball run smoothly on my device without any stutter.

So only two possible scenarios remain possible:

a) There is something I am doing wrong with Unity (but my scene/setup is so simple that I cannot imagine what)

b)Unity Iphone is extremely sensitive to background processes, but this becomes visible only in games with constant movement, like the attached above.

Any thoughts?

Well, there are lots of things that can cause stutters. Background tasks for sure. Loading textures or sounds for the first time is a common culprit too and easily avoided if you load them in advance (rather than the first time the player comes across them). Instantiating objects can cause hitches too, one of many reasons why I make pools of objects off screen and draw from the pool as needed rather than creating/destroying objects constantly. There’s also a brief “warm up” period for Unity scenes in my experience (on the device), where its a bit unstable for 10-15 seconds after my splash screen disappears, but stabilizes afterwards.

If you guys can send me a bare project that is affected by this, I’ll take a look and find out what is causing it. The only real way of figuring it out is to see this in action.

The first few seconds of stabilizing can be accelerated by pre-allocating directly from mono, more here: Heap pre-allocation to boost startup http://forum.unity3d.com//viewtopic.php?t=31838, I got my game running smooth after 2 or 3 seconds. I can find what pre-alloc amount is best for your case.

With the last few Unity iPhone updates, things are running even better, and I get steady 60 fps. So if you guys are getting hitches there is definitely something to find and fix.

EDIT: I’m checking the stutters.unityPackage, and will let you guys know if I find anything. But feel free to send me any other case. I’ll be on a long flight tonight and can use the entertainment.

EDIT: The stutters.unityPackage doesn’t hitch for me. It’s from an old post, so it might have been fixed with recent Unity updates.

@MikaMobile (where did PirateNinjaalliance go ?!)
I have already tracked down the methods you propose for performance stability. They all are a must for a game. However, I don’t know if they apply here, since the problem occurs even for the simplest scene. In stutter.package above the scene has just 3 balls and some constraining cubes on the edges of the screen. No textures/sounds, no heavy scripts and no instantiation/destroy of objects. The balls and colliders are not instantiated with code, they have been placed on screen from the scene setup.
The “warm up” period seems to be extended for ever here…

@rozgo
Your “memory pre-allocation” neat trick is something I have already tested (with high anticipation), but it didn’t help either. This was reasonable, since there is no memory consumption in the stutter.package. The used memory is always the same (see profiler results in my previous message).
Unfortunately, the stutter.package (and generally my stutter problem) is not old. It has been made with Unity Iphone 1.6 and compiled with xcode 3.1.4 (“release” compilation). My device also wears the latest OS (but I have compiled both for that and for 2.2.1 with no difference in stutters).

Besides these, the fact that you don’t see any stutters in stutter.package deeps me in thoughts…
I have already re-installed Unity and XCode twice the previous days. On my iphone device everything is set to “off” and the OS is up to date too. Other Unity games run smoothly, so the device is not the problem.

What else could be causing the problem from my side?

Is everybody here using a 3G? Just another variable to consider.

I’ve played Zombieville on my 3G and didn’t experience any performance hitches.

I am using a 3G device, and have no stutters in Zombieville too. I also don’t see any stutters in dozens of other games I have downloaded (Unity and others).

Don’t see what else to try. I feel like i’m cursed…

For the majority of developers here, the new OS4 is a possible threat for their Unity projects. For me it was already a cursed path, because of the unexplained stutters even in the simplest scene setup…

UPDATE
I also tried with no success:

  1. Reinstalling Unity iPhone (1.5-6-7)
  2. Reinstalling Xcode (the last 3 versions)
  3. Installing Snow Leopard (I was on 10.5)
  4. Put a “loading” scene(0) and have the game at scene(1)
  5. Instantiate the balls and colliders in loading scene(0).

Number 4 give me some better stability, but the result lacks consistency.

Did anyone else tried to compile the “stutters” package from the previous page? Do you see any stutters?

Thanks

Well, for what it’s worth, I’m a pure C programmer, and on my 2G I timed the frame durations and every 20 or so frames I got a spike of about 8 ms, pushing even the tightest update loop (that involves graphics) over 1/60th. I suspect it’s an iPhone background thread doing something expensive.

So what probably happens (and why you don’t see it in the Cocos2D sample) is that that spike is still there on the 3G, however the Cocos2D update loop is tighter and thus doesn’t push the frame over 16.66 ms, while it does for Unity, after all, being general purpose takes some sacrifice.

Another thing could be that the Cocos2D sample uses proper timestepping, whereas the Unity code doesn’t, so a stutter on the Cocos2D sample would be less noticable.

The easiest solution is to aim for 30fps, you’ll have a lot of breathing room all of a sudden and it’s really smooth enough for a game (after all, most movies have been running at 32 (24?) fps for decades and noone complained about the movies not being smooth. As long as you properly timestep your code noone will know.

If you really want I could see if I see the same spike on the 3G. I’d have to see if I can find the code somewhere…my iPhone development kinda stalled wright after it started ith deadlines at work taking over what used to be called life.

Kaj

noocell

It looks like the stutter is caused through you Z restriction script on the balls. Let the scene run for about a minute and and rotate around the scene inside Unity’s “Scene View”. You will see that the three ball elements will stutter not on the Y direction but on the Z direction. I also added a ball that doesn’t show this behavior.

Here is a link how you could replace your current scripting. :wink:

http://vonlehecreative.com/blog/video-games/unity-resource-velocitylimiter/

Peter.

311615–11013–$stutters_101_232.unitypackage (37.3 KB)

Make sure your image planes are SINGLE POLY. If you are using the built in image planes remember they are mostly used for Water surfaces, or cloth, so they have much much more draw calls and poly counts. Simply build a UV’ed single or double or quad poly plane, and load it into your scene. And you will thank me.