Code Faster In Unity5 min read
There is a reason that we have game engines today and that reason is avoiding repetition. Imagine that you are coding a renderer for each game you create. It would be really sad. Unity gives us enough abstraction to create every kind of game and that’s great. Well, can we go one more step further from this abstraction?
Yes, we can!
We already have lots of libraries, and thousands of game development assets today. These are also great for the sake of avoiding reinventing the wheel. So, it won’t be wrong to say that a Unity game is consisted of ;
- game engine,
- external libraries and game assets,
I would like to move your attention to the “scripts” you wrote. Each game has a concept. You always repeat some set of specific actions for that game. This can be using nested loops or creating a class that extends the IMonster interface. I use it for binding Zenject implementations a lot for example. Whatever it is, we all have some specific type of coding style and usually use similar structures. I mean who doesn’t use Debug.Log on daily basis?
Almost every IDE has addressed this problem and lets users create their own code templates. Visual Studio has Snippets and Rider is using Live Template as a name. I will only talk about Snippets here since I use Visual Studio as my preferred IDE. However, you can also check this link to find out how Rider’s live templates work.
I bet that you always type ‘D’ and wait for the IDE to suggest you “Debug”, then you hit enter. You do the same action for writing “Log” or maybe you complete it since it is not that much of a typing. This is still acceptable because it is short. However, using Visual Studio Snippets is much more fun. I only press “dl” and then hit the tab button twice. Visual Studio automatically generates it for me.
The game currently I work requires lots of nested-loops. I stopped writing it over and over. Whenever, I realize that there was some repetitive piece of code, I try to make a snippet out of that. Visual Studio also gives you the ability to create parameters in snippets. So, you can specify editable parts of each snippet.
Let’s create our first Snippet
“Debug.Log(message)” will be our first snippet to learn here. Once you do this, you will be able to generate other ones you’d like anyway.
In order to create a snippet, the only thing you need is to create a .snippet file. The snippet format is actually XML, so you will feel quite familiar while creating one. Here is the one I have created;
<?xml version="1.0" encoding="utf-8"?> <CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet"> <CodeSnippet Format="1.0.0"> <Header> <Title>Debug Log</Title> <Author>erdiizgi.com</Author> <Description>Debug.Log(message)</Description> <Shortcut>dl</Shortcut> </Header> <Snippet> <Code Language="CSharp"> <![CDATA[Debug.Log($message$);]]> </Code> <Declarations> <Literal> <ID>message</ID> <ToolTip>Type the message you would like to see in the logs.</ToolTip> <Default>""</Default> </Literal> </Declarations> </Snippet> </CodeSnippet> </CodeSnippets>
Creating a snippet is as simple as above. Most of the things are optional, but I think this is the least you can provide. There two parts you should define while creating a snippet. These are Header and Snippet.
<Header> <Title>Debug Log</Title> <Author>erdiizgi.com</Author> <Description>Debug.Log(message)</Description> <Shortcut>dl</Shortcut> </Header>
The most important part of the Header is the Shortcut. Because this is how you will reach this snippet once it is defined. For example, here I will type dl, then I will hit to tab twice and, I will have the whole code piece.
<Snippet> <Code Language="CSharp"> <![CDATA[Debug.Log($message$);]]> </Code> <Declarations> <Literal> <ID>message</ID> <ToolTip>Type the message you would like to see in the logs.</ToolTip> <Default>""</Default> </Literal> </Declarations> </Snippet>
<Snippet> is where the magic is actually happening. Take a look at the <Code> tag. We specify Language=”CSharp” as a first thing here, after that, we define the code piece we would like to pass as a snippet inside the <![CDATA[…]]> block.
The good thing with this meta, we can also define parameters. Here I have only one parameter and that is $message$ (you have to put $ before and after your parameter). You also need to give some details about your parameter. Tooltip is optional, but helpful if you will share your snippets with other people or your team.
Well, that’s all you need to know to create a snippet. Once you create it, you should save it as your_snippet_name.snippet to any folder you wish.
How to use the snippet in Visual Studio?
1. Click on Tools -> Code Snippet Manager
2. Select the Language as CSharp, then click to Import button.
3. Find your snippet and click OK button
4. You will see your snippet in the window if the snippet definition is correct.
5. Now, whenever you type dl and double hit to the tab, the code will appear.
Conclusion: Using Snippets
That’s all. Creating snippets is extremely easy. There are already a bunch of default snippets provided by the Visual Studio itself. Check this out to learn about it more.
I strongly think that this is not a waste of time. I also plan to have a repository of Unity Snippets so that people can contribute. Let me know if you encounter any problem while creating your first snippet.
Comment me below about what you think on Visual Studio Snippets.