Having a bunch of sounds in the project == death?

I’m a little bit confused.

We added a bunch of sounds to our iPhone game, and now it dies on the actual iPod Touch hardware. The funny thing is it hasn’t even gotten to the scene where all the sounds are used yet. (two silent scenes before it loaded fine)

So I guess a few things I’m wondering:

  • when are sounds actually loaded? in the scene they’re used? some other logic?

  • what is the limit for sounds? is there a good way to compress sound effects reasonably so that they take up less space / are more efficient?

Thanks in advance for any and all help on this subject. :wink:

Limit of sound:

  1. You can only have 1 compressed sound playing at a time. Any sound started after the first one will disable the first one

  2. Sounds are loaded when the scene is loaded if assigned to a game object as component. You can load them dynamically and clean them again if you don’t want that to happen

  3. uncompressed sounds best are as short as possible. You definitely would not use them for music, they are only used for effects.
    Uncompressed sounds are pretty large and you don’t have the RAM to waste it on large uncompressed sounds.

I think I realized the problem.

I had written an editor/menu script to auto-convert the Music files to Apple format. (cause this is a cross unity / unity iphone project)

The problem is that I assumed that the TextureImporterFormat.Automatic was equivalent to “Apple MP3” - because it appeared to be behaving that way. (and there’s no specific entry for ‘Apple MP3’ as far as I can tell)

When I changed the format back to uncompressed, reimported, then to Apple MP# and reimported manually, everything seemed to work fine.

There doesn’t seem to be a specific TextureImporterFormat setting for the Apple/MP3 format, which confuses me.

glad to hear :slight_smile:

Okay, so my problems aren’t all gone.

Now what happens is that the game randomly shuts down on the iPod Touch hardware.

I’m guessing its running out of memory? Does Unity iPhone have memory leaks?

You can check your apps memory consumption by running it with the activity monitor tool in xCode, and/or check to see if the console in xCode has spit out the line: Application.DidRecieveMemoryWarning.

If activity monitor says you’re under 40mb or so, you shouldn’t be crashing.

This is probably stuff you’ve thought about, but I’m just thinking out loud… sort of. Does it shut down during gameplay or is it during loading, etc? If the app doesn’t respond to the OS for a while, it will be terminated. Also, memory fragmentation can be as bad as memory leaks. If you app is alloc/deallocing ram every frame, and if some of that ram isn’t released and other parts are, then you might end up with swiss cheese ram where there’s no contiguous block big enough to allocate something, even though there’s technically megabytes free. The only way around that is to restructure your memory use, or to use a garbage collector that can compact memory (which has its own performance issues, of course).

I thought the idea was that Unity would manage the memory in such a way that that wouldn’t happen?

How is it even possible to write different garbage collection routines in Unity?

Sorry, I’m speaking in general, not specifically about Unity. I don’t know if Unity uses a GC or if it’s modifiable. I’m assuming that your crash may not be directly related to your previous sound problems.

I think that memory fragmentation can still happen because of things that scripts do. For example, if you instantiate something, add something to a dynamic array, create/modify strings, etc. I don’t know if this is a problem in Unity, and I don’t know if this is your problem. It just seems like you’re stuck and don’t know what to do so I’m trying to help by providing some possibilities.

If you can be sure that there just isn’t a random crash bug in your script code, then memory use would be the first thing I’d suspect to cause random crashing.

Does the crash happen at random times after the app starts up? Like, once it might crash after 10 seconds and another time it might run for an hour before crashing? Or is it generally always after X minutes?

Unity does manage memory for you, and you can’t have any manual control (on the level of what bananaraffle is describing) even if you wanted to. All you can do is not fill up your limited RAM with a ton of assets, and keep your runtime memory allocations to a minimum. Did you check to see how much memory you’re actually using?

Its not after a set amount of time, its different. It seems to crash “around” the loading of a new scene. It could be right at load or a little bit after load. (like a few seconds after)

Initially I was using uncompressed textures and a bunch of sounds. (uncompressed textures was a temporary thing for easily supporting a project that could be both web + iPhone based)

When all that was in (it started with adding the sounds) the game would crash when it hit the intro screen.

I took out a bunch of sounds and changed most of the textures to PVR. Now it crashes after a “random” # of levels. (could be 3, could be 12, etc)

I was watching memory usage with the Object Allocations performance tool in XCode… and when the game crashes, it doesn’t seem to correspond with an increase in memory usage. The memory usage seems to stay pretty constant.

I also tried running leaks, and there appear to be some leaks although I’m not sure if I’m reading that tool properly yet.

Another question I have… how does Unity handle resources?

Is it smart enough to only include assets that are attached to components? Or does it just include everything in the assets folder?

It only builds stuff that’s referenced, with the exception of “Resources” folders: http://unity3d.com/support/documentation/Manual/Loading%20Resources%20at%20Runtime.html

Make sure any of your folders aren’t named Resources (or have the word in them at all, I think).

Ah, thanks Matt. That’s good to know. :slight_smile:

edit:
Just as a test, I took out our sounds folder… that reduces the app size from 41mb to 25mb…

HMM.