Unity Custom Editor to Set up Components

About a month ago I finished up the GameDevHQ intensive training program. The intensive training program was an 8 week program and deep dive into C# and Unity. In the last week of the program, we were given some coding challenges that you might run into when applying for jobs with Unity. I was given a 48 hour challenge to make a sort of car showroom type thing with some specific functionality requirements:

⦁ Enable each part of the car to be selected, which will move the camera to view the part close up and label the part

⦁ Labels will only show for the selected part

⦁ A part can be selected by either clicking on the part or by clicking on the UI button on the left side of the screen that corresponds to the part

⦁ Each part on the car should be listed on the left with its own button

⦁ Parts should highlight somehow when hovered over to show they are clickable

Beyond those requirements, it stated: “This should be setup to be reusable. It should be written as if you will need to use this application to do the same thing for 20 different cars with different parts. The less work required for every other car the better.” I took this part to heart, and really tried to make the program as reusable as possible, and wanted it to work with many different models.

I started work on the project and quickly decided to use Cinemachine for the cameras. It was my first time using Cinemachine on its own without Timeline and it is a great tool for cameras and camera switching and direction in Unity.

I also decided pretty early that I would need a component for each car to keep track of its parts, and then a component for each part itself to be able to handle mouseovers, mouse clicks, parts changing color, and labeling.

After I had the basic functionality set up, I started looking for a way to more easily set up each car model. Each one needed a few things, that in order to do manually would take some time. The parent model needed an Object component on it, and all the children needed part components, mesh colliders, and Cinemachine Virtual cameras as children of the parts.

In order to set this all up automatically, I started working on a Unity custom editor window. I eventually got this set up so that I could import a model or multiple models, make them a child of the BulkCreatorObject game object in my scene, and then just press a single button in the custom editor window and the model would process and be ready to use in the program. After running the custom editor, you can just hit play and the new car model is already added to the list and working in the program.

What the custom editor window looks like. The set up new object button was made obsolete by the bulk set up new objects button.

What the custom editor is doing behind the scenes:

  1. It first unpacks the model prefab so that we can make changes to the child objects.
  2. Then it checks if there is an object script on the parent object, and if so, the object component is removed.
  3. It then adds or re-adds the object script to the parent object.
  4. Then it loops through the child objects of the model (parts) and cleans up un-needed objects like separate LODs, and if Cinemachine cameras are already present, we delete them.
  5. After that, it loops through each part again and if there is a part component or a mesh collider already on the part object, it deletes them and then re-adds them. If there is no renderer on the child object, it is deleted.
  6. Also in here, the code looks for skinned mesh renderers, and if found, converts them to regular mesh renderers.
  7. It then sets the name of the part in the part script.
  8. And then it finally adds or re-adds the Cinemachine cameras to each part.
Some of the editor code to first set up the parent object

All of this together ends up setting up the models for the program automatically, and since we are checking for the components that we add in the process and deleting them if they are present, this process can be run multiple times on the same object and it will just re-run the process without errors or duplicating components or objects.

Some of the editor code involved with setting up or resetting each part.
Because the UI was shrunk for this gif, not all parts are displaying, but at normal size they do.

Above is an example of another car that I brought in that was processed by my custom editor process. It required no setup from me other than dropping the model under the BulkCreatorObject game object in the scene and clicking the button on the custom editor.

Below is a video where I take a look at the car showroom program and shows more about how it works.

As a final project for the GameDevHQ Intensive program, this was a lot of fun! I think I managed to go above and beyond the requirements for this programming challenge and learned a lot about custom editors and Cinemachine in the process!

Since the Intensive program ended I have participated in a couple of game jams! My game BEESWAX won the GameDevHQ Community game jam #3, and jam results are still pending for IndiCade Climate Jam, where I worked with a great team to make Planit! Check out some gameplay below!

Leave a Comment