Unity Apps run well in desktop, but fails on devices

19 Nov

I recently worked with several development groups and found that some junior programmers frequently complain as follows:

The app works well on desktop (unity editor’s game and scene view), but it crashes or fails to function when the app is deployed on Android or iPhone devices 


As a case study,  one of our team member told me that the texture does not show up on Android Devices. It ultimately turns out that he is using an absolute path:

var sprite = Resources.Load(C:/abc/project/asssets/sprites/def);

This, of course, will NOT work, as when the app is deployed on Android device, the file system is different from the Windows system, so it fails to fetch the image to memory.

Another example, is to download some file from the web and save it to local storage. In order to avoid repetitive downloading the same resources (such as html, css, JavaScript or image file), below code is used:

var cacheFile = DateTime.Now.ToString("yyyy-MM-dd-HH") + ".html";
if (System.IO.File.Exists(cacheFile ))
      doc = new HtmlDocument();
      doc.Load(cacheFile );
      Debug.Log(string.Format("Loading data from cache file: {0}", cacheFile ));
      string remoteUrl = "SomeUrl";
      HtmlWeb web = new HtmlWeb();
      doc = web.Load(remoteUrl);
      Debug.Log(string.Format("Loading data from web : {0}", remoteUrl));
      doc.Save(cacheFile ); 

Note the bolded code above, it is assumed that the file is loaded/saved to and from the current folder (i.e. the root Assets folder of the Unity project)! This works perfect, if you run the app in Unity Editor’s game view, or even run it via Unity Remote!

But when you deploy the app on Android device, you will fail! Simple, there is no such folder called “current” folder on the mobile devices, and even if there exists one such folder, you might most likely found that the folder is not writable!

So the solution is simple: use the right directory to read and write your data to and from. Here is the snippet of the solution: 

var cacheFile = DateTime.Now.ToString("yyyy-MM-dd-HH") + ".html";
//Application.persistentDataPath is the key!
string filePath = Application.persistentDataPath + "/" + cacheFile;  
if (System.IO.File.Exists(filePath))
    // .... Save or read

Now use the right folder/directory (neither the hard coded absolute path, nor the take granted path of relative directory) to code in your work, you will never complain as above any longer!

Happy coding for Apps & Games!

Leave a comment

Posted by on November 19, 2014 in Programming, Unity 3d


Tags: , , , , , , , , ,

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: