Search Unity

How to Load Multiple files into an Array

Discussion in 'Scripting' started by LandonF, Feb 27, 2017.

  1. LandonF

    LandonF

    Joined:
    Mar 1, 2016
    Posts:
    34
    I want to take the files that I've already saved in unity's persistent data path, and then load them into a bar graph I have made. Here's an example of what I mean...

    file 1: score = 150, Team name = Fighters
    file 2: score = 70, Team name = Defenders
    I want these to set the size of the Team name array to equal 2 (because of the amount of files), and the team name array should contain "Fighters" and "Defenders". Then the score array would be a size of 2 and then contain 150, and 70.
     
  2. AverageProg

    AverageProg

    Joined:
    Jun 25, 2015
    Posts:
    38
    You don't really need an array if you know you always have 2 teams, just have them separate fields and be glad this problem was so easy to solve.
     
  3. LandonF

    LandonF

    Joined:
    Mar 1, 2016
    Posts:
    34
    I plan to have a lot of files (like 20-100). I need to load all of these files into the values of my bar graph. I have no idea how to load multiple files at a time into unity.
     
    Last edited: Feb 27, 2017
  4. vothka

    vothka

    Joined:
    Mar 27, 2015
    Posts:
    59
    Hello Sir,

    just how i would approach this:
    (and sorry if some capital letters are mixed up, autocorrection is a mess)

    as you have given few Information about how you store / save your files and how you would like to read them i start at 0

    First, lets create a model for our files

    Code (CSharp):
    1. [System.Serializable]
    2.     public class FileModel
    3.     {
    4.         public int Score { get; set; }
    5.         public string TeamName { get; set; }
    6.     }
    Note the Serializable Attribute - we will Need it later for the binaryformatter

    now lets assume we are in some Method (for sake of discussion in the start())

    Code (CSharp):
    1. void Start () {
    2.  
    3.         var path = Path.Combine(Application.streamingAssetsPath, @"DataFiles\");
    4.         BinaryFormatter bf = new BinaryFormatter();
    5.         if(!Directory.Exists(path))
    6.         {
    7.             Directory.CreateDirectory(path);
    8.         }
    9. }
    10.  
    we Setup a path where we would like to store our files (note here that you have to use the streamingassetpath as otherwise after a build your path would not work)
    and a binaryformatter object (for later use ((using System.Runtime.Serialization.Formatters.Binary;)

    now lets write a method that stores your data

    Code (CSharp):
    1.  public void SaveToDisk(FileModel model, string path)
    2.     {
    3.         BinaryFormatter bf = new BinaryFormatter();
    4.         using (FileStream fs = File.OpenWrite(path + "\\" + model.TeamName + ".dat"))
    5.         {
    6.             bf.Serialize(fs, model);
    7.         }
    8.     }
    As you can see the method would get a model for your data and a path to store it - the filestream would write what it gets from the binaryformatter in given file / path

    so let's go back to our start method and try and read them again -> to store them in an Array (because that's what this is about right?)

    Code (CSharp):
    1. .
    2. .//add in Start()
    3. .
    4. var model = new FileModel() { Score = 100, TeamName = "AnyName" };
    5.         var model2 = new FileModel() { Score = 210, TeamName = "SomeName" };
    6.         SaveToDisk(model, path);
    7.         SaveToDisk(model2, path);
    8.  
    9. //we simply created two test objects and save them
    10.  
    11.  
    12. //let's get the Directory (using System.IO)
    13. DirectoryInfo di = new DirectoryInfo(path);
    14.  
    15. //Now get all files from this Directory - and let's filter them by their fileextension
    16. //I used System.Linq here - of Course you can do in other ways
    17. var files = di.GetFiles().Where(o => o.Name.EndsWith(".dat")).ToArray();
    18.  
    19.  
    20. //now we create our Array of fileModels - the size of it is simply the amount of files we have in our Directory which match certain criteria (fileextension here)
    21. var AllFiles = new FileModel[files.Length];
    22.  
    23. //For every file we now use the binaryformatter to get back our data and store it in our Array as a filemodel so we can easily work with the data again
    24. for (int i = 0; i < files.Length; i++)
    25.         {
    26.             using (FileStream fs = File.OpenRead(files[i].FullName))
    27.             {
    28.                 Debug.Log(files[i].FullName);
    29.                 AllFiles[i] = (FileModel)bf.Deserialize(fs);
    30.             }  
    31.         }
    32.  
    33. //Just a testmessage to see if it worked
    34. Debug.Log(AllFiles[0].Score);
    35. Debug.Log(AllFiles[1].Score);
    36.  
    37.  
    38.  

    hope this helps and is what you asked for


    greetings
     
  5. LandonF

    LandonF

    Joined:
    Mar 1, 2016
    Posts:
    34


    This is exactly what I needed! Thank you very much!
     
  6. jpgordon00

    jpgordon00

    Joined:
    Jan 9, 2021
    Posts:
    25
    Another solution is to use my group downloader or a download handler to download files. Then use
    Code (CSharp):
    1. string data = Files.ReadAllText(path);
    for each file.