top of page

005-Buttons Assignment

1.1 Assignment Description

In this assignment perform the following tasks

1. Build an Editor window at menu location-"MyExercises/Exercise001"

2. Add Buttons that perform the following task

- Clamp all selected objects to rounded positions i.e. if object is at position-

(1.3,2.2,6.7)-It's position should change to (1,2,7)

- Button to delete all the selected gameObjects in the sceneView.

- Button to add rigidbody to selected gameObjects

- Button to remove rigidbody from selected gameObjects

- Button to move all the selected GameObjects by 1 Unit upwards

3. Project files for the assignment are in folder-005. All the assignments and solutions are uploaded on the patreon.

​

1.2 Assignment Solution

1. Open the 005 Folder

2. Let us create a basic editor window called MyExerciseEditor001 with menu location set to "MyExercises/Exercise001"

image.png
1.3 Adding buttons

1. Let us now add the buttons for all the 5 functionalities and then open the editor window.

​

image.png
image.png
1.4 Assignment Part-a Clamping Functionality

1. Let us make a function called as ClampAllSelectedToAxis()

2. In this function we will get all the gameObjects from the Selection class iterate over them and then round their position to the axis

image.png

3. In the clamper function let us round the position along each axes and then assign its position to the current transform.

image.png

Now let us test the clamper button.

image.png
image.png

Before clamping

After clamping

1.5 Assignment-1 Part b-Button to delete all the selected GameObjects in the sceneView

1. Let us add functionality to DeleteSelectedGameObjects button. To do this let us add a function called as DeleteSelectedGameObjects()

2. It iterates over the selection of the gameObjects and deletes them one by one.

image.png
image.png

Instead of using Destroy method we are using DestroyImmediate method becuase this method works in the editor view when the game is  not playing. Destroy() method doesn't work in the editor view.

image.png
image.png

Before deleting

After deleting

1.5 Assignment-1 Part c-Button to add Rigidbody to selected gameObjects

1. Let us adding rigidbody functionality to AddRigidbody button.

2. Iterate over all the gameObjects and rigidbody to each of them when the button is pressed.

image.png
image.png
image.png

Before adding rigidbody

image.png

After adding rigidbody

1.6 Assignment-1 Part-d Removing Rigidbody from selected gameObjects

1. let us add a function called RemoveRigidbodyFromSelectedObjects().

2. In this function we are going to iterate over all the gameObjects and get their rigidbody components.
3. Then we are going to call DestroyImmediate method to destroy the components as we are in the editor mode.

image.png
image.png
image.png

Before

image.png

After

1.6 Assignment-1 Part-e Moving selected objects by 1 unit upward

1. Let us write a function called as MoveUpwardBy1Unity().In this function we are going to iterate over all the selected gameObjects and move them up by 1 unit.

image.png
image.png
image.png
image.png

Before

After pressing move up button 3 times

End

Kindly support me through my Patreon account if you find these tutorials useful. Feel free to connect with me on Twitter. This support enables me to create more educational content.

bottom of page