Join Jesse Freeman for an in-depth discussion in this video Building a custom inspector, part of Unity 5 2D: Building a Tile Map Editor.
- Now that we have our tile map and a way to create it, let's look into building a custom Editor for the tile map. Inside of our Editor folder, let's create a new script. And we're going to call this TileMapEditor. Then, let's open this up in MonoDevelop. Just like we did before, we going to need to import the UnityEditor. Now again, this script won't be attached to a game object. Instead, we're going to need to actually extend a class called Editor, which will allow us to create our own custom Inspector.
So let's replace MonoBehaviour with Editor. And let's remove the Start and Update methods. Now, in order for us to link the TileMapEditor to the tile map, we're going to need to create a meta-tag above it that will associate it with a custom Editor. Let's do open brackets, CustomEditor, and we're going to need to get the type of the class that we want to use. So in this case, we'll use typeof. And we're going to pass it in to TileMap.
And then we can close the bracket. Now, whenever we select any tile map in our scene, we're going to get this custom Editor in the Inspector. In order to make sure this works, let's add a quick method just to display a label inside of the Inspector. Since we're extending the Editor, we're going to need to override a method called OnInspectorGUI. And then from here, we're going to use a special class to create a label field This class is called the EditorGUILayout.
It has a static method that allows us to create a LabelField. And now we'll pass in a message for it to display. Let's save our script and go back into Unity. If we select the Tile Map, you'll now see in the Inspector it says Our custom editor. At this point, we now have the basic foundation for a custom Inspector and we'll continue to add to this Editor as well as our tile map class to flush this out.
- Adding custom dropdown menus to Unity
- Building a custom inspector
- Creating a texture picker
- Drawing the tile map grid
- Creating a tile picker
- Scaling a Texture2D
- Creating a tile brush
- Moving the tile brush
- Erasing and clearing tiles