Using unity and C# Create a StreamReader member variable for the script. Create
ID: 3689745 • Letter: U
Question
Using unity and C#
Create a StreamReader member variable for the script.
Create an empty method called LoadGems( ) that takes in a string that represents the file name. Don't hard-code it to "gem_data.txt". Inside of that method, bring the StreamReader variable to life, passing it the file name .
Below that, create a string called "dataline" and initialize it to the empty string ""
Immediately afterwards, read a line of data using the StreamReaders ReadLine() method.
This returns you the next available line in the file, so put it into "dataline".
While (dataline != null), print out dataline using Debug.Log() Inside that loop, read the next line of data.
At the bottom of the method (after the loop), close the file using sr.Close();
Explanation / Answer
below are the code ,with commeents to explain the code;
//include //
static GameObject obj;
public int j=0;
public String[] get = new String[5obj.transform.position = [b]new[/b] Vector3((pos[ind]),(pos[ind++]),(pos[ind++]));];
public int k = 0;
public float[] pos = new float[5];
public int ind = 0;
public String filename ;
//variable declare//
void Start() {
get = readfile("hero.txt");
pos = positionread("posizione.txt");
int i = 0;
Debug.Log((get.Length)-1);
while(get.Length!=i+1){
Debug.Log(get[i]);
obj = (GameObject)Instantiate(UnityEngine.Resources.Load(get[i]));
Vector3 tmp = obj.transform.localScale;
tmp.x += 50f;
tmp.y += 50f;
tmp.z += 60f;
obj.transform.localScale = tmp;
obj.transform.position = [b]new[/b] Vector3((pos[ind]),(pos[ind++]),(pos[ind++]));
ind++;
i++;
if(get.Length==i)
break;
}
j=i;
}
//for reading//
public String[] readfile (String filename) {
try {
int[] line ;
StreamReader sr = new StreamReader( Application.dataPath+"/"+filename);
int i = 0;
int p = 0;
while(p!=1){
line[i] = sr.ReadLine();
if(line[i]=="stop"){
p = 1;
line[i]=null;
}
}
sr.Close();
sr = null;
return line;
}
catch (Exception e) {
Debug.Log("internal error!");
return null;
}
}
//for position //
public float[] positionread (String filename) {
try {
float[] pos ;
StreamReader dr = new StreamReader( Application.dataPath+"/"+filename);
int i = 0;
int j = 0;
while(j!=1){
pos[i] = dr.ReadLine();
if(pos[i]=="stop"){
j = 1;
pos[i]=null;
}
dr.Close();
dr = null;
return pos;
}
catch (Exception e) {
Debug.Log("internal error!");
return null;
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.