Moongate Games UK

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

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

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

 In this part of the tutorial we'll start working on making a basic object selection script and linking it to our input list.

 The first thing we'll need to do for this is to make a few basic objects for the probably something like these below.

These are a the test objects that I'll be using for this tutorial.

These are a the test objects that I'll be using for this tutorial.

 Once you have made all the objects put them into prefabs into the 'Level Parts' folder (hopefully with better naming than me.)

Should probably have better naming for some of these.

Should probably have better naming for some of these.

 Next we'll be working on storing this object collection in a script so we can select the object we want to place. To do this we'll start by making a new C# script called 'PartList' this will hold references to each of the prefabs along with the currently selected part.

 The first thing we'll need to do when we open the 'PartList' script is to add the following to the top of the script:

using System.Collections.Generic;

 This will enable us to use the things such as List<> and Dictionary<,>. So next we'll be adding the following variables to the script.

public List<GameObject> Parts = new List<GameObject>();
public GameObject SelectedPart;

private int _selectedPartNumber = 0;

 The first 'Parts' variable will hold the list of GameObjects which we'll be using for the level editor. Please note that in an actual game it's probably a better idea to have a centralized list of objects, like in a file or something that can be loaded outside the editor itself as well you'll find out why in a later tutorial.
 The second variable 'SelectedPart' is pretty self explanatory being that it holds the currently selected game object.
 The third and final variable '_selectedPartNumber' holds the number of the selected part for use when cycling through the list.
(Which is what we're going on to now.)

 The first thing that we'll need to do is remove the 'void Update()' method since we won't need that in this script.

 Then we'll need to add a line of code to the 'void Start()' method which will select the first in the 'PartList'.

SelectedPart = Parts[_selectedPartNumber];

 Next we'll need to code the logic for the logic for changing objects which we'll do with the following code:

public void SelectNextPart(){
    _selectedPartNumber++;
    if(_selectedPartNumber >= Parts.Count){
        _selectedPartNumber = 0;
    }

    SelectedPart = Parts[_selectedPartNumber];
}

 Once you have put that into the script, save it and go back to the Unity Editor. Then drag the script into the 'scriptHolder' Game Object and then add all the objects to list like so:

You may have more objects. All you need to do is enter the amount of part prefabs you have .

You may have more objects. All you need to do is enter the amount of part prefabs you have .

 Once all that is done we need to open up the 'LE_Input' script as we need to add a way to switch objects.
 First we'll need to add another variable to the 'LE_Input' script as shown below.

private PartList _partList;

 We'll also need to add a small part to the 'void Start()' method to link to the 'PartList' script.

_partList = GetComponent<PartList>();

 Once those two little bits have been we'll need to extend the 'InputCheck()' method to support cycling through the parts, the following code can go after the check to reset the '_moved' boolean.

if(Input.GetButtonUp("Fire1")){
    _partList.SelectNextPart();
}

  Now if you save all of that, go  back into the Unity Editor, make sure 'Maximize on Play' is unchecked and then run the project. If you select the 'scriptHolder' GameObject you should see the Selected Part defaulted to Floor (Or the object you set in the first element in the list.), if you then click on the Gameplay area and tap the Left Control button, you should then see the Selected Part change.

<--  Part 1 | Part 3 -->