- Home /
Question by
vishnu gb · Mar 03, 2015 at 09:07 AM ·
instantiatecubexmlread
instantiate a cube at a position by read from xml file
hello, While reading from the xml file,instantiating of start and dest is not visible(it shows up when z cooedinate value changed to -2) while without xml reading it works fine(for openf == 0).Iam really not able to figure out what is the problem.Please help
void Start()
{
if (PlayerPrefs.GetInt("openf") == 0)
{
size.x = PlayerPrefs.GetInt("size_l");
size.y = PlayerPrefs.GetInt("size_b");
parent_nodes = new nodes[(int)size.x * (int)size.y];
grid_arr = new Transform[(int)size.x, (int)size.y];//array to refference to grids created
for (int x = 0; x < size.x; x++)//creating grids
{
for (int z = 0; z < size.y; z++)
{
Transform newcel;
newcel = (Transform)Instantiate(cellprefab, new Vector3(x, z, 0), Quaternion.identity);
newcel.name = (newcel.position.x.ToString() + newcel.position.y.ToString());
newcel.parent = transform;
grid_arr[x, z] = newcel;
}
}
creategrid();
}
else
{
string xml = PlayerPrefs.GetString("xmlfile");
int x = 0, y = 0;
file_path = PlayerPrefs.GetString("file");
if (file_path != "")
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xml);
XmlElement root = xmlDoc.DocumentElement;
if (root.HasAttributes)
{
int.TryParse(root.GetAttribute("size_x"), out x);
int.TryParse(root.GetAttribute("size_y"), out y);
size.x = x;
size.y = y;
parent_nodes = new nodes[(int)size.x * (int)size.y];
grid_arr = new Transform[(int)size.x, (int)size.y];//array to refference to grids created
}
XmlNodeList grds = xmlDoc.GetElementsByTagName("Grid");
bool f = false;
foreach (XmlNode nd in grds)
{
XmlElement elm = (XmlElement)nd;
if (elm.GetElementsByTagName("name")[0].InnerText == "block")
{
int.TryParse(elm.GetElementsByTagName("X")[0].InnerText, out x);
int.TryParse(elm.GetElementsByTagName("Y")[0].InnerText, out y);
b = (Transform)Instantiate(block, new Vector3(x, y, -1), Quaternion.identity);
b.parent = transform;
b.name = "block";
grid_arr[x, y] = b;
}
else
{
int.TryParse(elm.GetElementsByTagName("X")[0].InnerText, out x);
int.TryParse(elm.GetElementsByTagName("Y")[0].InnerText, out y);
b = (Transform)Instantiate(cellprefab, new Vector3(x, y, -1), Quaternion.identity);
b.parent = transform;
b.name = x.ToString()+y.ToString();
grid_arr[x, y] = b;
}
}
}
else
{
MessageBox.Show("!!!Corrupted XML file \n \t or\n Invalid XML file...");
}
creategrid();
}
}
void creategrid()
{
Camera.main.transform.position = new Vector3(grid_arr[(int)(size.x / 2), (int)(size.y / 2)].position.x, grid_arr[(int)(size.x / 2), (int)(size.y / 2)].position.y, -5);//scaling to the view
/////////////////////////////////////////Set camera view///////////////////////////////////////////////
bool f = false;
for (int i = 0; i < size.x; i++)//creating grids
{
for (int j = 0; j < size.y; j++)
{
if (grid_arr[i, j].name != "block")
{
if (f == false)
{
start = (Transform)Instantiate(start, new Vector3(i, j, -1), Quaternion.identity);
dest = (Transform)Instantiate(dest, new Vector3(i, j, -1), Quaternion.identity);
f = true;
break;
}
}
}
if (f == true)
break;
}
if (size.x > size.y)
{
Camera.main.orthographicSize = (size.x / 2) + 3f;//projecting the camera view to the game
}
else if (size.x < size.y)
{
Camera.main.orthographicSize = (size.y / 2) + 3f;//projecting the camera view to the game
}
else
{
Camera.main.orthographicSize = (size.x / 2) + 3f;//projecting the camera view to the game
}
}
Comment
Your answer
Follow this Question
Related Questions
Reading XML file 0 Answers
Place cubes? problem with my script :S 1 Answer
How do I read XML on iPhone? 3 Answers