Questions about iOS optimization: some weird puzzle

Hi folks,

I have met several puzzle while optimizing our project for iOS:

Our current project is a 2D arcade game with a lot of spawns, popping score texts(using SpriteText from Sprite Manager 2) and repeating sfx. We recently added a lot of sfx and music into the game and finished the sound scripting, since then the performance dropped quite a lot and we have to put a lot of effort into optimization.

We searched the forum upside down, we use every mentioned optimization method such as pre-allocated heap, use object pools to eliminate instantiate at runtime, cache component in a variable, use ToString() instead of String.Format, reduce file size of audio and texture assets, etc.

Q1.
But the “used heap” number in internal profiler still gets up quickly every tick. Are there any other allocation that can be prevent in scripting? We seems to have done enough except for convert an int value to a string so we can display a popping score on screen. But it seems that this kind of allocation is inevitable, is that true?

Q2.
Also our game runs much smoother on iPad compare to iPhone4. How’s that possible? Does that have something to do with iPhone4 having more memory available so when garbage collection happens it takes out more trash than iPad does? But from reading internal profiler I can’t confirm that, both device takes out about 900k heap when GC occurs, or more depending on how much heap you pre allocated. How does the performance difference happen? Does anyone ever have the same experience?

Q3.
My last question (sorry for asking so much:p), in order to play the same audioclip multiple times in a short period(overlapping may happen), what’s the best method performance wise? I’ve read several post about play multiple audio clip but they have different opinions. For example,

http://forum.unity3d.com/threads/17165-Playing-sound-causes-gradual-performance-loss

In this thread it’s said PlayOneShot() will cause performance issue so using multiple sound source to cycling is better. But I also read here that:

http://forum.unity3d.com/threads/39548-Number-of-audio-sources-question-(optimization)

They say using PlayClipAtPoint is “quick and efficient”.

Which one should I go for? I have some quick sound that gets played whenever there’s an input. So it can be played 5 times within a second. I want to ensure the sound will not interrupt each other and won’t have an impact on performance.

Thanks a lot for reading, any tip would be highly appreciated :slight_smile:

nantas

Some small thoughts:

Q1: Converting int to string for display is not needed, it is an intermediate step that can be eliminated. The chars in a string are eventually converted to polygons and texture coordinates, the same can be done by mathematically extracting digits from an int. Of course you need a text solution that supports this.
Also, if you’re using coroutines, do not create new WaitForSecond or WaitForFixedUpdate class instances all the time. Starting a new coroutine also allocates.

Q3: An advantage of the pool approach - beyond recycling - is control. You can easily limit how much sounds play at once. If a lot is going on at the same time, you can get away with not playing all of it. Only play the most important stuff and discard the rest.

@JasperFlick

Thanks a lot for the reply!

About WaitForSeconds, is there any difference using

yield WaitForSeconds()
and
yield return new WaitForSeconds()?

If they both allocate, what’s a valid substitution to replace it?

Thanks!

In both cases it’s a call to the constructor of the WaitForSeconds class, which creates a new object instance. In C# the “new” keyword is required, in Javascript it’s optional.

If you’re yielding for the same duration all the time, like a continuous timed event, you can store the WaitForSeconds in a variable and reuse it.

Conversion from int to string probably creates garbage collected data, but it is not a problem, you would hardly need to run it every frame. Typically, ipad apps are slower so I am confused too.

Hey Nantas,
did you find out what is the best way to play multiple sounds reducing the CPU overhead? I’m using PlayOneShot but it looks like it’s pretty heavy…what are the other options?