Moongate Games UK

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

Tutorial: Building an In game Level Editor. (Part #1)

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

 In this tutorial series we'll be making a pretty basic in-game level editor inspired by the level editor in games such as Timesplitters 2. For this I'll be using a new project but it should be alright to add this to an already existing project.

 So to start with we are going to set up a  folder structure consisting of 'Level Parts' in a 'Prefabs' folder and a 'Level Editor' folder within the 'Scripts' folder. Then within the 'Level Editor' folder you will need to add 3 more folders one called 'Controls', another called 'Entities' and a last one called 'IO' like so.

This is the kind of folder structure you should be looking for. Though it doesn't matter that much, it's mainly to keep it neat and tidy.

This is the kind of folder structure you should be looking for. Though it doesn't matter that much, it's mainly to keep it neat and tidy.

 Next we'll start working on our first script which will be the controls for the Level Editor itself. For this we'll use the Axis and Buttons provided in Unity so we can have things like joystick support etc.

 So first we'll need to create a C# script in the 'Controls' folder called 'LE_Input' this will setup the base script that unity provides. You will then need to open up the script in the editor which use usually use with Unity3D (in my case it's Mono.) and add the following to the script.

private Vector3 _cursorPosition = new Vector3(0.0f,1.0f,0.0f);
private Vector3 _placementPosition = new Vector3(0.0f,0.0f,0.0f);

public GameObject Cursor;

 All of the above variables are pretty self explanatory '_cursorPosition' is the current position of the editors cursor. '_placementPosition' is the location we will pass when placing an object and 'Cursor' is the GameObject of the cursor itself.

 The next thing we need to do is the movement code itself, this will be using both the 'Horizontal' and 'Vertical' axis to detect movement on a range of input devices. So first things first we will need to add a new boolean variable called '_moved' and by default have it set to false. To do that we will need this line of code added below the 'Cursor' game object.

private bool _moved = false;

 The next thing we will need to do is to add an 'InputCheck()' method like so:

void InputCheck(){
    if(!_moved){
        if(Input.GetAxis ("Horizontal") > 0){
            _cursorPosition = new Vector3(_cursorPosition.x + 1.0f,1.0f,_cursorPosition.z);
            _placementPosition = new Vector3(_placementPosition.x + 1.0f,0.0f,_placementPosition.z);
            _moved = true;
        }
        else if(Input.GetAxis ("Horizontal") < 0){
            _cursorPosition = new Vector3(_cursorPosition.x - 1.0f,1.0f,_cursorPosition.z);
            _placementPosition = new Vector3(_placementPosition.x - 1.0f,0.0f,_placementPosition.z);
            _moved = true;
        }
        else if(Input.GetAxis ("Vertical") > 0){
            _cursorPosition = new Vector3(_cursorPosition.x,1.0f,_cursorPosition.z + 1.0f);
            _placementPosition = new Vector3(_placementPosition.x,0.0f,_placementPosition.z + 1.0f);
            _moved = true;
        }
        else if(Input.GetAxis ("Vertical") < 0){
            _cursorPosition = new Vector3(_cursorPosition.x,1.0f,_cursorPosition.z - 1.0f);
            _placementPosition = new Vector3(_placementPosition.x,0.0f,_placementPosition.z - 1.0f);
            _moved = true;
        }
        Cursor.transform.position = _cursorPosition;
    }
    if(Input.GetAxis ("Horizontal") == 0 && Input.GetAxis ("Vertical") == 0){
        _moved = false;
    }
}

 The above code will set the new position of the cursor based on what direction the Axis iis moved. To limit the cursor moving to only one button press I added in the _moved = true; part within the if(!_moved) If _moved == false statement, and added an if statement to set '_moved' to false when both the Horizontal and Vertical axis equal 0.

 The next thing we need to do is to add the function to run in the 'void Update()' loop. To do this we just type InputCheck(); between the curly braces.

 Once you have done that save your script and flick back to the Unity Editor. You will now need to make two Empty GameObjects call one of them 'Cursor' and place it at X: 0.0, Y: 1.0 and Z: 0.0. This will be the cursor itself. The other empty GameObject you will need to name 'scriptHolder' this will hold the main scripts for the level editor.

 You will then need to create a cube as the child of 'Cursor' and name the cube to 'CursorCube' this is more of a visual indicator of where the cursor is more than anything.

 Once you have set all of that up you will need to add the input script onto the 'scriptHolder' GameObject, you will also need to drag the Cursor object in the Hierarchy window over to the cursor slot in the LE_Input part of the inspector.

This is how adding the Cursor to the LE_Input script.

This is how adding the Cursor to the LE_Input script.

 When you have put all of that together if you run project you should get something like the video below.

If you have had any issues with this part of the tutorial don't hesitate to leave a comment below.

Part 2 -->