Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Android streaming assets path for binary file reading?

Discussion in 'Android' started by jacksonbarnes, May 20, 2019.

  1. jacksonbarnes

    jacksonbarnes

    Joined:
    Sep 13, 2017
    Posts:
    17
    Hello all!

    I have a question that I have had some confusion on because I know Android packages everything up into an .apk file so what is the best way to get access to the streaming assets / resources folder in code to parse a binary file?

    Here is the code below that I use to get access to the .bin file for iOS and the editor but obviously this does not work for android.

    Code (CSharp):
    1. public void InitializeTemporalData()
    2.     {
    3.         string path = Path.Combine(Application.streamingAssetsPath, "SpatialReport_LogCumulativeDeaths.bytes");
    4.  
    5.         using (FileStream fs = File.OpenRead(path))
    6.         {
    7.             using (BinaryReader reader = new BinaryReader(fs))
    8.             {
    9.                 // Read header
    10.                 nodeCount = reader.ReadInt32();
    11.                 timestepCount = reader.ReadInt32();
    12.  
    13.                 // Read node ID list
    14.                 nodeIds = new List<int>(nodeCount);
    15.  
    16.                 for (int i = 0; i < nodeCount; i++)
    17.                 {
    18.                     nodeIds.Add(reader.ReadInt32());
    19.                 }
    20.  
    21.                 // Read an array of floats for each timestep
    22.                 timesteps = new List<List<float>>(timestepCount);
    23.  
    24.                 for (int i = 0; i < timestepCount; i++)
    25.                 {
    26.                     List<float> timestepValues = new List<float>(nodeCount);
    27.                     for (int j = 0; j < nodeCount; j++)
    28.                     {
    29.                         timestepValues.Add(reader.ReadSingle());
    30.                     }
    31.                     timesteps.Add(timestepValues);
    32.                 }
    33.  
    34.                 // Find the min & max value to fit
    35.                 for (int timestepIndex = 0; timestepIndex < timestepCount; timestepIndex++)
    36.                 {
    37.                     List<float> timestepValues = timesteps[timestepIndex];
    38.  
    39.                     maxValueOfTimeSteps = timestepValues.Max<float>();
    40.                     minValueOfTimeSteps = timestepValues.Min<float>();
    41.                 }
    42.             }
    43.         }
    44.     }
    Any Ideas?

    Thanks, J
     
  2. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,653
    That won't work, because the apk is a zip file and streaming assets are inside of it.
    You can use UnityWebRequest to read them.
    The alternative is to read the apk itself using some zip library etc. Actually it is possible to read files from streaming assets using .NET file APIs too, as they are stored without compression, but you still need to get their sizes and offsets from the zip header.
     
  3. jacksonbarnes

    jacksonbarnes

    Joined:
    Sep 13, 2017
    Posts:
    17
    Ah dang got it. Is there any way to store data inside of Unity that I can easily get access to?

    For example: Parse the binary into a scriptable object that I can read the data from without having to parse the data while the app is running?

    There has to be some way to store data in Unity without having to manually typing values in the inspector right?

    Like I want to store an array of 18,000 numbers before I even build to a specific platform.