1
\$\begingroup\$

I have successfully read in and written level data using .dat files on unity PC builds, but compiling for android stops me from being able to read in information from the resources folder. I was under the impression that when it's compiled the resources folder was still accessible? I could create a .dat file in the persistent data path but that means hard coding the information only to serialize it, rather than the data originating from the .dat file itself. How can I include this data within the apk without hard coding it?

Here is my code, working on PC:

    Stream file = File.Open("Assets/Resources/levelData.dat", FileMode.Open);

    levelData = (LevelDataFile)bf.Deserialize(file);

    file.Close();

Edit: for context, the level data is very simple as this is just a puzzle game, and each level will just have a few starting positions and obstacles etc, and there will be 100+ levels, hence I'm not doing a scene per level.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Have you considered storing this data in a TextAsset and reading it with Unity's asset referencing methods rather than raw file IO? Or making your level data a ScriptableAsset do you don't need to deserialize it yourself? \$\endgroup\$
    – DMGregory
    May 26, 2019 at 19:32
  • \$\begingroup\$ I haven't considered either of those options - how would I go about using TextAssets? \$\endgroup\$
    – Figwig
    May 27, 2019 at 14:21

1 Answer 1

2
\$\begingroup\$

There are two things you can do here.

  1. You can continue to keep using the Resources folder, which will be available in android. But the thing is, in android, you have to use file:/// or similar prefix to get the file. Also, I’m not sure if you can use the relative path.

    What you can do is, you can access the dat file using the Resources.Load<TextAsset>(path); where the path will be relative, and not absolute.

 

  1. You do not need to worry about the absolute path, if you put the dat file in the streaming assets folder instead. You can pick up the file using StreamReader.
\$\endgroup\$
7
  • \$\begingroup\$ If I were to use the resources folder still, do I use "File:///Assets/Resources/levelData.dat" still? \$\endgroup\$
    – Figwig
    May 27, 2019 at 14:20
  • \$\begingroup\$ You will skip the file:/// when using relative paths. You can do Resources.Load<TextAsset>(“levelsData.dat”); to get the file. \$\endgroup\$
    – Shraa1
    May 27, 2019 at 14:24
  • \$\begingroup\$ In my case, I'm using a custom class, not a text asset, but putting that class into the type specifier results in a message saying I can't as it doesn't inherit from a Unity Object. Do I have to change it's inheritance, or can I import as a text asset and then deserialize it? \$\endgroup\$
    – Figwig
    May 27, 2019 at 15:18
  • \$\begingroup\$ Yes, you can just simply deserialise it from normal text. You do not need to change the inheritance. Even if your class is not inheriting from any class, you’ll be able to deserialise it \$\endgroup\$
    – Shraa1
    May 27, 2019 at 15:20
  • \$\begingroup\$ Thanks. Do you know how I would go about this? The deserializer only takes in streams, do I just have to convert the string into a stream? \$\endgroup\$
    – Figwig
    May 27, 2019 at 15:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .