Saturday, August 15, 2015

Making a Lights Out Game in Unity 5 - Part 2


So I was trying to finish this today, but I guess I didn't work fast enough. I am actually almost done with the game, but the most important part of the game so far is the level editor that allows us to create levels on the fly and save them to a text file.

The little script is designed check which of the light switches are "on" and then writes it to a text file. Then I manually copy it over to an XML file that reads it to load levels. I will continue to work on this and hopefully I will be done tomorrow. That's all for now, thanks for reading!

levelEditor.cs:

using UnityEngine;
using System.Collections;
using System.IO;

public class levelEditor : MonoBehaviour {

public string levNumber;
string runtimeLevels;

// Use this for initialization
void Start () {
}

// Update is called once per frame
void Update () {
}

public void setLevelName(string nr) {
levNumber = nr;
}

public void ClearButton(){
for (int i = 1; i < 26; i++) {
if (GameObject.Find (i.ToString ()).GetComponent<lightSwitch> ().isOn) {
GameObject.Find (i.ToString ()).GetComponent<lightSwitch> ().change ();
}
}
}

public void SaveButton(){
string levelstring = "";

for (int i = 1; i<26; i++) {
if (GameObject.Find (i.ToString ()).GetComponent<lightSwitch> ().isOn) {
if (levelstring.Length == 0)
levelstring = i.ToString ();
else
levelstring += "," + i;
}
}

runtimeLevels += 
"\n\n" +
"<level>" + "\n" +
"<levelname>" + levNumber + "</levelname>" + "\n" +
"<setup>" + levelstring + "</setup>" + "\n" +
"</level>";
System.IO.File.WriteAllText("D:/Documents/Unity Projects/LightsOut/Assets/Resources/editor.txt", runtimeLevels);
}
}

No comments:

Post a Comment