Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by aegis80 · Jun 06, 2018 at 09:02 AM · buildfile

Unity - Read text file from external source

I'm trying to read a file in Unity from external source with no luck. To be more clear i can easily read and write on file with StreamWriter and StreamReader while i'm developing the game. The problem is that after build game don't want to read the file anymore. The problem is that in the build i have no more the Resource folder neither the position.txt file. So i tried to put the file under c: calling the function in this way

 > ReadFromFile(@"C:\test\position.txt");

It still works under developement but not after build

This is the code i'm using during development and it works

 string path = Application.dataPath+"/Resources/position.txt";
  ReadFromFile(path);
  public  bool ReadFromFile(string fileName)
 {

     try
     {
         string line;
         StreamReader theReader = new StreamReader(fileName, Encoding.Default);

         using (theReader)
         {

             do
             {
                 line = theReader.ReadLine();

                 if (line != null)
                 {

                 string[] entries = line.Split(',');
                 switch (entries[0])
                 {
                     case "1":
                         Console.WriteLine("Case 1");
                         cube1 = GameObject.Find("Cube1");
                         cube1.GetComponent<Renderer>().material.color = Color.red;
                         StartCoroutine(RotateCoroutine(new Vector3(0, 90, 0), 2.0f, cube1));

                         break;
                     case "2":
                         Console.WriteLine("Case 2");
                         cube2 = GameObject.Find("Cube2");
                         cube2.GetComponent<Renderer>().material.color = Color.red;
                         StartCoroutine(RotateCoroutine(new Vector3(0, 90, 0), 2.0f,cube2));
                         break;
                     case "3":
                         Console.WriteLine("Case 3");
                         cube3 = GameObject.Find("Cube3");
                         cube3.GetComponent<Renderer>().material.color = Color.red;
                         StartCoroutine(RotateCoroutine(new Vector3(0, 90, 0), 2.0f, cube3));
                         break;
                     default:
                         Console.WriteLine("Default case");
                         break;
                 }

                 }
             }
             while (line != null);
             theReader.Close();
             return true;
         }
     }
     catch (Exception e)
     {
         Console.WriteLine("{0}\n", e.Message);
         return false;
     }

}

Can someone give me some hints to make it work?

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Hellium · Jun 06, 2018 at 09:34 AM 0
Share

Why don't you simply copy-paste the Resources folder into your _Data folder after the game is built?

avatar image aegis80 Hellium · Jun 06, 2018 at 09:42 AM 0
Share

I'm a newbie of unity. After build i have only 2 folders alt text Build and TemplateData. Wich folderare you refferring to? And what function i have to call to understand the true path the builded game? I know that there are those but i still not understand how to use them

Debug.Log("Enviroment " + Environment.GetFolderPath(Environment.SpecialFolder.Personal)); Debug.Log("Application Data Path " + Application.dataPath); Debug.Log("Persistent Data Path " + Application.persistentDataPath);

folders.png (3.4 kB)

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by sisus_co · Jun 06, 2018 at 12:08 PM

You can use Application.streamingAssetsPath instead of the Resources directory if you want to read the text file using StreamReader at runtime.

Alternatively you can use Resources.Load to access files from the Resources directories at runtime, like so:

 var textAsset = Resources.Load<TextAsset>("position");
 var text = textAsset.text;´
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

92 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Distribute terrain in zones 3 Answers

Mac OS X Build Execute File? 1 Answer

Manipulating and using .Json files at runtime. 0 Answers

Possible to make build output clean? 1 Answer

Error building OSX only. 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges