Unityxyz
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"
1.3 Adding buttons
1. Let us now add the buttons for all the 5 functionalities and then open the editor window.
​
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
3. In the clamper function let us round the position along each axes and then assign its position to the current transform.
Now let us test the clamper button.
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.
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.
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.
Before adding rigidbody
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.
Before
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.
Before
After pressing move up button 3 times