Hi together,
I have a script to Save GameObject, but it does not work.
The Positions of the GameObject will be saved in XML but not load aggain
Please Help me!^^
Here is the script:
using UnityEngine;
using System.Collections;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
using System.Text;
public class CubeSaver: MonoBehaviour
{
Rect _Save, _Load, _SaveMSG, _LoadMSG;
bool _ShouldSave, _ShouldLoad,_SwitchSave,_SwitchLoad;
string _FileLocation,_FileName;
public GameObject _Player;
UserData myData;
string _data;
public GameObject loadObject; //Objekt-Prefab, damit beim Laden die Objekte instanziiert werden können
public GameObject[ ] myObjects;
float VPositionX;
float VPositionY;
float VPositionZ;
int VarrayCount;
// When the EGO is instansiated the Start will trigger
// so we setup our initial values for our local members
void Start () {
// We setup our rectangles for our messages
_Save=new Rect(10,80,100,20);
_Load=new Rect(10,100,100,20);
_SaveMSG=new Rect(10,120,400,40);
_LoadMSG=new Rect(10,140,400,40);
// Where we want to save and load to and from
_FileLocation=Application.dataPath;
_FileName=“WorldsSave.xml”;
// we need soemthing to store the information into
myData=new UserData();
}
void OnGUI()
{
//************************************************** *
// Loading The Player…
// **************************************************
if (GUI.Button(_Load,“Load”)) {
GUI.Label(_LoadMSG,"Loading from: "+_FileLocation);
// Load our UserData into myData
LoadXML();
if(_data.ToString() != “”)
{
// notice how I use a reference to type (UserData) here, you need this
// so that the returned object is converted into the correct type
myData = (UserData)DeserializeObject(_data);
// Anzahl der Objekte laden
VarrayCount = myData._iUser.arrayCount;
//myObjects = GameObject.FindGameObjectsWithTag(“SaveTags”);
for (var i = 0; i < VarrayCount; i++) //alle Elemente der Array durchgehen
{
//Daten laden // load
VPositionX = myData.iUser.xArray*;*
VPositionY = myData.iUser.yArray*;
VPositionZ = myData._iUser.zArray;*
//Objekt erstellen // Instantiate Object
Instantiate(loadObject, new Vector3(VPositionX, VPositionY, VPositionZ), Quaternion.identity);
}
// just a way to show that we loaded in ok
//Debug.Log(myData._iUser.name);
}
}
//************************************************** *
// Saving The Player…
// **************************************************
if (GUI.Button(_Save,“Save”))
{
myObjects = GameObject.FindGameObjectsWithTag(“SaveTags”);
myData._iUser.xArray = new float[myObjects.Length]; //Array mit so vielen Elementen wie zu speichernden Objekten
myData._iUser.yArray = new float[myObjects.Length];
myData._iUser.zArray = new float[myObjects.Length];
GUI.Label(_SaveMSG,"Saving to: "+_FileLocation);
for (var i = 0; i < myObjects.Length; i++) //alle Elemente der Array durchgehen
{
myData.iUser.xArray = myObjects*.transform.position.x; //x-Position des aktuellen Elements an die richtige Stelle in die Array der x-Werte schreiben*
myData.iUser.yArray = myObjects*.transform.position.y;*
myData.iUser.zArray = myObjects*.transform.position.z;*
}
// Create the XML
_data = SerializeObject(myData);
// This is the final resulting XML from the serialization process
CreateXML();
Debug.Log(_data);
}
}
/* The following metods came from the referenced URL /
string UTF8ByteArrayToString(byte[ ] characters)
{
UTF8Encoding encoding = new UTF8Encoding();
string constructedString = encoding.GetString(characters);
return (constructedString);
}
byte[ ] StringToUTF8ByteArray(string pXmlString)
{
UTF8Encoding encoding = new UTF8Encoding();
byte[ ] byteArray = encoding.GetBytes(pXmlString);
return byteArray;
}
// serialize the UserData object of myData*
string SerializeObject(object pObject)
{
string XmlizedString = null;
MemoryStream memoryStream = new MemoryStream();
XmlSerializer xs = new XmlSerializer(typeof(UserData));
XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
xs.Serialize(xmlTextWriter, pObject);
memoryStream = (MemoryStream)xmlTextWriter.BaseStream;
XmlizedString = UTF8ByteArrayToString(memoryStream.ToArray());
return XmlizedString;
}
// Deserialize it back into its original form
object DeserializeObject(string pXmlizedString)
{
XmlSerializer xs = new XmlSerializer(typeof(UserData));
MemoryStream memoryStream = new MemoryStream(StringToUTF8ByteArray(pXmlizedString) );
XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
return xs.Deserialize(memoryStream);
}
// Finally the save and load methods for the file itself
void CreateXML()
{
StreamWriter writer;
FileInfo t = new FileInfo(_FileLocation+“\”+ _FileName);
if(!t.Exists)
{
writer = t.CreateText();
}
else
{
t.Delete();
writer = t.CreateText();
}
writer.Write(_data);
writer.Close();
Debug.Log(“File written.”);
}
void LoadXML()
{
StreamReader r = File.OpenText(_FileLocation+“\”+ _FileName);
string _info = r.ReadToEnd();
r.Close();
_data=_info;
Debug.Log(“File Read”);
}
}
// UserData is our custom class that holds our defined objects we want to store in XML format
public class UserData
{
// Define a default instance of the structure
public DemoData _iUser;
// Default constructor doesn’t really do anything at the moment
public UserData() { }
// Store in the XML file, defined here
public struct DemoData
{
public float[ ] xArray; //Array für alle X-Positionen
public float[ ] yArray; //Array für alle Y-Positionen
public float[ ] zArray; //Array für alle Z-Positionen
public int arrayCount; //Anzahl der zu speichernden/zu ladenden Objekte
}
}
I hope you see the logical “error”
PS: The problem should be in Loading Player^^_