I've been busy porting Twitchy Thrones to Unity. The game was originally programmed in Objective-C, using XCode and the Cocos2D framework. Cocos2D is a great framework but if you want to publish your game for iPhone, Android and PC you have to use C++ and Cocos2D-X. Setting up Cocos2D-X isn't trivial and using C++, of course, implies losing comforts like automated memory management. As for Objective-C, it's almost exclusively used by Apple and thus can't be used, as far as I know, to port a game to other platforms. This is actually a Good Thing, since it is a monolithic programming language, full of unnecessarily verbose syntax and lacking modern programming language features such as generics, real encapsulation, string operators, etc.
So I turned to Unity and C# to publish Twitchy Thrones on Android and PC. I'm finding Unity amazing, except for a few quirks. If I knew before what I know now I would have started the project in Unity to begin with.
Everything was going well, except... When the Objective-C/Cocos2D version was originally published to the App Store it occupied 17.3 MB on my iPhone. After most features were implemented in the Unity version I installed it on my iPhone expecting a similar file size. Nope. 156 MB! In these days where most games are free or freemium, a big file size can change someone's mind when it comes to downloading your game.
What follows is information I found scattered across the web to solve the problem of Unity's output file size, which I hope will be more useful gathered in one place.
As mentioned in Unity's own article about reducing file size (http://docs.unity3d.com/Manual/ReducingFilesize.html), the editor log provides a summary of the size of each of your assets after you perform a build of your project. This is what I got for mine:
Except for the theme song, all my biggest assets were textures. The problem is, while XCode uses the compressed .png images and even compresses them further with pngcrush (https://developer.apple.com/library/ios/qa/qa1795/_index.html), Unity uses the raw texture data, resulting in enormous file sizes. In Twitchy Thrones' case, a 2D pixel art game, reducing the texture quality wasn't an option since any blurry texture stood out from the rest of the game.The first solution I came across was bypassing Unity's content pipeline and loading the textures from the .png files in real time. If you change an asset's extension to .bytes, Unity treats it as binary data (http://docs.unity3d.com/Manual/class-TextAsset.html) and doesn't unpack it, thus the asset won't bloat your file size. Here's Unity's code sample from loading a texture from a .bytes file:
Textures 65.2 mb 79.6%
Meshes 0.0 kb 0.0%
Animations 113.1 kb 0.1%
Sounds 10.2 mb 12.5%
Shaders 42.9 kb 0.1%
Other Assets 543.3 kb 0.6%
Levels 92.8 kb 0.1%
Scripts 439.5 kb 0.5%
Included DLLs 5.2 mb 6.4%
File headers 99.8 kb 0.1%
Complete size 81.9 mb 100.0%
Used Assets, sorted by uncompressed size:
6.1 mb 7.5% Assets/Sound/Songs/ThemeSong.mp3
2.7 mb 3.3% Assets/Menus/WorldMap/WorldMapBirdFlockMiddle.png
2.7 mb 3.3% Assets/Menus/WorldMap/WorldMapBirdFlockBottom.png
2.7 mb 3.3% Assets/Menus/WorldMap/WorldMapBirdFlockTop.png
2.1 mb 2.5% Assets/Maps/Map16/HelpPanel6.png
2.0 mb 2.4% Assets/Fonts/Resources/Fonts.png
1.3 mb 1.5% Assets/Menus/Main/BannerFinalChallenge.png
1.3 mb 1.5% Assets/Menus/Main/BannerFinal.png
1.0 mb 1.3% Assets/Maps/Map16/Map16Background.png
1.0 mb 1.2% Assets/Menus/WorldMap/WorldMapClouds2.png
1.0 mb 1.2% Assets/Fonts/Resources/MenuFonts.png
946.8 kb 1.1% Assets/Menus/Settings/SettingsFinal.png
946.8 kb 1.1% Assets/Cutscenes/Cutscene2/Cutscene2Banner.png
946.8 kb 1.1% Assets/Cutscenes/Cutscene1/Cutscene1Banner2.png
946.8 kb 1.1% Assets/Cutscenes/Cutscene1/Cutscene1Banner1.png
946.8 kb 1.1% Assets/Cutscenes/Cutscene2/Cutscene2Cup.png
768.3 kb 0.9% Assets/Menus/WorldMap/WorldMapClouds3.png
768.3 kb 0.9% Assets/Menus/WorldMap/WorldMapClouds1.png
710.3 kb 0.8% Assets/Menus/WorldMap/WorldMapBackgroundLand3_2.png
710.3 kb 0.8% Assets/Menus/WorldMap/WorldMapBackgroundLand3_1.png
710.3 kb 0.8% Assets/Menus/WorldMap/WorldMapBackgroundLand3_0.png
//Load texture from disk
TextAsset bindata= Resources.Load("Texture") as TextAsset;
Texture2D tex = new Texture2D(1,1);
A script component was added to each GameObject containing a SpriteRenderer using the offending giant texture and on the Awake() function the texture was loaded using code very similar to the above. A sprite was then created with the texture and this sprite was set to the GameObject's SpriteRenderer. Problem solved, I thought. But when I booted the game on iPhone, a screen that took less than 1 sec to load before now took more than 10 secs! The performance of Texture.LoadImage is simply terrible.
Luckily, someone with the same problem lead to the solution, the WWW api (http://answers.unity3d.com/questions/511268/texture2dloadimage-too-slow-for-use-ios.html
). The game ran perfectly on Unity's editor, but when running on iOS I got an "unsupported URL" error.
Back to Google. Most threads referring the same error warned that the URL is case sensitive and spaces need to be replaced with "%20" as with any URL, but that was not the problem. My problem was, Unity packs the assets in its own format and doesn't keep the original file structure, so the file I was trying to load in the URL didn't exist in iOS. Fortunately Unity provides a way to keep the original assets in their original form in the final build, the "StreamingAssets" folder (http://docs.unity3d.com/ScriptReference/Application-streamingAssetsPath.html
Note that unlike "Resources" folders, which can be placed anywhere in the hierarchy of the "Assets" folder, the "StreamingAssets" folder must be placed in the root of the "Assets" folder. After placing the textures in the new folder, with their extension changed to .bytes for the reason mentioned above, I changed my GameObjects and respective scripts.
Here's one of the final GameObjects (The Sprite should be set to "None" in the SpriteRenderer):
And a link to the SpriteTextureLoader script (Sorry for not putting it here but the formatting got messed up when I tried):http://www.onceabird.com/tt/code.txt
A couple of notes:
- The main downside of this method is that the sprites won't be visible in the Editor when the game's not running. But then again in most cases this method should only be implemented when the game is nearly done, as premature optimization is the root of all evil.
- Although I haven't experimented, this method should work with other asset types, such as textures for models in a 3D game.
- I chose to create a script component for each GameObject where needed, but obviously this code can be used to load all textures in a loading screen if needed.
I only changed the biggest textures so far, but right now Twitchy Thrones occupies 87 MB on my iPhone, which is 55% of the original size.
EDIT: Fixed linked SpriteTextureLoader script to work on Android.