I have a save and load script like this:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Xml;
using System.Text;
using System.IO;
public class GameManager : MonoBehaviour {
private List<GameObject> Players = new List<GameObject>();
private string path;
public string mapName;
public Camera myCam;
private GameObject previousPlayer;
void Start () {
GameObject[] allPlayers = new GameObject[GameObject.FindGameObjectsWithTag("Player").Length];
allPlayers = GameObject.FindGameObjectsWithTag("Player");
for(int i = 0; i < allPlayers.Length; i++)
{
//getting all players that needs to be stored in xml
Players.Add(allPlayers*);*
-
}* -
}*
-
void Update () {*
-
if(Input.GetMouseButtonDown(0))* -
{* -
Ray ray = myCam.ScreenPointToRay (Input.mousePosition);* -
RaycastHit hit;* -
if (Physics.Raycast (ray, out hit, Mathf.Infinity)) {* -
if(hit.collider.tag == "Player")* -
{* -
Player playerScript = hit.collider.gameObject.GetComponent<Player>();* -
playerScript.canControl = true;* -
if(previousPlayer == null)* -
{* -
previousPlayer = hit.collider.gameObject;* -
}* -
else if(previousPlayer.transform != hit.collider.transform)* -
{* -
Player previousPlayerScript = previousPlayer.GetComponent<Player>();* -
previousPlayerScript.canControl = false;* -
previousPlayer = hit.collider.gameObject;* -
}* -
}* -
}* -
}* -
}*
-
void OnGUI()*
-
{*
-
if(GUI.Button(new Rect(0,0,100,50),"Save"))* -
{* -
Save();* -
}* -
if(GUI.Button(new Rect(110,0,100,50),"Load"))* -
{* -
Load();* -
}* -
}*
-
void Load()*
-
{*
-
path = Application.dataPath + "/XmlDocs/"+mapName+".xml";* -
XmlReader reader = XmlReader.Create(path);* -
XmlDocument xmlDoc = new XmlDocument();* -
xmlDoc.Load(reader);* -
XmlNodeList Data = xmlDoc.GetElementsByTagName("Data");* -
for(int i = 0; i < Data.Count;i++)* -
{* -
// getting data* -
XmlNode DataChilds = Data.Item(i);* -
// getting all gameObjects stored inside data* -
XmlNodeList allGameObjects = DataChilds.ChildNodes;* -
for(int j = 0; j < allGameObjects.Count ; j++)* -
{* -
XmlNode game_Objects = allGameObjects.Item(j);* -
//Finding the player, if he is present than loading its position and rotation* -
GameObject player = GameObject.Find(game_Objects.Name);* -
if(player)* -
{* -
XmlNodeList GameObjects_Position_Rotation = game_Objects.ChildNodes;* -
//First element have the position stored inside it* -
XmlNode GameObjects_Position = GameObjects_Position_Rotation.Item(0);* -
string[] split_position = GameObjects_Position.InnerText.Split(',');* -
player.transform.position = new Vector3(float.Parse(split_position[0]),float.Parse(split_position[1]),float.Parse(split_position[2]));* -
//Second element have the rotation stored inside it* -
XmlNode GameObjects_Rotation = GameObjects_Position_Rotation.Item(1);* -
string[] split_rotation = GameObjects_Rotation.InnerText.Split(',');* -
player.transform.rotation = new Quaternion(float.Parse(split_rotation[0]),float.Parse(split_rotation[1]),float.Parse(split_rotation[2]),float.Parse(split_rotation[3]));* -
}* -
}* -
}* -
reader.Close();*
}
-
void Save(){*
-
path = Application.dataPath + "/XmlDocs/"+mapName+".xml";* -
XmlDocument xmlDoc = new XmlDocument();* -
XmlElement elmRoot = xmlDoc.CreateElement("Data");* -
xmlDoc.AppendChild(elmRoot);*
-
for(int i = 0; i < Players.Count; i++)* -
{*
-
//Creating an xml element with player name*
XmlElement Player_Object = xmlDoc.CreateElement(Players*.name);
_ //Creating an xml element for saving player position*_
* XmlElement Player_Position = xmlDoc.CreateElement(“Position”);
_ //Combining player position x,y and z value into a single string separeted by commas*_
Player_Position.InnerText = Players.transform.position.x+“,”+Players_.transform.position.y+“,”+Players*.transform.position.z;
//Creating an xml element for saving player rotation*
* XmlElement Player_Rotation = xmlDoc.CreateElement(“Rotation”);*
* //Combining player rotation x,y,z and w value into a single string separeted by commas*
Player_Rotation.InnerText = Players.transform.rotation.x+“,”+Players.transform.rotation.y+“,”+Players.transform.rotation.z+“,”+Players*.transform.rotation.w;_
Player_Object.AppendChild(Player_Position);
Player_Object.AppendChild(Player_Rotation);
elmRoot.AppendChild(Player_Object);
_ }*_
* StreamWriter outStream = System.IO.File.CreateText(path);*
* xmlDoc.Save(outStream);*
* outStream.Close();*
* }*
}
The script above is just can save and load a current game, so if I want to save and load the game in different name I have to change the game name manually.
How can I save and load many games in different name automatically?
Please help me ![]()