Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 MickM · Aug 25, 2012 at 09:37 AM · arrayiotxt

Read text file to get 2D array

Gday all (again)

Another Q relating to the maze game I am working on:

I have made a maze game that creates levels from scripts rather than constructing it by manually placing the walls down. Initially I just have a Terrain and an empty Game Object called Startup with scripts attached. The maze load script loads the maze from an array and places the player at start location, exit trigger at the exit and walls etc down according to the characters in the array.

Currently I specify the array inside the maze load script (testing and proof of concept) and it works great.

What I WANT to do is read a text file to set up the array... This will allow people to make their own and play them.

What I am looking to do is read a text file and save each line as an array of single characters. I then want to put all the individual line arrays into another array (creating a 2D array)

Code as follows:

 #pragma strict
 
 var exitPrefab : GameObject;
 private var thePlayer : GameObject;
 var thePlayerPrefab : GameObject;
 var wallPrefab : GameObject;
 private var escMenu = false;
 
 var rowOne =   ["8","E","8","8","8","8","8","8","8","8","8","8","8"];
 var rowTwo =   ["8"," ","8"," "," "," "," "," "," ","8","Z"," ","8"];
 var rowThree = ["8"," ","8","8","8","8","8"," ","8","8","8"," ","8"];
 var rowFour =  ["8"," ","8"," "," "," "," "," ","8"," ","8"," ","8"];
 var rowFive =  ["8"," "," "," ","8"," ","8"," "," "," "," "," ","8"];
 var rowSix =   ["8","8","8","8","8","8","8","8","8","8","8","8","8"];
 
 var mazeArray = [rowSix, rowFive, rowFour, rowThree, rowTwo, rowOne];
 
 private var iylength = mazeArray[0].Length; //13
 private var ixlength = mazeArray.Length; //6
 private var thePlayerLoc;
 
 function Start () {
     if (exitPrefab == null){ exitPrefab = Resources.Load("PrefabExit");}  
     if (wallPrefab == null){ wallPrefab = Resources.Load("WallPrefab");}  
     if (thePlayerPrefab == null){ thePlayerPrefab = Resources.Load("PlayerPrefab");}  
     
     SetupMaze();
 }
 
 function Update () {
 if (Input.GetKeyDown(KeyCode.Escape)) {
         if (escMenu == true) { 
             escMenu = false;
             }
         else { 
         escMenu=true;
         }
     }
     
     if (escMenu){Screen.lockCursor = false;}
     else {Screen.lockCursor = true;}
 }
 
 
 function SetupMaze(){
     for (var ix = 0; ix < 13; ix++){
         for (var iy = 0; iy < 6; iy++){
             if (mazeArray[iy][ix] == "8"){
                 Instantiate(wallPrefab, Vector3 (ix+50.5, 0.5, iy+50.5),  Quaternion.identity);        
             }
             
             if (mazeArray[iy][ix] == "Z"){
                 thePlayer = Instantiate(thePlayerPrefab, Vector3 (ix+50.5, 1.5, iy+50.5),  Quaternion.identity);
                 thePlayerLoc = Vector3 (ix+50.5, 1, iy+50.5);
             }
             
             if (mazeArray[iy][ix] == "E"){    
                 Instantiate(exitPrefab, Vector3 (ix+50.5, 0.5, iy+50.5),  Quaternion.identity);        
             }
         }
     }
     yield;
     thePlayer.transform.position = thePlayerLoc;
 }
 
 
 
 function OnGUI () {
     if (escMenu == true) {
         if (GUI.Button (Rect (Screen.width/2-50,Screen.height/2,100,20), GUIContent ("Quit"))){
             Application.LoadLevel (0);
             }
     }
 }


Questions:

  1. How do I read a text file to individual arrays (I have searched but not been able to find anything that suits what I need)

  2. Is there a way to test in the unity editor that will work when built? (ie. I am assuming I will have to add another folder once it is built as unity combines everything when it builds? But wont this mean the code that works when testing in unity will have to be changed to work when it is built?)

  3. Is there a better way to compare the arrays? (the rowOne - rowSix is just for testing, When I can get it to read from text files I will make that part dynamic)

Assistance is GREATLY appreciated!!!

Comment
Add comment · Show 1
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 SvenEV · Aug 25, 2012 at 10:15 AM 0
Share

Here's a Linq approach, but it's C# (as I currently don't know how to express this in UnityJS), so not an answer to your question. It reads a text file and creates a two dimensional array out of it.

 // Read the text file
 var text = File.ReadAllText(@"C:\Path\To\Your\File.txt");
 
 // Split the text into individual rows
 var rows = text.Split(new[] { "\r\n", "\n" }, StringSplitOptions.None);
 
 // Do some fancy Linq stuff
 string[][] maze = rows.Select(r => r.ToCharArray().Select(o => o.ToString()).ToArray<string>()).ToArray<string[]>();

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by drudiverse · Aug 26, 2015 at 04:38 AM

Just looking into this topic.

perhaps writing JSON format would make sense in this instance.

The problem i have is that large strings cause an error with unity... gui string thingy not worky memory overload... message..

So either i have to read array entries line by line to strings, or i have to find another format that can read directly as non string data.

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

9 People are following this question.

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

Related Questions

DirectoryNotFoundException when running a build 1 Answer

Read txt file from Android Downloads file 1 Answer

Writing to txt file using textfield problem 2 Answers

How to read text file to array for my generateLetter script? 0 Answers

How do I make a public array of arrays appear in the inspector? 1 Answer


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