- Home /
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.
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)
}
}
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?
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?
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.
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.
Did you get any debug logs? I think it must be "./places.txt"
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);
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?
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
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.