Search Unity

getFiles() doesnt work on Android

Discussion in 'Scripting' started by deLord, Jan 19, 2016.

  1. deLord

    deLord

    Joined:
    Oct 11, 2014
    Posts:
    306
    I wanna query a certain folder for level files (text) that have been inserted during the course of the game (persistent throughout game starts). There can be an infinite amount of those level files.

    In my first project it worked like this:

    Code (CSharp):
    1. if(Application.platform == RuntimePlatform.Android){
    2.     dataPath = Application.dataPath;
    3. }
    4. FileInfo loadFile = new FileInfo(dataPath+"/save/"+slot);
    5. StreamReader reader = loadFile.OpenText();
    6.  
    Now, querying a folder seems to be a problem somehow. No matter which manifestation of dataPath I use
    Code (CSharp):
    1. dataPath = Application.persistentDataPath;
    2. dataPath = "jar:file://" + Application.dataPath + "!/assets/";
    3. dataPath = Application.dataPath;
    when running the code, the game stop executing the scripts, does NOT throw a UnityException and obviously my game breaks.
    I found a lot of threads about this topic but nothing I have read behaved how I expect it. So the question is:

    How do I query a folder on Android for files if the second line breaks the code:
    Code (CSharp):
    1. DirectoryInfo levelsDir = new DirectoryInfo(dataPath+"/levels/");
    2. FileInfo[] levelsDirArray = levelsDir.GetFiles("*.txt");
    For over a week I could not find an answer, so thanks for your help
     
  2. larku

    larku

    Joined:
    Mar 14, 2013
    Posts:
    1,422
  3. deLord

    deLord

    Joined:
    Oct 11, 2014
    Posts:
    306
    Depends on what you mean with "shipped with the game"

    They are not there when you download the game. But they can be unlocked ingame. They are generated randomly with names like 10000, 10001, .... 20000, 20001, .... 50000, 50001.
     
  4. larku

    larku

    Joined:
    Mar 14, 2013
    Posts:
    1,422
    By shipped with game I mean are the text files bundled with the build in Unity (ie will be stored as an asset)

    Sounds like the links above are what you need by your description.
     
  5. deLord

    deLord

    Joined:
    Oct 11, 2014
    Posts:
    306
    actually I think they provide the solution for when you have built-in files. If I got that wrong, please explain to me once more.

    They are basically like regular save files, except I don't know their names, so that's why I need to query a whole folder. I don't understand why this is a problem or which function I must use. I can not make any presets of them.
     
  6. larku

    larku

    Joined:
    Mar 14, 2013
    Posts:
    1,422
    But your description is of built-in-files no?

    Are you downloading these files?

    How do these files get in this "whole folder"

    Mobile and Web platforms often do not have such features available (they are more sandboxed than that) that's why these asset API's exist.
     
  7. deLord

    deLord

    Joined:
    Oct 11, 2014
    Posts:
    306
    So how do you create a savegame if creating text files isn't possible?

    They are generated randomly at runtime. They are plain text files and it completely works when using the Editor or with the savegame file (there is only 1).
     
  8. larku

    larku

    Joined:
    Mar 14, 2013
    Posts:
    1,422
    I'd suggest using PlayerPrefs if that suffices (again, a storage mechanism that works on all platforms).

    You can save to the filesystem on some platforms (WebGL - no chance) and other have various restrictions etc. Unless PlayerPrefs will not work, I'd use that for game saves.

    If they are generated randomly at runtime, could you not just keep the 'seed' for the random number generator as a player pref and regenerate from that as needed?
     
  9. deLord

    deLord

    Joined:
    Oct 11, 2014
    Posts:
    306
    PlayerPrefs seems definately the wrong mechanism for that. It is not a player preference but a file with a lot of information.

    Even IF PlayerPrefs could store a seed, what would be the key for theoretically 50000 files?

    I dont really understand why this simple things turns out to be that complex and why nobody has an answer to that
     
  10. larku

    larku

    Joined:
    Mar 14, 2013
    Posts:
    1,422
    It's not complex at all.

    How about explain in detail what it is your're doing and we'll let you know what is the right or wrong mechanism.

    If something is generated at runtime then the seed is a single integer no? (the seed to the random number generator) so any randomly generated level should take at most 1 int to store... no?

    Where does 50000 files come in to it? You'll need to explain what you're wanting to do - you're initial description made no indication of 50000 files.