Level Designing using Editor Scripting
- santosh nalla
- May 2, 2020
- 4 min read
Go watch this video before you start reading this blog.
An editor script is any piece of code that uses methods from the UnityEditor namespace, and its principal objective is to create or modify functionalities in the Unity editor.
Editor Scripting usually make your level design faster and efficient.
In the above video, you see there are buttons, with each button having a specific functionality.
Ok lets start. Before you write down editor script for your levels, make sure you import "using UnityEditor" and inherit "EditorWindow" class.
Once you have to included that, You have to create a window where you can place all your buttons required to design level. Lets create and name that window "Level Designer".
To create a window, First we define where should that window be present. I have put in in "Window" tab which is at the top of unity editor and gave "Level Designer Window" as its label. Once you click on this, your level designer window appears.
[MenuItem("Window/Level Designer Window")]
using UnityEditor;
public class LevelDesigner : EditorWindow
{
[MenuItem("Window/Level Designer Window")]
public static void displayWindow()
{
GetWindow(typeof(LevelDesigner));
}
.
.
.displayWindow() function actually runs on clicking "level Desiner Window" button in Window tab.
This function creates window, by executing inbuilt static function "GetWindow" in the EditorWindow class which we have inherited.
Now lets start putting buttons and giving functionality to each button. To place any button on the window which you have created, use the function GUILayout.Button(string btnName) which takes a parameter button Name( it is an overloaded function which have multiple parameters).
Before we trying to displace button on the window, Lets look into onGUI() function. this onGUI() function (is like an update function in unity) which runs more than once per every frame. So to create a button we have to call the function GUILayout.Button() in onGUI().
Now where to define the functionality of the button you have created? This is really simple,
what every code you write in between the curly braces of the GUILayout.Button() function will be executed on that button click.
One more important thing to note here is, whatever you want to instantiate in editor script, create a Resources folder and drag and drop those object here in this folder.
For example, on button click "Create Arrow", I want to instantiate an arrow in the scene window. Firstly, create an arrow prefab and drop that arrow prefab on to the "Resources" folder.
public class LevelDesigner : EditorWindow
{
[MenuItem("Window/Level Designer Window")]
public static void displayWindow()
{
GetWindow(typeof(LevelDesigner));
}
private void OnGUI()
{
if (GUILayout.Button("CREATE ARROW"))
{
arrow = Instantiate(Resources.Load<GameObject>("Arrow"), new Vector3(0, 2.44f, 0), Quaternion.Euler(new Vector3(-90, 0, 0)));
}
saw how we took the gameobject from Resources folder? We used Resources.Load<GameObject>("Arrow") to get that.
The above code creates a button "CREATE ARROW" and every time you click on this button, it creates an arrow at mentioned location.
So now that you have understood how this works, i wiill include my entire code below.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
public class LevelDesigner : EditorWindow
{
[MenuItem("Window/Level Designer Window")]
public static void displayWindow()
{
GetWindow(typeof(LevelDesigner));
}
float xPos = -2.1f;
float yPos = 0;
float zPos = 0;
public GameObject tile;
GameObject arrow;
GameObject obstrucle;
int i = 0;
private void OnEnable()
{
}
private void OnGUI()
{
if (GUILayout.Button("CREATE ARROW"))
{
arrow = Instantiate(Resources.Load<GameObject>("Arrow"), new Vector3(0, 2.44f, 0), Quaternion.Euler(new Vector3(-90, 0, 0)));
}
if (GUILayout.Button("Create Tile Straight"))
{
xPos += 2.1f;
Instantiate(Resources.Load<GameObject>("Path"), new Vector3(xPos, yPos, zPos), Quaternion.Euler(new Vector3(-90, 0, 0)));
arrow.transform.position = new Vector3(xPos, 2.44f, zPos);
}
if (GUILayout.Button("Create Tile Right"))
{
zPos -= 2.1f;
Instantiate(Resources.Load<GameObject>("Path"), new Vector3(xPos, yPos, zPos), Quaternion.Euler(new Vector3(-90, 0, 0)));
arrow.transform.position = new Vector3(xPos, 2.44f, zPos);
}
if (GUILayout.Button("Create Tile Left"))
{
zPos += 2.1f;
Instantiate(Resources.Load<GameObject>("Path"), new Vector3(xPos, yPos, zPos), Quaternion.Euler(new Vector3(-90, 0, 0)));
arrow.transform.position = new Vector3(xPos, 2.44f, zPos);
}
if (GUILayout.Button("Create Tile Down"))
{
xPos -= 2.1f;
Instantiate(Resources.Load<GameObject>("Path"), new Vector3(xPos, yPos, zPos), Quaternion.Euler(new Vector3(-90, 0, 0)));
arrow.transform.position = new Vector3(xPos, 2.44f, zPos);
}
if (GUILayout.Button("Go Straight"))
{
xPos += 2.1f;
arrow.transform.position = new Vector3(xPos, 2.44f, zPos);
}
if (GUILayout.Button("Go Right"))
{
zPos -= 2.1f;
arrow.transform.position = new Vector3(xPos, 2.44f, zPos);
}
if (GUILayout.Button("Go Left"))
{
zPos += 2.1f;
arrow.transform.position = new Vector3(xPos, 2.44f, zPos);
}
if (GUILayout.Button("Go Down"))
{
xPos -= 2.1f;
arrow.transform.position = new Vector3(xPos, 2.44f, zPos);
}
if(GUILayout.Button("Obstrucle Creation"))
{
obstrucle = Instantiate(Resources.Load<GameObject>("WallModel"), new Vector3(xPos, 0, zPos), Quaternion.Euler(new Vector3(0, 0, 90)));
obstrucle.transform.position += new Vector3(0, 0, 1.1f);
i = 0;
}
if(GUILayout.Button("Rotate Obstrucle"))
{
if (i != 10)
{
if (i == 0)
{
obstrucle.transform.position += new Vector3(0, 0, -(1.1f * 2));
i++;
}
else if (i == 1)
{
obstrucle.transform.rotation = Quaternion.Euler(new Vector3(0, 90, 90));
obstrucle.transform.position += new Vector3(+1.1f, 0, +1.1f);
i++;
}
else
{
obstrucle.transform.position += new Vector3(-2.2f, 0, 0);
i = 10;
}
}
}
if(GUILayout.Button("Create Red portal1"))
{
GameObject greenPortal1 = Instantiate(Resources.Load<GameObject>("PortalsRed1"), new Vector3(xPos, 0.21f, zPos), Quaternion.Euler(new Vector3(0, 0, 0)));
greenPortal1.name = "PortalsRed1";
}
if (GUILayout.Button("Create Red portal2"))
{
GameObject greenPortal2 = Instantiate(Resources.Load<GameObject>("PortalsRed2"), new Vector3(xPos, 0.21f, zPos), Quaternion.Euler(new Vector3(0, 0, 0)));
greenPortal2.name = "PortalsRed2";
}
if (GUILayout.Button("Create Diamond"))
{
GameObject diamond = Instantiate(Resources.Load<GameObject>("Dimond"), new Vector3(xPos, 0.183f, zPos), Quaternion.Euler(new Vector3(0, 0, 0)));
}
if (GUILayout.Button("Reset x,y,z positions"))
{
xPos = -2.1f;
yPos = 0;
zPos = 0;
if(arrow != null)
arrow.transform.position = new Vector3(xPos, 2.44f, zPos);
i = 0;
}
}
}
Hope you found this useful, Thank You :)




Comments