The Art of Minimalism

How to work efficiently in Unity3D and not repeat yourself

Everyone at least once in one’s life happened to do one simple task over and over again to reach desired goal. Even though the whole thing was incredibly boring, it still had to be done. Now imagine that you could replicate any part of this process and use it to create as many equal outcomes as there are needed. Sounds great huh? No more peeling hundreds of thousands of potatoes to make some fries! No more folding 1000 origami cranes to make one wish come true! And… no more setting every single game asset on your own!

Did you read Patryk’s last blog-post about creating “Sticker Man” Customization Popup? If you didn’t, you may want to read it first, because we are going to continue that topic.

The Power of Prefabs

First of all, let me tell you quickly what prefabs actually are and why are they so useful. Basically, they are kind of pattern for objects that you need to use frequently and you expect them to look or behave this same exact way. By these objects I mean specific types of enemies, parts of environment, UI elements etc. For example on the right side of our popup you can see a group of cells, which represents mouths that you can put on your ‘Sticker Man’. Obviously, I didn’t create them manually, one by one. Would you like to know how does this magic work? Do not hesitate to read further!

Prefabs Construction Pipeline

To be able to put our feet up and make good use of the time that prefabs may spare for us, we have to do some work first. Begin with creating a game object, which is going to represent our cell and add all necessary components to it (if you don’t know how to do it, you can always read Patryk’s last blog-post).

At this point your cell should look like the one above and have the same components. Now you can use drag and drop to put it from Hierarchy into Project Tab. Why there? Well… from the moment you put your game object into Project Tab it becomes a resource, which can be used as template for game objects you instantiate in your game. Guess how do we call these templates…yes! They are prefabs!

When you select MouthCell in Project Tab, you may notice that right under prefab name in Inspector Tab three new buttons appeared. From now on every single change in your prefab is going to affect objects that were created with use of this template. Isn’t that convenient? Just hit button ‘Apply’ to save all changes or if you make a little mess and you can’t reverse changes manually press ‘Revert’ button to bring back your old settings.

The Last Part of Puzzle

To be completely satisfied with our work (or taking shortcuts), we have to make Unity do one last thing for us. Our cell prefab is ready and we can make as many exact copies of it as we want. But also we want all these cells to lay out somehow. Obviously, since we have more important things to do and we value our time, we will lay out them automatically. Select ‘MouthCellsSection’ in Hierarchy Tab and add ‘Grid Layout Group’ Component to it. You can play with its parameters to achieve your own vision, but if you want to make your cells look just like in my example, fell free to copy-paste values.

Time To Reap The Harvest

Since we worked so hard, it’s time for reward! Drag ‘MouthCell’ prefab and drop it inside ‘MouthCellsSection’. Select ‘MouthCell’ in Hierarchy Tab and copy it a few times. It feels good huh? With such a small effort we created whole front-end side of mouths section and from now on every single change is going to be a piece of cake! If you want to look through the whole project, you can always fork it on our GitHub.

What’s Next?

In the next part we are going to explain how to load data for specific mouths into cells, add some logic to our popup and why Model-View-ViewModel (MVVM) and UniRx are such a good couple when it comes to this matter.

Don’t forget to tap and hold 👏 and to follow DaftMobile Blog (just press “Follow” button below)! You can also find us on Facebook and Twitter.