Moongate Games UK

"Changing worlds, one line of code at a time"

Tutorial: Building an in game Level Editor (Part #5)

Game Engine: Unity 5.1
Language: C#
Subject: In-game Level Editor

 In this part of the In-game Level Editor series we're going to be starting work on setting up the saving and loading methods for the Level Editor.

 So to start with we're going to work on coding up a basic saving system which will save all the level data in a binary format. So first things first we need to create a new C# script under the 'IO' folder called 'FileIO' this will hold all the Loading and Saving functions for the level editor and could also be used for loading the level in game as well.

 Before we do anything else we'll need to get unityserializer-ng which a saving solution for Unity3D. To install this check the install instructions near the bottom of the unityserializer-ng page.

 Once you have installed Unity Serializer into your project you'll need to open up the 'FileIO' script we made earlier and make it look like the following code.

using UnityEngine;
using System.Collections;
using System.IO;
using Entities;
using Serialization;

public class FileIO : MonoBehaviour {

    public void SaveMap(string fileName, Level levelData){
        var levelDataBytes = Serialization.UnitySerializer.Serialize(levelData);
        if(!Directory.Exists (Application.persistentDataPath + "/CustomLevels/"))
            Directory.CreateDirectory(Application.persistentDataPath + "/CustomLevels/");

        var filePath = Application.persistentDataPath + "/CustomLevels/" + fileName + ".lvl";

        using (var fs = new FileStream(filePath,FileMode.Create,FileAccess.Write)){
            using (var bw = new BinaryWriter(fs)){
                bw.Flush ();


    public Level LoadMap(string fileName){
        Level loadedLevel = null;

        var filePath = Application.persistentDataPath + "/CustomLevels/" + fileName + ".lvl";

        using (var fs = new FileStream(filePath,FileMode.Open,FileAccess.Read)){
            loadedLevel = UnitySerializer.Deserialize<Level>(fs);

        Debug.Log (loadedLevel.LevelName);
        Debug.Log (loadedLevel.LevelDescription);
        Debug.Log (loadedLevel.Author);
        Debug.Log (loadedLevel.LevelParts.Count);

        return loadedLevel;

 You will need to add error handling to this at some point before you release your game with this in it. But what these two methods do are save and load the level data in a binary format, so if you attempt that now, you are likely to get a few errors due to the way that the Level class and in the LevelPart class.

 To rectify this we will need to change the code in the said classes.

 So firstly we'll need to add empty constructors to both of the classes. Then we'll need to remove the private variables and remove the '{ get { return blahblah; } }' part from each of the public variables. This should work, but if it doesn't then comment below and I'll give you a hand.

 Presuming that the there are no errors (there maybe some warnings but that's fine.) we can move on to a really basic user interface for the level editor itself in the next part.

<-- Part 4 | Part 6 (Coming Soon)