Синтаксис:
Используется csharp
using UnityEngine;
using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using System.Xml;
using System.IO;
public class SaveLoad : MonoBehaviour {
Transform playerTransform;
public Vector3 playerPosition = new Vector3(0,0,0);
void Awake(){
playerTransform = GameObject.Find("player").transform;
Load();
}
void Save(){
string filepath = "data.xml";
XmlDocument xmlDoc = new XmlDocument ();
XmlNode rootNode = null;
if (!File.Exists(filepath))
{
rootNode = xmlDoc.CreateElement ("level");
xmlDoc.AppendChild (rootNode);
}
else {
xmlDoc.Load(filepath);
rootNode = xmlDoc.DocumentElement;
}
rootNode.RemoveAll ();
XmlElement elmNew = xmlDoc.CreateElement ("player");
XmlAttribute position_Z = xmlDoc.CreateAttribute ("position_z");
position_Z.Value = playerTransform.position.z.ToString ();
XmlAttribute position_Y = xmlDoc.CreateAttribute ("position_y");
position_Y.Value = playerTransform.position.y.ToString ();
XmlAttribute position_X = xmlDoc.CreateAttribute ("position_x");
position_X.Value = playerTransform.position.x.ToString();
elmNew.SetAttributeNode (position_Z);
elmNew.SetAttributeNode (position_Y);
elmNew.SetAttributeNode (position_X);
rootNode.AppendChild (elmNew);
xmlDoc.Save (filepath);
}
void Load(){
string filepath = "data.xml";
XmlDocument xmlDoc = new XmlDocument ();
xmlDoc.Load(filepath);
XmlNodeList levelList = xmlDoc.GetElementsByTagName ("Level");
foreach (XmlNode level in levelList)
{
XmlNode node = level.SelectSingleNode("player");
XmlAttributeCollection attr = node.Attributes;
string s1 = attr[0].Value;
float x_coord = System.Convert.ToSingle (s1);
s1 = attr[1].Value;
float y_coord = System.Convert.ToSingle (s1);
s1 = attr[2].Value;
float z_coord = System.Convert.ToSingle (s1);
playerPosition = new Vector3(x_coord,y_coord,z_coord);
}
}
void Update () {
if (Input.GetKeyDown (KeyCode.I)) {
Save();
}
if (Input.GetKeyDown (KeyCode.P)) {
Load();
}
}
}
using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using System.Xml;
using System.IO;
public class SaveLoad : MonoBehaviour {
Transform playerTransform;
public Vector3 playerPosition = new Vector3(0,0,0);
void Awake(){
playerTransform = GameObject.Find("player").transform;
Load();
}
void Save(){
string filepath = "data.xml";
XmlDocument xmlDoc = new XmlDocument ();
XmlNode rootNode = null;
if (!File.Exists(filepath))
{
rootNode = xmlDoc.CreateElement ("level");
xmlDoc.AppendChild (rootNode);
}
else {
xmlDoc.Load(filepath);
rootNode = xmlDoc.DocumentElement;
}
rootNode.RemoveAll ();
XmlElement elmNew = xmlDoc.CreateElement ("player");
XmlAttribute position_Z = xmlDoc.CreateAttribute ("position_z");
position_Z.Value = playerTransform.position.z.ToString ();
XmlAttribute position_Y = xmlDoc.CreateAttribute ("position_y");
position_Y.Value = playerTransform.position.y.ToString ();
XmlAttribute position_X = xmlDoc.CreateAttribute ("position_x");
position_X.Value = playerTransform.position.x.ToString();
elmNew.SetAttributeNode (position_Z);
elmNew.SetAttributeNode (position_Y);
elmNew.SetAttributeNode (position_X);
rootNode.AppendChild (elmNew);
xmlDoc.Save (filepath);
}
void Load(){
string filepath = "data.xml";
XmlDocument xmlDoc = new XmlDocument ();
xmlDoc.Load(filepath);
XmlNodeList levelList = xmlDoc.GetElementsByTagName ("Level");
foreach (XmlNode level in levelList)
{
XmlNode node = level.SelectSingleNode("player");
XmlAttributeCollection attr = node.Attributes;
string s1 = attr[0].Value;
float x_coord = System.Convert.ToSingle (s1);
s1 = attr[1].Value;
float y_coord = System.Convert.ToSingle (s1);
s1 = attr[2].Value;
float z_coord = System.Convert.ToSingle (s1);
playerPosition = new Vector3(x_coord,y_coord,z_coord);
}
}
void Update () {
if (Input.GetKeyDown (KeyCode.I)) {
Save();
}
if (Input.GetKeyDown (KeyCode.P)) {
Load();
}
}
}
Позицию игрока скрипт то вроде бы сохраняет, вот текст в XML :
<level>
<player position_x="371.0868" position_y="456.4431" position_z="390.0807"/>
</level>
А вот при нажатие кнопки загрузки ничего не происходит, кто знает как это решить объясните пожалуйста.