Search Unity

load binery data from .dat file (or if necessary .txt) on mobile phone

Discussion in 'Scripting' started by flomotion, Jul 21, 2014.

  1. flomotion

    flomotion

    Joined:
    Jul 21, 2014
    Posts:
    1
    Hi everyone!

    This is my first time posting a question (because so far i could always find my questions already answered), so i will try my best to formulate the question as clear as possible:

    I have an object Level containing some data i need to load the level and i have it saved (on the phone) like this:

    string path=Application.persistentDataPath + "/"+"dataName"+".dat";
    if (!(File.Exists (path)) ) {
    BinaryFormatter bf = new BinaryFormatter ();
    FileStream file=File.Create (path);
    bf.Serialize (file, level);
    file.Close();
    break;
    }

    now I can copy these .dat files to any folder (in this case assets/Levels) on my computer and load them with this code:

    Level level=new Level();


    string path=Application.dataPath + "/Levels" + "/dataName" + ".dat";
    if(File.Exists(path)){
    BinaryFormatter bf = new BinaryFormatter ();
    FileStream file = File.Open (path,FileMode.Open);
    level= (Level)bf.Deserialize(file);
    file.Close();
    }


    this works flawlessly on the computer but it will not work on the mobile phone. i guess this is because the folders i created do not exist anymore after creation of the app and i will have to use resources.load to get my data.

    I put my level data in the resources folder and tried some things like:

    BinaryFormatter bf = new BinaryFormatter ();
    TextAsset asset = Resources.Load("dataName") as TextAsset;
    Stream s = new MemoryStream(asset.bytes);
    level= (Level)bf.Deserialize(s);


    this doesnt work, probably because my data is stored as .dat. My question is now if there is any way to import my binary data from those .dat files and if not, how can i save my binary data to a txt file.

    Or any other solution how to save an object to a binary file and reload it afterwords (not from persistantDataPath but from a folder within Assets) ???

    Thank you guys a lot for helping me!

    kind regards, flo
     
  2. sinanasiri

    sinanasiri

    Joined:
    Feb 21, 2018
    Posts:
    1
    it wont be on mobile
     
  3. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,620
    You're right that the specific example there doesn't work because Unity doesn't recognise the .dat file as a TextAsset, but there's a more fundamental issue at play.

    The Resources folder isn't actually a folder of files when it's in your game. Unity puts all of the stuff in Resources in an AssetBundle which it then includes in your game. So you can't access those things as files because a) that's not how they're stored in a build and b) Unity will convert them to the appropriate internal asset format at build time. The folder itself is just a hacky way Unity lets us include stuff in our build (see their documentation for why I call it "hacky").

    You could write code to work around that and load files that happen to be in the Resources folder, but that will only work in the Editor for the reasons above.

    If you specifically need file access on the phone then your first example is the correct approach - persistentDataPath points to a place where your app has permission to read and write files.

    However, if what you're really after is a way to save levels as files that will get deployed to your device with games then that's a little different. In that case my suggestion would be to look into AssetBundles and TextAssets as you're doing. However, you don't load those as files. From memory, loading a TextAsset basically gives you a string of the loaded contents, and you can't re-write that back to the asset. You need to give it a file extension that Unity will recognise as an asset and handle data encoding correctly, but a TextAsset does not strictly have to contain textual data - it's just data as text.
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Unfortunately the OP was almost 4 yrs ago.:)
     
  5. nhiepphong3693

    nhiepphong3693

    Joined:
    Sep 7, 2020
    Posts:
    1
    For those who have similar at this time, my simple solution is that when you save the file just put it in text format extension (.txt or .json) even though the correct extension is actually different (.dat, .gz, .bin, .zip ...). By doing this, you can still use Resource.Load<TextAsset>(path).bytes to get byte array and do deserialize like normal.
    Or similarly you can save in correct extension but when loading file, try to change extension to .txt or .json (I have not tested this one but it should work as the fore-mentioned)
     
    chsnkoff and DavidBaiao like this.
  6. DavidBaiao

    DavidBaiao

    Joined:
    Oct 14, 2021
    Posts:
    1
    THANK YOU