Search Unity

  1. Unity 2018.3 is now released.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. We've updated our Terms of Service. Please read our blog post from Unity CTO and Co-Founder Joachim Ante here
    Dismiss Notice
  4. Want to provide direct feedback to the Unity team? Join the Unity Advisory Panel.
    Dismiss Notice
  5. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice

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

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

  1. flomotion


    Jul 21, 2014
    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);

    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";
    BinaryFormatter bf = new BinaryFormatter ();
    FileStream file = File.Open (path,FileMode.Open);
    level= (Level)bf.Deserialize(file);

    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


    Feb 21, 2018
    it wont be on mobile
  3. angrypenguin


    Dec 29, 2011
    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


    Aug 3, 2015
    Unfortunately the OP was almost 4 yrs ago.:)