Using the Handles class
In this section, we are going to extend the capabilities to the Edit mode, allowing the user to also reallocate the position of the pieces in the grid. To achieve this, we will use the class Handles
.
In Unity, a handle is a 3D control you use to manipulate items in the Scene View. The Handles
class allows you to use several built-in handle GUIs, such as the tools to position, scale, and rotate an object via the Transform component. Let's take a look at the following screenshot:
The Handles
class is also used to add the GUI to the Scene View. We did that at the beginning of the chapter using the methods BeginGUI
and EndGUI
.
Let's make the necessary changes to make this work. Let's add this code snippet defining two variables to the LevelInspector
class to save the original position of the piece:
private int _originalPosX; private int _originalPosY;
Then we will create a new method called Move
:
private void Move() { Vector3 gridPoint = _myTarget.WorldToGridCoordinates(_itemInspected...