Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question How to Import csv using C# from a local drive file path?

Discussion in 'Scripting' started by Drachensunde, Sep 27, 2022.

  1. Drachensunde

    Drachensunde

    Joined:
    Sep 27, 2022
    Posts:
    8
    Hello,

    I'm sorry if this has been answered before, I have been searching everywhere trying to find an answer. I am using the latest version of unity and visual studio. I want to import a csv from a local drive file path and then every 30 minutes or so refresh the file. The reason I need to use a C# script is because the data is constantly being updated. I thought I could use System.IO.File but I'm not able to find any examples of code for it.

    Could you please help with an example or point me to another forum.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,517
    System.IO.File can read local files. Test that first. It is going to require the correct path and more importantly the correct operating system security settings.

    When it fails, check the device log.

    Once you have the file open, there are some C# CSV libraries you can use but the format is simple enough you might find it easier to just write the parsing logic yourself, depending on how reliable the formatting is.

    Remember that CSV is an ambiguously-defined format and everybody does it a little bit differently. :)
     
  3. nTu4Ka

    nTu4Ka

    Joined:
    Jan 27, 2014
    Posts:
    69
    Here is an example from microsoft.com
    https://learn.microsoft.com/en-us/dotnet/api/system.io.file?view=net-7.0

    Code (CSharp):
    1. using System;
    2. using System.IO;
    3.  
    4. using (StreamReader sr = File.OpenText(path))
    5.         {
    6.             string s;
    7.             while ((s = sr.ReadLine()) != null)
    8.             {
    9.                 Console.WriteLine(s);
    10.             }
    11.         }
     
  4. AnimalMan

    AnimalMan

    Joined:
    Apr 1, 2018
    Posts:
    1,164
    Close the reader when finished *
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,517
    He did. See the
    using()
    statement. :)
     
    nTu4Ka and AnimalMan like this.
  6. AnimalMan

    AnimalMan

    Joined:
    Apr 1, 2018
    Posts:
    1,164
    Just incase he does the full variable Stream reader Shabam
     
  7. Drachensunde

    Drachensunde

    Joined:
    Sep 27, 2022
    Posts:
    8
    Thank you everyone for the help.
     
  8. Drachensunde

    Drachensunde

    Joined:
    Sep 27, 2022
    Posts:
    8
    Thank you for this answer it worked. However, In Unity Console.Writeline(s); isn't showing the data in my console. Which isn't a huge deal I changed it to Debug.Log(s); and can see the data is there so I should be able to work with it now.
     
    nTu4Ka likes this.
  9. Drachensunde

    Drachensunde

    Joined:
    Sep 27, 2022
    Posts:
    8
    I have another feature I'm trying to implement and I'm hoping someone can point me in the right direction. Now that I can read in the data. I want to display each of the row data on its own draggable object. (I'm thinking maybe a UI Image might be the best but I'm not sure). The only tutorials or forums I've found are turning it into a text asset or a ScriptableObject but the tutorials don't show how to use them after they are created.

    Also this is something that needs done at runtime and that can be updated with a refresh button. So I'm not sure if a text asset or scriptable object would be the right direction I need.

    I know how to make something draggable, that was pretty easy to find but I can't find anywhere someone displaying their csv data as text on an object, image, or anything.

    Note: the user will not be able to edit the draggable object, if the information is updated it will be through the csv. Which is the reason I am going to go for a refresh button.

    Note: The code below is what I'm experimenting with to read in the data. Currently I just have it Debug.Log the first column without the header.

    Code (CSharp):
    1. {
    2.         string csvText = System.IO.File.ReadAllText(csvPath);
    3.         csvText = csvText.Replace("\"", "");
    4.         string[] csvRows = csvText.Split('\n');
    5.        
    6.         string[] headers = csvRows[0].Split(',');
    7.  
    8.         var rsNbr  = new List<string>();
    9.  
    10.  
    11.         /*csvRows = csvRows.Skip(1).ToArray();*/
    12.         for (int i = 1; i < csvRows.Length; i++)
    13.         {
    14.             string[] rowData = csvRows[i].Split(',');
    15.             rsNbr.Add(rowData[0]);
    16.  
    17.         }
    18.         for (int i = 0; i < rsNbr.Count; i++)
    19.         {
    20.             Debug.Log(rsNbr[i]);
    21.         }
    22.  
    23.     }
     
  10. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,517
    Enclosed is an example of dynamically updating a collection of buttons at runtime.

    It uses the assumption that you author a "master" or "exemplar" button in your scene that the code proceeds to clone and customize based on the live data, in this case sprites and their names, but the idea extends to any data.

    Also, always best to start fresh posts for fresh topics, as per forum guidelines. :)
     

    Attached Files: