Hello people, i am quite new to programming in unity and i have done multiple searches on the internet and ended with a long method of saving my level, the game is a lego based game like minecraft. the method i am using (prepare your reading glasses and a cup of coffie) is:
private List<Object> LegoBlocks = new List<Object>();
private List<Vector3> Positions = new List<Vector3>();
private List<Quaternion> Rotations = new List<Quaternion>();
private List<Object> LegoBlocks1 = new List<Object>();
private List<Vector3> Positions1 = new List<Vector3>();
private List<Quaternion> Rotations1 = new List<Quaternion>();
private List<Vector3> Spacings = new List<Vector3>();
public int listlenght = 0;
public GameObject[] block11;
public GameObject[] block12;
public GameObject[] block22;
public GameObject[] blockplate11;
public GameObject[] blockround11;
public GameObject[] blockroundplate11;
public GameObject[] blocklog11;
public GameObject plant11;
public GameObject door134;
public bool cansave = false;
public bool canreset = false;
public bool canload = false;
// Use this for initialization
void Awake ()
{
if (!Directory.Exists (@"C:\Users\Public\Documents\Saves")) {
Directory.CreateDirectory (@"C:\Users\Public\Documents\Saves");
StreamWriter writer = new StreamWriter (@"C:\Users\Public\Documents\Saves\TestSave.txt");
writer.WriteLine ("Empty");
writer.Close ();
}
else
ReadFromFile();
}
void ObjectReciever(List<Object> blocks)
{
LegoBlocks = blocks;
}
void PositionReciever(List<Vector3> Poss)
{
Positions = Poss;
}
void RotationsReciever(List<Quaternion> rots)
{
Rotations = rots;
}
void SpacingReciever(List<Vector3> spaces)
{
Spacings = spaces;
}
// Update is called once per frame
void Update ()
{
if (cansave == true) {
LegoBlocks.AddRange (LegoBlocks1);
Positions.AddRange (Positions1);
Rotations.AddRange (Rotations1);
LegoBlocks1.Clear();
Positions1.Clear();
Rotations1.Clear();
SaveToFile ();
cansave = false;
}
if (canload == true)
{
Application.LoadLevel (0);
canreset = false;
}
if (canreset == true)
{
LevelReset ();
canload = false;
}
}
void SaveScriptAllowed (bool save)
{
cansave = save;
}
void ResetScriptAllowed (bool reset)
{
canreset = reset;
}
void LoadScriptAllowed (bool load)
{
canload = load;
}
void LevelReset ()
{
StreamWriter writer = new StreamWriter (@"C:\Users\Public\Documents\Saves\TestSave.txt");
writer.WriteLine ("Empty");
writer.Close ();
}
void SaveToFile ()
{
if (Directory.Exists(@"C:\Users\Public\Documents\Saves"))
{
LevelReset ();
StreamWriter writer = new StreamWriter (@"C:\Users\Public\Documents\Saves\TestSave.txt");
listlenght = 0;
foreach (Object o in LegoBlocks) {
listlenght++;
}
string[] blocks = new string[listlenght];
string[] pos = new string[listlenght];
string[] rot = new string[listlenght];
string[] space = new string[listlenght];
int a = 0;
foreach (Object o in LegoBlocks) {
blocks [a] = o.ToString ();
a++;
}
a = 0;
foreach (Vector3 p in Positions) {
pos [a] = p.ToString ();
a++;
}
a = 0;
foreach (Quaternion r in Rotations) {
rot [a] = r.ToString ();
a++;
}
for (int i = 0; i < listlenght; i++) {
writer.WriteLine (blocks <em>+ ":" + pos _+ ":" + rot *);*_</em>
* }*
* writer.Close();*
* }*
* else*
* {*
* Directory.CreateDirectory(@“C:\Users\Public\Documents\Saves”);*
* SaveToFile(); *
* }*
* }*
* void ReadFromFile ()*
* {*
* StreamReader reader = new StreamReader (@“C:\Users\Public\Documents\Saves\TestSave.txt”);*
* string s = reader.ReadLine ();*
* if (s != “Empty”) {*
* while (s != null) {*
* char[] delimiter1 = {‘:’};*
* char[] delimiter2 = {‘,’};*
* string[] fields1 = s.Split (delimiter1);*
* string posstring1 = fields1 [1];*
* int t = posstring1.Length;*
* posstring1 = posstring1.Remove (t - 1);*
* posstring1 = posstring1.TrimStart (‘(’);*
* string[] posstring2 = posstring1.Split (delimiter2);*
* float[] pos1 = new float[3];*
* pos1 [0] = System.Convert.ToSingle (posstring2 [0]);*
* pos1 [1] = System.Convert.ToSingle (posstring2 [1]);*
* pos1 [2] = System.Convert.ToSingle (posstring2 [2]);*
* Vector3 pos = new Vector3 (pos1 [0], pos1 [1], pos1 [2]);*
* string rotstring1 = fields1 [2];*
* int r = rotstring1.Length;*
* rotstring1 = rotstring1.Remove (r - 1);*
* rotstring1 = rotstring1.TrimStart (‘(’);*
* string[] rotstring2 = rotstring1.Split (delimiter2);*
* float[] rot1 = new float[4];*
* rot1 [0] = System.Convert.ToSingle (rotstring2 [0]);*
* rot1 [1] = System.Convert.ToSingle (rotstring2 [1]);*
* rot1 [2] = System.Convert.ToSingle (rotstring2 [2]);*
* rot1 [3] = System.Convert.ToSingle (rotstring2 [3]);*
* Quaternion rot = new Quaternion (rot1 [0], rot1 [1], rot1 [2], rot1 [3]);*
* if (fields1 [0] == “1 1 Black(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (block11 [0], pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* if (fields1 [0] == “1 1 Blue (Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (block11 [1], pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* if (fields1 [0] == “1 1 Brown(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (block11 [2], pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* if (fields1 [0] == “1 1 Green(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (block11 [3], pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* if (fields1 [0] == “1 1 Pink(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (block11 [4], pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* if (fields1 [0] == “1 1 Purple(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (block11 [5], pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* if (fields1 [0] == “1 1 Red(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (block11 [6], pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* if (fields1 [0] == “1 1 White(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (block11 [7], pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* if (fields1 [0] == “1 1 Yellow(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (block11 [8], pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* if (fields1 [0] == “1 2 Black(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (block12 [0], pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* if (fields1 [0] == “1 2 Blue(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (block12 [1], pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* if (fields1 [0] == “1 2 Brown(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (block12 [2], pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* if (fields1 [0] == “1 2 Green(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (block12 [3], pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* if (fields1 [0] == “1 2 Pink(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (block12 [4], pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* if (fields1 [0] == “1 2 Purple(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (block12 [5], pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* if (fields1 [0] == “1 2 Red(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (block12 [6], pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* if (fields1 [0] == “1 2 White(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (block12 [7], pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* if (fields1 [0] == “1 2 Yellow(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (block12 [8], pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* if (fields1 [0] == “2 2 Black(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (block22 [0], pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* if (fields1 [0] == “2 2 Blue(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (block22 [1], pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* if (fields1 [0] == “2 2 Brown(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (block22 [2], pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* if (fields1 [0] == “2 2 Green(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (block22 [3], pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* if (fields1 [0] == “2 2 Pink(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (block22 [4], pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* if (fields1 [0] == “2 2 Purple(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (block22 [5], pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* if (fields1 [0] == “2 2 Red(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (block22 [6], pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* if (fields1 [0] == “2 2 White(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (block22 [7], pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* if (fields1 [0] == “2 2 Yellow(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (block22 [8], pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* if (fields1 [0] == “1 1 Black Plate(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (blockplate11 [0], pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* if (fields1 [0] == “1 1 Blue Plate(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (blockplate11 [1], pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* if (fields1 [0] == “1 1 Brown Plate(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (blockplate11 [2], pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* if (fields1 [0] == “1 1 Green Plate(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (blockplate11 [3], pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* if (fields1 [0] == “1 1 Pink Plate(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (blockplate11 [4], pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }if (fields1 [0] == “1 1 Purple Plate(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (blockplate11 [5], pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* if (fields1 [0] == “1 1 Red Plate(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (blockplate11 [6], pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* if (fields1 [0] == “1 1 White Plate(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (blockplate11 [7], pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* if (fields1 [0] == “1 1 Yellow Plate(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (blockplate11 [8], pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* if (fields1 [0] == “1 1 Black Round(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (blockround11 [0], pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* if (fields1 [0] == “1 1 Blue Round(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (blockround11 [1], pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* if (fields1 [0] == “1 1 Brown Round(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (blockround11 [2], pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* if (fields1 [0] == “1 1 Green Round(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (blockround11 [3], pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* if (fields1 [0] == “1 1 Pink Round(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (blockround11 [4], pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* if (fields1 [0] == “1 1 Purple Round(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (blockround11 [5], pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* if (fields1 [0] == “1 1 Red Round(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (blockround11 [6], pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* if (fields1 [0] == “1 1 White Round(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (blockround11 [7], pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* if (fields1 [0] == “1 1 Yellow Round(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (blockround11 [8], pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* if (fields1 [0] == “1 1 Black Round Plate(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (blockroundplate11 [0], pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* if (fields1 [0] == “1 1 Blue Round Plate(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (blockroundplate11 [1], pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* if (fields1 [0] == “1 1 Brown Round Plate(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (blockroundplate11 [2], pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* if (fields1 [0] == “1 1 Green Round Plate(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (blockroundplate11 [3], pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* if (fields1 [0] == “1 1 Pink Round Plate(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (blockroundplate11 [4], pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* if (fields1 [0] == “1 1 Purple Round Plate(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (blockroundplate11 [5], pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* if (fields1 [0] == “1 1 Red Round Plate(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (blockroundplate11 [6], pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* if (fields1 [0] == “1 1 White Round Plate(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (blockroundplate11 [7], pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* if (fields1 [0] == “1 1 Yellow Round Plate(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (blockroundplate11 [8], pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* if (fields1 [0] == “1 2 Black Log(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (blocklog11 [0], pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* if (fields1 [0] == “1 2 Blue Log(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (blocklog11 [1], pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* if (fields1 [0] == “1 2 Brown Log(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (blocklog11 [2], pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* if (fields1 [0] == “1 2 Green Log(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (blocklog11 [3], pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* if (fields1 [0] == “1 2 Pink Log(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (blocklog11 [4], pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* if (fields1 [0] == “1 2 Purple Log(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (blocklog11 [5], pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* if (fields1 [0] == “1 2 Red Log(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (blocklog11 [6], pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* if (fields1 [0] == “1 2 White Log(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (blocklog11 [7], pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* if (fields1 [0] == “1 2 Yellow Log(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (blocklog11 [8], pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* if (fields1 [0] == “1 1 Leave(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (plant11, pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* if (fields1 [0] == “1 3 4 Door(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (door134, pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* s = reader.ReadLine ();*
* }*
* }*
* }*
As you can see its a mess and i was wondering if there was an easier way to do this than having to save every block type individually and having to make new variables for each new block i put in, if anyone knows a better way to save a modified level then can you please help me, your help is appreciated.