In this post we will learn how to extend the Scene View, probably the most important Editor window in Unity! Things we do in this post:
- Show a gizmo for an invisible object from Editor code.
- Create our own custom GUI in the scene view
- Create a simple move tool to move our target objects around our game arena but make sure they are within the arena limits.
This article is part of a series in which I explain all the techniques for extending the Unity Editor. See the overview post here.
Prerequisite: You need to know C# programming and be familiar with Unity Editor to benefit most from this post.
There is an accompanying Unity project. I highly recommend downloading it and trying the topics as you read them here. Seeing the examples in action makes them stick to your brain.
This is the acompanying repository for my series on Unity Editor Scripting. You can find the articles down below. In…
For instructions on how to get started with the project, see the overview post.
Without further ado, let’s jump in!
Gizmos are little icons that are shown in the SceneView for objects that don’t have a mesh. Camera, AudioSource, Text are sample objects that use Gizmos. Gizmos help us select these invisible objects from the SceneView rather than finding them in the hierarchy.
In our game, the Manager object is invisible but contains quite a bit of logic in it. Let’s add a Gizmo to it!
We can add a gizmo for a MonoBehaviour by adding the
OnDrawGizmos method to the class. Let’s add the following to GameManager.cs.
Gizmosnamespace contains helper methods for drawing different gizmos. In our case we want to draw a custom icon where our Manager object is.
- The icon (“managerIcon.png” in this case), should exist under “Assets/Gizmos” path.
- The last parameter tells Unity whether the icon should have a fixed size or scale with Camera movement. Unity’s internal components like Camera and Light use
trueby default, so we do the same here.
Now we can select the Manager game object from the scene view by clicking the gizmo.
Custom Scene View GUI
Next, let’s create a simple GUI in the Scene View to change the size of our arena. This could make it easy for our developers to set the arena size to standard sizes we want in our game.
To create GUI in the Scene View, we have to create a
OnSceneGUI method in our Editor script. So let’s add the following method to GameManagerEditor.cs.
- In lines 3 to 6 we get the current
SceneViewinstance and compute a
Rectto show our custom GUI in.
Handlesnamespace contains helpers for adding controls to the Scene View.
Handles.BeginGUI()creates a container for IMGUI. Every IMGUI call between
Handles.EndGUI()will show in our Scene View.
- Lines 7 to 19 are IMGUI calls to show 3 buttons that will set the size of the Arena object in our Scene and adjust the camera’s position to contain the arena.
A. Similar to the OnInspectorGUI() method, OnSceneGUI() method gets called only when the Inspector for the object is open.
B. Our GUI for adjusting the size of the Arena.
Move Handle for Arena Object
A Handle in Unity terminology is an interactive 3D object in the Scene View. The scale and move controls are example handles. That’s the reason for the name of
Handles namespace in the previous section to create a container for our IMGUI calls. We can use its helpers to draw 3D objects and even 3D handles like the move and scale handles. It is possible to create custom handles beyond the ones already defined by Unity. If I get time I’ll make a post on that. In this post, let’s use the move handle from Unity.
Add this line to the end of
OnSceneGUI method of GameManagerEditor.cs.
Note that while the Manager object is selected, a second move handle is shown for the Arena object. You can move either object using its corresponding handle.
In this post we learnt about multiple ways to extend the SceneView’s functionality:
- Show a gizmo from our Editor code.
- Create custom GUI in the SceneView.
Handlesfor modifying objects in the SceneView.
If you have any questions and suggestions about this post, feel free to leave a comment.
If you find this tutorial useful, please support me on Patreon. It takes a good amount of time to write these tutorials and your support will keep me going. Thank you!
This is the last post in this series. You made it! There is a lot more to learn about Unity Editor Scripting. This series lays the foundation on how to do it. Here are a few topics to read on if they apply to you: