Search Unity

Best Live Trainings of 2017: Object pooling, shader writing and particle splatting

December 16, 2017 in Technology | 7 min. read

This year has been a great year for us in Online Evangelism and next year is shaping up to be even better (and busier!). Coming into the end of the year I thought it might be a good time to take a look back and pick out some of my favorite content which I created for our Live Training series, in case there were sessions that anyone missed.

Maybe you’ve got some time over the holidays to try out some new prototype ideas, or you’re looking for a solution for an existing project. My goal here is to share a selection of training material to educate and inspire you to take your game making to the next level.

Object Pooling In Flappy Bird

The first session I’d like to highlight is my session on making a Flappy Bird style game. This is a beginner friendly session for folks who have done some of the most basic Unity tutorials, like for example our 2D UFO series, and are ready to add a few concepts to their toolbox. The key areas of focus for this training were on object pooling, creating the illusion of movement by moving the world instead of the player, and creating ‘cartoon’ or unrealistic 2D physics.

I think the object pool concept is probably the one with the broadest possible application to many projects. Object pooling, for those who are unfamiliar, is basically recycling for GameObjects. It becomes especially important and useful in games that feature many individual objects, which are created and destroyed over time. The purpose of an object pool is to make this process performant with regard to memory usage, and specifically, garbage collection. Basically, every time you instantiate or destroy an object you create work for the garbage collection system, called an allocation. After a certain number of allocations have occurred, the garbage collector has to run. When it does, you can see a momentary and significant drop in your game’s framerate, which feels like a glitch or short freeze. This is obviously a short, simple explanation of this concept. For some great further reading on the topic of performance, I highly recommend my colleague Kerry Turner’s excellent articles on the subject, including one specifically on garbage collection, in the Performance Optimization section of the Unity Learn site.

Clearly we don’t want this to happen during gameplay, and so a way to avoid this is to not instantiate or destroy objects while the game is running. Instead, we spawn a pool of objects which are initially out of view or inactive. Then, when we need an object, for example a bullet or explosion in a shooter game, we request one from the pool. When we’re done with that object, we turn it back off and move it out of view, ready to be used again. In the linked training, we use this technique for the endless series of columns that the player flaps past.

This content is hosted by a third party provider that does not allow video views without acceptance of Targeting Cookies. Please set your cookie preferences for Targeting Cookies to yes if you wish to view videos from these providers.

While we’re briefly talking about performance optimization, you also might enjoy checking out a talk on the subject of Performance Optimization For Beginners that Kerry and I gave at Unite Amsterdam 2017.

A First Look At Shaders

One frequent, ongoing request we had from our live training community, those who join us live on Twitch and those who watch our content on YouTube, was for some training around writing your own shaders in Unity. This was a slightly scary topic for me as a trainer, since before doing the session it wasn’t something I had done before. However! The audience had spoken, and so I hit the books in an attempt to learn enough to talk for an hour without sounding like a fool or saying something terribly, terribly wrong. I stood on the shoulders of giants, and had some great research material to work from, with extra special thanks going out to the creators of the material linked in this twitter thread.

You may have heard the term shader bandied around, but basically, a shader is a program that is used to determine how things are rendered in computer graphics. Shaders are amazing and fun, and in the end were not as hard as I feared they would be to learn. The super cool thing about shaders is that they allow you to achieve graphical effects that you really can’t achieve any other way. By learning to write your own shaders, you can begin to create truly unique and novel visual effects for your games, and make them stand out from the crowd.

In this training I walk through writing your first shader, hence the title Writing Your First Shader In Unity. I assume no prior in-depth knowledge of 3D rendering or shader coding and attempt to walk you through the process in an approachable way.

This content is hosted by a third party provider that does not allow video views without acceptance of Targeting Cookies. Please set your cookie preferences for Targeting Cookies to yes if you wish to view videos from these providers.

Particle Collisions and Decal Rendering

The third training that I’d like to share with you is a bit more on the advanced side. That being said, if you know some C# scripting fundamentals, the training takes you step by step through the process, so don’t be put off. The title of the training is Controlling Particles Via Script. Frankly, this is a terrible, deeply boring title, which I think makes the session sound less cool than it is, but it’s what I could come up with at the time . ¯\_(ツ)_/¯

After going through the training, you’ll be able to create a particle based decal system that allows you to fire particles into the scene and use them to ‘paint’ surfaces in your level in a performant way. For all you optimization nerds, you can paint thousands of decals all over the scene, and because it’s a particle system, they all get drawn in a single SetPass call or draw call. Yes, this is the kind of stuff I get excited about. Yes, I am a giant nerd.

Most people think of particles in a fairly limited sense, as being useful for creating little animated sparkles and juice effects. The fact is that Unity’s particle system has tons of cool functionality in it, including the ability to detect collisions, return data based on those collisions, and dynamically render thousands of images in whatever configuration you define via code in a super performant way. In this training I show you how to create a gun that shoots particle bullets, and then execute code based on what was hit. This is a technique I was inspired to check out after hearing that top-down shooter game Assault Android Cactus (which has TONS of bullets on screen at a time) used Unity’s particle system for their core shooting system. My initial goal was to just shoot a particle at something, and then run some code, but once I got going I figured out that not only could I use particles in this way, but that I could use a second particle system to display the resulting splat decals, controlling everything via C# script. I had a ton of fun programming this one, and also splatting paint all over the place is super fun. I think someone made a game about that... Splat-something.

This content is hosted by a third party provider that does not allow video views without acceptance of Targeting Cookies. Please set your cookie preferences for Targeting Cookies to yes if you wish to view videos from these providers.

I hope that you’ve learned something and found something useful for your own projects in this end of year round-up. If you’ve never been to one of our live sessions, I’d love to see you there! You get to learn awesome content like this AND ask questions in real-time via Twitch chat. I also usually make some hilarious, dumb mistakes, and we all get a lot of laughs at my expense. We’re live on Twitch every two weeks, and you can check out the schedule here.

What is a cool technique that you learned in 2017? Let me know in the comments!

December 16, 2017 in Technology | 7 min. read