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
1
Question by gilgada · Mar 23, 2012 at 10:43 PM · inputtextstringtextfieldread

Taking data from text file

Currently I have a TextField that accepts an input string and then in another script, this string is passed on and if statements check the string, moving the camera to a different position if the string is of the specified criteria. The problem is, I will have many different strings and matching coordinates for each one (the coordinates correspond to the camera's transform.position). I don't want to write a different if statement for each one. What I am looking to do is have a single if statement that checks the string from TextField against the contents of a text file. The relevant coordinate will then be taken form the text file and passed to the transform.position of the camera object.

The text file will be in the format of String/Coordinate on each line. An example would be

 England/(-87.37f,49.43f,20.22f) 

Currently my script has the following code:

using UnityEngine;

using System.Collections;

class CameraPositions : MonoBehaviour {

 public SearchField Field;
 private Vector3 Entry;
 private string Result;
 private bool confirm;
 
 #region methods
 void Update()
 {
     confirm = Field.Confirm;
     if(confirm == true)
     {
         UpdateResult();
     }
     Debug.Log (transform.position);
 }
     
 void UpdateResult()
 {
     Result = Field.Search;
     if(Result ==  "England" )
     {
         Entry = new Vector3( -87.37f,49.43f,20.22f);
         UpdateCamera();
     }
     
 }

 void UpdateCamera()
 {
     GameObject CameraContainer = GameObject.FindGameObjectWithTag("Player");
     CameraContainer.transform.position = Entry;
 }
 #endregion

}

I have looked at similar questions on here but they seem to be solved by using arrays. I'm not sure if my problem requires an array or not and do not have much experience with them.

Any help would be greatly appreciated.

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

Answer by raoz · Mar 26, 2012 at 03:39 PM

Here, this should work, I tested it, but it uses arrays. Basically it reads the file at the beginning, and has a search function. Hope it works. I tested it with the following:

England/(-87.37,49.43,20.22) Estonia/(-12.34,45.67,78.90)

And it worked


Coordinates.cs


 using UnityEngine;
 using System.IO;
 using System.Collections;
 
 public class Coordinates : MonoBehaviour {
     public string[] Names;
     public Vector3[] Locations;
     string filepath = "./example.txt"; //This is the path of the text file
     
     // This gets all the locations from the file
     void Start () {
         string[] filelines = File.ReadAllLines(filepath);
         Names = new string[filelines.Length];
         Locations = new Vector3[filelines.Length];
         for(int i = 0; i < filelines.Length; i++)
         {
             string[] parts = filelines[i].Split("/"[0]);
             Names[i] = parts[0];
             string coords = parts[1].Substring(1,parts[1].Length-3);
             string[] coord = coords.Split(","[0]);
             float coord0 = float.Parse(coord[0]);
             float coord1 = float.Parse(coord[1]);
             float coord2 = float.Parse(coord[2]);
             Locations[i] = new Vector3(coord0, coord1, coord2);
             Debug.Log(Names[i] + " : " + Locations[i].ToString());
         }
     }
     
     //This finds the entry
     //It takes one parameter, the string it is searching for
     //If it finds it, returns the location assosiated with it
     //Else, returns 0, 0, 0
     Vector3 FindEntry (string findthis) {
         for(int i = 0; i < Names.Length; i++)
         {
             if(Names[i] == findthis) //Iterates trought names and checks if the name is what we are looking for
             {
                 Debug.Log("Found: " + findthis + " at " + Locations[i].ToString());
                 return Locations[i]; //Returns the location associated with it
             }
         }
         //This is what happens if it found it
         Debug.Log("Unable to find the location specified");
         Vector3 result = new Vector3(0,0,0);
         return result; //Returns (0, 0, 0)
     }
 }
Comment
Add comment · Show 9 · 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 gilgada · Mar 26, 2012 at 08:32 PM 0
Share

awesome! this looks great! just a quick question, for the filepath of my text file, is it relative to the root folder of my unity project?

avatar image raoz · Mar 26, 2012 at 08:55 PM 1
Share

You can make it relative or absolute, if you make it relative(like $$anonymous$$e), than yeah, it is relative to the root folder. Now could you please tick it as the correct answer?

avatar image gilgada · Mar 26, 2012 at 08:58 PM 0
Share

and also, how can I amend my original CameraPositions.cs to pass its Result from the TextField to the Coordinates class? I have set a return method in CameraPositions.cs to make Result accessible but I'm not sure what to do next.

avatar image raoz · Mar 26, 2012 at 09:19 PM 1
Share

Actually, you call the FindEntry function from your other script. Easiest way to do this (because these 2 work together anyways) is to make your script inherit from $$anonymous$$e, to do this you replace this: "class CameraPositions : $$anonymous$$onoBehaviour {" with this: "class CameraPositions : Coordinates {" and make your Update method look like this: Result = Field.Search(); Entry = FindEntry(Result); I hope you understand me.

avatar image raoz · Mar 26, 2012 at 09:44 PM 1
Share

Did you get any debug logs? I think it must be "./places.txt"

Show more comments
avatar image
1

Answer by DaveA · Mar 24, 2012 at 12:25 AM

Not sure what Field is about, but if you want to parse that string into something useful, here's some nuggets:

Assume you read England/(-87.37f,49.43f,20.22f) into a string called 'inputString'

 string[] parts = inputString.Split("/"[0]);
 // parts[0] now has "England" and parts[1] has "(-87.37f,49.43f,20.22f)"
 string coords = parts[1].Substring(1,parts[1].Length-2); // now has "-87.37f,49.43f,20.22f"
 string coord = coords.Split(","[0]);
 float x = parseFloat(coord[0]);
 float y = parseFloat(coord[1]);
 float z = parseFloat(coord[2]);
 Vector3 pos = new Vector3 (x,y,z);
Comment
Add comment · Show 8 · 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 gilgada · Mar 24, 2012 at 11:06 AM 0
Share

Field is a reference to my search field class that has the OnGUI.TextField function. Your solution is great, but how could I get it to check the result from Field against all entries in the text file and then output the location coordinates to a vector3?

avatar image gilgada · Mar 25, 2012 at 12:30 PM 0
Share

would this require arrays to accomplish?

avatar image DaveA · Mar 25, 2012 at 10:12 PM 1
Share

The Split function results in an array of strings. You can also parse your file that way, just load the file into a string and split it, say, on linefeeds (each array entry will be one line of the file). Then loop through that array and (split more if you need to) to find what you need. Refer to http://msdn.microsoft.com/en-us/library/system.string_methods.aspx

avatar image gilgada · Mar 25, 2012 at 10:51 PM 0
Share

thanks :) I'll take a look into this in the morning!

avatar image gilgada · Mar 26, 2012 at 09:18 AM 0
Share

cant make much sense of it. I've looked at the String.Split $$anonymous$$ethod and I'm unsure on how to use it. I've also looked at the code you posted above and was wondering how to point to the text file to use.

Show more comments

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 to put each line of a text file into a separate sring c# 1 Answer

How to define in which text field the cursors is located 2 Answers

comparing a number input to a letter input 3 Answers

Android Keyboard .text String returning empty? 1 Answer

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