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
6
Question by headkit · Apr 28, 2010 at 02:39 PM · arraytextfiledataread

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

hi folks!

i want to read a large amount of text-pairs like ["foo", "bar"] into my unity-document and want it to be easy to be changed later. in the end there should be an big array holding these pairs in their sub-arrays, like [["foo", "bar"], ["foo2", "bar2"], ["foo2", "bar2"], ...]

any ideas? thnx!

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
16
Best Answer

Answer by duck · Apr 28, 2010 at 03:51 PM

If by "read" you mean you want to read this data from a text file, you can add a text file to your project by copying it into your assets folder.

You can then reference the file in a variable whose type is "TextAsset" (you need to drag the reference in).

Then you could parse the text using String.Split. For example, if your data file uses commas to separate items in pairs, and newline characters to separate pairs, you could use this:

Original File:

apple,ball
car,dog
egg,fish
goat,hat

Script (JS):

var dataFile : TextAsset;

function Start() {

 var returnChar = "\n"[0];
 var commaChar = ","[0];

 var dataLines = dataFile.text.Split(returnChar);
 var buildDataPairs = new ArrayList();

 for (var dataLine in dataLines) {
     var dataPair = dataLine.Split(commaChar);
     buildDataPairs.Add(dataPair);
 }
 var dataPairs = buildDataPairs.ToArray();

 Debug.Log(dataPairs[2][1]);  // prints "fish"
 Debug.Log(dataPairs[3][0]);  // prints "goat"

}

Or, in C#:

using UnityEngine; public class ReadData : MonoBehaviour {

 public TextAsset dataFile;

 void Start() {

     string[] dataLines = dataFile.text.Split('\n');
     string[][] dataPairs = new string[dataLines.Length][];

     int lineNum = 0;
     foreach (string line in dataLines)
     {
         dataPairs[lineNum++] = line.Split(',');
     }

     Debug.Log(dataPairs[2][1]);  // prints "fish"
     Debug.Log(dataPairs[3][0]);  // prints "goat"
 }

}

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
1

Answer by johan-skold · Apr 28, 2010 at 03:29 PM

If I understand this correctly, you basically want a list with string key/value-pairs? For C# - if you're fine with having unique keys - you could use a Dictionary:

using System.Collections.Generic; using UnityEngine;

public class Test : MonoBehaviour { private Dictionary<string, string> pairs = new Dictionary<string, string>();

 public void Start()
 {
     pairs["foo"] = "bar";
     pairs["foo2"] = "bar2";
 }

}

I do not believe javascript has Generics support however, and I'm not sure how "proper" unity's javascript support is. If it's true javascript you could use built-in arrays. If not, you could settle for a HashTable (untested):

private var pairs : Hashtable = new Hashtable();

function Start() { pairs["foo"] = "bar"; pairs["foo2"] = "bar"; }

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

No one has followed this question yet.

Related Questions

2D array problem in C# 2 Answers

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

Getting A Variable From a Outside Text File 1 Answer

Trying to download and show contents of text file 1 Answer

How to read text file to array for my generateLetter script? 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