Unity Apps run well in desktop, but fails on devices

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:

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: