Search Unity

List from text file

Discussion in 'Scripting' started by Demipimp, Jun 25, 2019.

  1. Demipimp

    Demipimp

    Joined:
    Sep 7, 2016
    Posts:
    15
    I would like to take data from a text file like below and turn each group of names for a list that I can that assign random names from please help or point me in the right direction

    Code (CSharp):
    1. Boys:("Michael","John", "Joseph", "Robert", "Jacob" , "Derrick", "Justin", "Corey", "Malcolm"),
    2. Girls: ("Erica", "Christine", "Alexandria", "Stacey","LaTasha", "Jessica","LaTonya", "Candice")
    3. Dogs:("Max", "Charlie", "Cooper", "Buddy", "Jack",
     
  2. Text Asset and something like https://docs.unity3d.com/ScriptReference/JsonUtility.FromJson.html
    If you can edit the content of the text file. If not, just read up and parse if it's needed. Although it is always more performant if you write an editor tool to do it and store the data inside Unity in ScriptableObject or generated CS file.
     
  3. Demipimp

    Demipimp

    Joined:
    Sep 7, 2016
    Posts:
    15
    I'm not seeing very many (any really)examples on how to make it a list is there any Json utility list function? Or is it just an integersts floats and strings
     
  4. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,334
    string.Split to split the data into an array.
    string.Substring to break off parts of the string you don't care about.

    Turn it into a List by creating the List and using AddRange, or using Linq's ToList function on the array.
     
  5. tcmeric

    tcmeric

    Joined:
    Dec 21, 2016
    Posts:
    190
    Or you can just put them in separate text files. Separate each name by a new line.

    Code (CSharp):
    1. public string loadWordListPath;
    2. loadTermList = new List<string>();
    3.  
    4. var stream = new StreamReader(loadWordListPath);
    5.   while (!stream.EndOfStream)
    6.      loadTermList.Add(stream.ReadLine());
     
    SirhotBay likes this.