Search Unity

Alot of txt files

Discussion in 'Editor & General Support' started by imgregduh, Jul 14, 2013.

  1. imgregduh

    imgregduh

    Joined:
    Jan 23, 2013
    Posts:
    5
    Im working on something that may have alot of txt files. I have to parse the information of each txt file and display them. I'm curious if there is a way to code(C#) in a script to attach a txt file automatically without me manually attaching it to one gameobject.
     
  2. tonyd

    tonyd

    Joined:
    Jun 2, 2009
    Posts:
    1,224
    You could load a text file based on the object name.

    Something like:

    Code (csharp):
    1. string fileName = this.name + "_text"; //--so if the name was object1, it would set the fileName to 'object1_text'
    2. TextAsset textAsset = Resources.Load(fileName) as TextAsset;
    3. string theText = textAsset.text;
     
  3. imgregduh

    imgregduh

    Joined:
    Jan 23, 2013
    Posts:
    5
    Does the file have to be in a certain place in my project folder?
     
  4. tonyd

    tonyd

    Joined:
    Jun 2, 2009
    Posts:
    1,224
    It should be in a folder called 'Resources'.
     
  5. imgregduh

    imgregduh

    Joined:
    Jan 23, 2013
    Posts:
    5
    can i have other folders in that folder and still have it called with
    Code (csharp):
    1. Resource.Load
     
  6. tonyd

    tonyd

    Joined:
    Jun 2, 2009
    Posts:
    1,224
    Yes, but you'd have to specify the path.

    Code (csharp):
    1. string folderName = "folder1/";
    And load folderName + fileName.
     
  7. imgregduh

    imgregduh

    Joined:
    Jan 23, 2013
    Posts:
    5
    Alright, thank you very much