I really like working with Unity but like with most engines or frameworks it requires
me to write a lot of boilerplate code.
Which most of the time ends up just slowing me down.
Well recently I discovered a tool that helps alleviate a lot of the pain of writing this
type of code.
And in this video I'm going to share that tool with you.
So you too can speed up your workflow by never having to write Unity boilerplate like Debug.Log,
RequireComponent, or SerializeField by hand
again.
Not only will I show you how to use this tool but I'll teach you how to customize it so
you can tailor to fit the needs of your own specific workflow.
Now there is one caveat that you should be aware about before we move forward.
This tool is actually a feature of the IDE Rider called Live templates.
Now I know what you might be thinking.
Rider's not free.
And for some folks might be considered a little too pricey.
So if you stick around and watch to the end of this video I'll show you how you can get
a six month extended trial license promo code.
That way you can play around with Rider for six months and decide for yourself whether
or not you think it's a worthy investment for your game development journey.
Live templates are like shortcuts for pieces of code that you find yourself writing a lot.
Think if statements, for loops, log statements.
Basically anything you find yourself writing over and over again is a prime candidate for
a live template.
And in most cases they'll even guide you through the process, step by step, using parameters
that help you fill out the fragment and then ending with the cursor in a place that makes
sense so you can keep typing and never break your flow.
Let me show you what I mean with this quick example.
So returning back to the example from the beginning of this video (a more stripped down
version), let's say we wanted to go ahead and loop through these audio clips.
Well with a live templates we can simply type in "for" and you could see that we're given
a couple of results at the top that each have this little square icon.
That icon represents a live template.
So we can see we've got a simple for loop, for each block, and even a reverse for loop.
For now we're just gonna go ahead and select the for loop by hitting tab.
And immediately an entire fragment of code is created.
And the cursor is placed on this "i" variable which we can change by simply hitting "j".
Or, I don't know, "z" or "x".
It doesn't matter.
I think I'm going to keep it as "i" for now.
If I hit tab, we can see we're taken to the next parameter in this snippet.
And so I'm going to set it to audioClips.Count.
All right now we'll hit tab one more time and the code fragment is complete.
And the live template has placed the cursor inside of the for loop where I can simply
just continue typing.
Just like that we have a for loop now.
There are a ton of predefined live templates or C# but there's also a bunch that were specifically
designed for Unity.
For instance let's say we wanted to add a serialized field.
We can just move the cursor up to where we'd like it to be and then start typing "sfield"
and we can see we have a Unity serialized field live template.
So go ahead and hit tab and the cursor is taken to our first parameter of this live
template and that's the type.
So I'm gonna make this a list of audio clips.
I'll hit tab and then I'll type in the name of the field.
I call it audioClips.
Now hit tab again and this particular live template ends with the cursor just below the
serialized field.
So now we can go ahead and just remove this.
All right.
And of course we're referencing an audio source and I want to make sure that whoever sets
up this GameObject doesn't forget to add an audio source so we can go ahead and put the
cursor above the class declaration and begin typing "req" and then we have a Unity RequiredComponent
attribute live template.
So I'll hit tab and then it types and all of that for us including the typeof so I can
go ahead and just write AudioSource.
And then hit tab and that's it.
Pretty cool right now.
Obviously in this PlayAll method that we've created here we won't be able to loop through
the clips and play them all.
In order to do that we'd have to use a Coroutine.
Well wouldn't you know it, there's actually a live template for Coroutines.
Just simply start typing "cor" and there you go!
Hit tab and we'll call this PlayAllCoroutine.
And then of course we would just make a call to StartCoroutine.
And place all of our logic in this method here.
And that's just a taste of the live templates that are available in Riter for both C# and
Unity, which by the way I've linked a full list for each in the description of this video.
There are even some live templates for some paradigms that I hadn't even really considered
before.
For instance if I use "sprop" instead of "sfield", it will actually create a serialized field
that acts as a backing property for a public type.
So if I wanted volume to be publicly accessible but set only from the editor then we could
do something like this which I think is pretty cool.
All right.
So the last thing I want to show you is how to create your own live template.
And I actually have a great example for this.
I recently created a video on using rich text in your Unity logs and that'll allow you to
do things like add color, set the font size, and set certain elements of your message to
be bold if you'd like.
Now there already is a live template for logging.
So it's basically just type in and log and we see we've got a whole bunch we can just
do a normal log, log an error,
exceptions, variables, warnings.
Um.
We just tab on this we can see basically makes a call to debug log and allows us to very
quickly add a message.
So for instance we can say that the volume equals and then add a reference to our volume.
But what I want to do is create a live template that allows me to very quickly log something
with a color.
To do that, we're gonna go ahead and expand the File menu and then select settings.
With the settings open we can very quickly find the live templates by searching for live
template.
And then when we see it pop up in the results we can go ahead and select the C# language.
And here's a list of all the available live templates for C# language and we can see here
the logs that we just leveraged in the editor.
Now if we take a look at the settings here on the left is where we place the code fragment
and each variable is wrapped with dollar signs.
And you can have as many as you'd like.
On our right are some options that define how the live template can be used.
So we can see that for this one the shortcut is log, and that's what we type in to the
editor.
The description is what displays in the intellisense menu.
These two options here are for allowing Rider to reformat the code as well as to shorten
the qualified references.
So this Unity engine that you see in the fragment will actually get imported automatically and
then we have our availability so we can see that this particular template is available
in C# 2.0 and Unity C# files.
And lastly this is only used for generation and not for surrounding code.
So why don't we give it a try.
We can make our own by selecting this option here.
It's basically the same symbol that we see in the intellisense menu, except it has a
little asterisk next to it.
So we're just click that and now we have a no name live template.
So first things first why don't we go ahead and add the code.
We obviously don't know the context of when this is gonna be used.
So we're gonna need to explicitly make sure to reference the UnityEngine and then we'll
call Debug.Log.
And then for the message we're going to reference the color tag.
And here we're going to enter our first parameter.
I'm gonna call it color.
So because I wrapped the word color there in dollar signs, that is now going to be considered
a parameter or a variable of this live template.
Then I'll close the tag and then I'm gonna add another variable that's going to represent
our message.
So I'll just call this message.
And then I can close out the color tag.
And complete the call.
And that's it.
Next we're gonna need to define the shortcut.
So I'm just gonna call this "logcolor".
And for description let's say something like "Unity Log with Color".
Very original.
We'll keep the reformat and shorten qualified references options checked and then I'm gonna
add the availability to Unity C# files just like the other log statement.
OK.
Lastly we'll keep this as generation and we don't need to edit anything else so we can
go ahead and save.
Now we can add a log statement using logcolor.
And we can see that the cursor is immediately placed over our first parameter which is the
color.
And I'll maybe we'll call this green since it's my favorite color.
And then I'm going to reference the volume.
And that's it.
Not bad.
And again this only scratches the surface of what you can do with life templates you
can create tons of custom templates and in fact if you join our discord server some of
our community members like Mateo and apieceoffruit have created their own life templates and
shared them in the community.
I can't wait to see some of the live templates that you guys come up with.
Feel free to join us on discord and share them there or share them with me on Twitter
to @infalliblecode.
Well if you stuck around this long I can only assume that you want to get your hands on
one of those promo codes.
To receive one all you need to do is sign up for my email newsletter using the link
in the description of this video.
Once you sign up you'll be taken to a welcome page that has the promo code as well as a
link to some instructions on how to redeem your copy of Rider.
Keep in mind that supplies are limited so be sure to act fast if you enjoyed this video
then please leave a like and a comment letting me know what you thought.
Thank you for watching.
And as always I'll catch you in the next video.
Special thanks to Dark Rush Photography, NZ, Thomas, Trond and WayneGlows.