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 YaVaho155 · Feb 29, 2012 at 12:30 AM · texttilematrixtxtmultidimensional array

Reading a Matrix from a .txt for level buildup

Okay, so I'm fairly new to Unity, js and C#, but I have to create a game for a school project. I'm working on a 2D tiled level, and I had the idea of saving a set of digits in a txt to use for generating the level. For example:

1,0,2,1
5,2,0,1
4,2,3,1

Every number would be a different type of tile, with 0 being no tile (a gap), 1 being grass, 2 being water etc.
On reading, a separate game object would instantiate a tile on the correct position at start (I guess this will be done with loops, but how would I count the amount of rows and columns then?).
The problem: I don't know how exactly to make it read the file (read some stuff about System.IO but it is rather confusing), plus I'd like to save this grid in a Matrix (var text[x][y], right?) for future reference in the game (like checking where a character can go and where not). How do I do this? Thanks in advance.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by syclamoth · Feb 29, 2012 at 12:53 AM

In this case, there's not too much point messing around with System.IO. I recommend learning C# over JS, because it's something that will be, in general, more useful to you (as well as having better .net compatibility).

I recommend storing the information as serialised objects internally in Unity. Unless there is some pressing need to be able to modify the levels outside, there's not need to trouble yourself.

Now, because Unity does not support serializing either jagged arrays or multidimensional arrays, you'll need to create your own special classes for this. Start with an enum for tile types:

 [System.Serializable]
 public enum TileType
 {
     Rocks,
     Trees,
     Water,
     Sand
 }

Then, create the data-storage types:

 [System.Serializable]
 public class TileArray
 {
     public Tile[] contents;
     public TileArray(int size)
     {
         contents = new Tile[size];
     }
 }

 [System.Serializable]
 public class TileArrayArray
 {
     public TileArray[] xAxis;

     public Tile this[int x, int y]
     {
         get{
             return xAxis[x].contents[y];
         }
         set {
             xAxis[x].contents[y] = value;
         }
     }

     public TileArrayArray(int sizeX, int sizeY)
     {
         xAxis = new TileArray[sizeX];
         for(int i = 0; i < sizeX; ++i)
         {
             xAxis[i] = new TileArray(sizeY);
         }
     }
 }

Now, in any of your classes, you can use

 public TileArrayArray tiles = new TileArrayArray(10, 10);

to create a 10x10 grid of 'Tile' objects, that can be accessed by using

 Tile curPoint = tiles[xPos, yPos];

There you go! Serializable multidimensional arrays.

Comment
Add comment · Show 5 · 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 YaVaho155 · Mar 02, 2012 at 03:13 PM 0
Share

Copied them in c# scripts, but in the TileArrayArray the line public TileArrayArray(sizeX, sizeY) gets underlined, and I get three errors: two times CS1041: Identifier expected (first time withTileArrayArray.cs(19,27) and once with (19,34), and once CS0100:Theparameter name 'NeedSomeGeneratorHere' is a duplicate. Any idea what went wrong? I had to make them in three separate cs files, right? Anyway, thanks so far for helping! :)

avatar image syclamoth · Mar 04, 2012 at 05:36 AM 0
Share

Sorry, made a few mistakes there. They're fixed now (forgot to add the types for the constructor). No, you can make them all in one file, just make sure you don't put them inside a '$$anonymous$$onoBehaviour' class. I have no idea what that 'NeedSomeGeneratorHere' thing is. Remember that I haven't given you any usage examples here- all you really need to know is the indexer syntax and constructor.

avatar image YaVaho155 · Mar 04, 2012 at 10:18 AM 0
Share

All right, thank you :D

avatar image Kadaiyen · Apr 10, 2012 at 10:34 AM 0
Share

Sorry to revive this after a month, but I'm looking to do something quite similar to the OP - but what is "Tile" and where is it defined? That's the only part I'm a bit confused on.

avatar image syclamoth · Apr 12, 2012 at 12:37 AM 0
Share

Obviously 'Tile' is a class that you declared elsewhere. If it's not in the Unity API, you need to write it yourself! The Unity API is a tool, not a complete program.

avatar image
1

Answer by Nalx · Nov 11, 2016 at 11:43 AM

To read your numbers try this :

 public void LoadText (string fileName){
         string[][] dataMatrix; 
         string[] result = File.ReadAllLines(fileName);
 
         dataMatrix = new String[result.Length][];
         for (int i = 0; i < result.Length; i++) {
 
             string[] entries = result [i].Split (','); 
         
 
             for (int j = 0; j < entries.Length; j++) {
 
                 dataMatrix[i] = entries; 
             }
         }
 
         for (int i = 0; i < result.Length; i++) {
             for (int j = 0; j < dataMatrix[i].Length; j++) {
                 Debug.Log (dataMatrix [i] [j]);
             }
         } 
     }

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

6 People are following this question.

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

Related Questions

How do I read read from and write to a text file? 2 Answers

DirectoryNotFoundException when running a build 1 Answer

Read txt into string array, recognize as commands with arguments 1 Answer

Efficient method of storing coordinate values sequentially in an array 0 Answers

Read txt file from Android Downloads file 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