Everything about editor scripting in Unity with C# code


Unity's editor is powerful but limited, so sometimes you need to improve the editor to fit your own game. This is a tutorial on how to do that! In each tutorial you will make at least two scripts: a so-called Editor script which is similar to the regular C# scripts and that communicates with the C# script. The difference is that the Editor script has to be placed in a folder called Editor. You can have multiple Editor folders in different sub-folders to make it easier to organize your project.

The Editor scripts can be divided into two sections and you don't need both when making an Editor script. The first section is called OnInspectorGUI() and this is where you add code to add stuff like buttons to a script. These buttons can then be found in the Editor below the public variables that belongs to the script. The second section is called OnSceneGUI() and is similar to the Update() method but is updating the Editor so you don't have to click play for it to run.

This tutorial is divided into the following sections:

  • 1. Add prefabs within circle. If you for example want to make a forest, you need to add trees within a circle. This tool is similar to Unity's own tree brush tool but you can use it with any prefab.
  • 2. Add prefabs along a line. If you for example want to make a fence, you need to add fence pieces between the start and the end of the fence.