Search Unity

  1. Unity 2020.1 has been released.
    Dismiss Notice
  2. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Porting to Android from PC game and file structure...

Discussion in 'Android' started by astracat111, Oct 26, 2017.

  1. astracat111


    Sep 21, 2016
    I'm not entirely sure how android file structure works....I thought you just pop files in StreamingAssets and then use Application.streamingAssetsPath...

    So in my project I have structured it in where for every scene file there's a corresponding xml .map file.

    I'm storing these in a folder structure like this:

    - > Data
    -- > Maps
    -- > Scenes

    The game checks if the corresponding .map (xml files) exist compared to the scene files. I'm storing my scene files right out in the open in streamingassets as well in the Scenes folder.

    So now I get an error when I try to use Directory.GetFiles. Here's what's coming up:

    Code (CSharp):
    1. 10-26 18:40:01.148: I/Unity(31855): DirectoryNotFoundException: Directory 'jar:file:/mnt/asec/com.AstraCat.TestGame-1/base.apk!/assets/Data/Scenes' not found.
    2. 10-26 18:40:01.148: I/Unity(31855):   at System.IO.Directory.GetFileSystemEntries (System.String path, System.String searchPattern, FileAttributes mask, FileAttributes attrs) [0x00000] in <filename unknown>:0
    3. 10-26 18:40:01.148: I/Unity(31855):   at System.IO.Directory.GetFiles (System.String path, System.String searchPattern) [0x00000] in <filename unknown>:0
    4. 10-26 18:40:01.148: I/Unity(31855):   at System.IO.Directory.GetFiles (System.String path) [0x00000] in <filename unknown>:0
    Again what my project does is looks in the Scenes folder and checks that all corresponding .map xml files exist (w same name as scene files) in the Maps folder, but then it can't find the directories. I AM using streaming assets.

    Here's my code for that method:

    Code (CSharp):
    1.     private void MG_Data_CheckIfCorrespondingMapFilesExist() {
    2.         //Check if corresponding .map files exist to .unity (scene) files:
    3.         string[] SceneFilesInFolder = Directory.GetFiles (Application.streamingAssetsPath + "/Data/Scenes/");
    4.         foreach (string scene in SceneFilesInFolder) {
    5.             if (Path.GetExtension (scene) == ".unity") {
    6.                 //This is the substring equasion that comes up with the .map file and path to check
    7.                 //again the .unity file and path:
    8.                 string MapFileAndPath =
    9.                     Application.streamingAssetsPath +
    10.                     "/Data/Maps/" +
    11.                     scene.Substring (scene.LastIndexOf("/")+1,
    12.                         (scene.LastIndexOf ("."))-(scene.LastIndexOf("/")+1)) +
    13.                     ".map";
    15.                 if (!File.Exists (MapFileAndPath)) {
    17.                     //if File doesn't exist in path....
    18.                     string errorone = "ERROR: The following .map file does not exist in the Data/Maps/ directory:\n"
    19.                         + MapFileAndPath;
    20.                     string errortwo = "All .unity (scene) files must also have corresponding .map files generated within the " +
    21.                         "/Data/Maps/ directory by using the editor. Please use 'Generate .map files' to generate all .map files for every " +
    22.                         "scene, or 'generate .map file for scene' within the Scene's 'Scene' object.";
    23.                     MG_Debug_ThrowError (errorone, errortwo);
    24.                     return;
    25.                 }
    26.             }
    27.         }
    28.     }
  2. astracat111


    Sep 21, 2016
    Okay, so I've basically found what I need to do.

    I believe what I'm needing to do is to use the persistentDataPath instead. The problem now is that I need to figure out how to take what's in the StreamingAssets directory and copy it to that persistentDataPath.

    This is tough because I have to use the WWW class because you can't use file IO to get inside the .jar file that's created within the apk that contains the StreamingAssets folder... The url that was given back was:

    Code (csharp):
    1. file:///mnt/asec/com.AstraCat.TestGame-2/base.apk!/assets/Data/
    Well, there's the folder, now how do I copy and paste (using WWW) everything in that directory and sub-directories?
  3. Zenix


    Nov 9, 2009
    I use something like this for moving files out of streaming assets.

    Code (csharp):
    2.                 var www = new WWW(fullpath);
    3.                 while (www.isDone == false)
    4.                     yield return null;
    6.                 if (string.IsNullOrEmpty(www.error) == false)
    7.                 {
    8.                     Log.Error("Error: " + www.error);
    9.                     continue;
    10.                 }
    12.                 var directoryName = Path.GetDirectoryName(destination);
    13.                 Directory.CreateDirectory(directoryName);
    14.                 File.WriteAllBytes(destination, www.bytes);