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 abc77889921 · May 27, 2017 at 05:52 AM · arraytextfileresources

How to read text file from resources and store into 2d array

Hello guys, I want to read a text file from resources folder and save the content of the file into 2d array. Here is my code where I can read the text file in editor but i can not access the file in build. Is there any solution of this or is there any other way I can do this?

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using System.Text.RegularExpressions;

 public class TestScript : MonoBehaviour
 {
     public Transform player;
     public Transform floor_valid;
     public Transform floor_obstacle;
     public Transform floor_checkpoint;

 public const string sfloor_valid = "0";
 public const string sfloor_obstacle = "1";
 public const string sfloor_checkpoint = "2";
 public const string sstart = "S";

 void Start ()
 {
     string[][] jagged = readFile ("assets/resources/level1.txt");

     // create planes based on matrix
     for (int y = 0; y < jagged.Length; y++) {
         for (int x = 0; x < jagged [0].Length; x++) {
             switch (jagged [y] [x]) {
             case sstart:
                 Instantiate (floor_valid, new Vector3 (x, y, 0), Quaternion.identity);
                 //Instantiate (player, new Vector3 (0, 0.5f, 0), Quaternion.identity);  //old line
                 Instantiate (player, new Vector3 (0, 0f, -0.5f), Quaternion.identity);
                 break;
             case sfloor_valid:
                 Instantiate (floor_valid, new Vector3 (x, y, 0), Quaternion.identity);
                 break;
             case sfloor_obstacle:
                 Instantiate (floor_obstacle, new Vector3 (x, y, 0), Quaternion.identity);
                 break;
             case sfloor_checkpoint:
                 Instantiate (floor_checkpoint, new Vector3 (x, y, 0), Quaternion.identity);
                 break;
             }
         }
     }
 }
 
 // Update is called once per frame
 void Update ()
 {
     
 }

 string[][] readFile (string file)
 {
     string text = System.IO.File.ReadAllText (file);
     string[] lines = Regex.Split (text, "\r\n");
     int rows = lines.Length;

     string[][] levelBase = new string[rows][];
     for (int i = 0; i < lines.Length; i++) {
         string[] stringsOfLine = Regex.Split (lines [i], " ");
         levelBase [i] = stringsOfLine;
     }
     return levelBase;
 }
 }



Comment
Add comment
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

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by kainerya · May 29, 2017 at 06:34 AM

 Object textFile;
 textFile = Resources.Load ("assets/resources/level1");
 TextAsset temp = textFile as TextAsset;
 char[] cArr = temp.text.ToCharArray ();

using System.IO;

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
avatar image
0

Answer by UnityGISTech · May 29, 2017 at 06:35 AM

Try this : using System.IO;

To Read: string text = System.IO.File.ReadAllText("your file location"); or string[] lines = System.IO.File.ReadAllLines("your file location");

To write: using (System.IO.StreamWriter file = new System.IO.StreamWriter("save location")) { foreach (string line in lines) { // If the line doesn't contain the word 'Second', write the line to the file. if (!line.Contains("Second")) { file.WriteLine(line); } } }

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
avatar image
0

Answer by FM-Productions · May 27, 2017 at 03:20 PM

Hi,

I can only make a guess, but if your code is working in the editor and not in the build, it is very likely that the relative path to the resources folder has changed. Maybe you could try to use a function for accessing the resource folder instead of using a hardcoded path? Maybe it works with Resources.Load:

https://docs.unity3d.com/ScriptReference/Resources.Load.html

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

79 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

Related Questions

2D array problem in C# 2 Answers

How can I read data from a text file, putting a large amount of data into structures 2 Answers

Can not load csv file 0 Answers

Is this possible to work as a temporary storage? 2 Answers

Best way to manage stats in a text file 2 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