Unity Script Template – Keyword Replacer

As I’ve mentioned in a previous post, it’s possible to change the default script templates in Unity. Unity’s default script “importer” also contains some (as far as I know, 3) keywords you can use which are #SCRIPTNAME# #SCRIPTNAME_LOWER# and #NAME#. They are very useful as you can see in the previous post, I’m using them to create a default singleton for example. But are they enough?

In my case, I wanted to add some information to the first couple of lines of each source file, as it’s a useful tradition.

In the above example, custom keywords like #CREATIONDATE# are replaced, also date and time is taken from the system date and time when the script is created. Pretty sweet huh?

Here is how it works;

  1. We have a custom default script template which uses any custom keywords we want
  2. We have a script, it triggers a method when a new script is created. It replaces all the keywords in our newly created script with the values we want

So how do we implement it? Create a script, I have named mine “ScriptKeywordProcessor”, you can name it whatever you like, then put it under an “Editor” folder in your Unity project. Below is the contents of this script, please take a look at the verbose comments to figure out how it works.

Okay, we have created this script and put it under Editor, now what? As you can see it contains some custom keywords such as #CREATIONDATE#, Unity does not support them by default so now we have to edit our new script template! You can head over to my post about changing the default script template for a detailed explanation. If you are familiar with the process, I’ll just include the content of the script template file here.


Special thanks to “hpjohn” for his little script here.

2 Comments Unity Script Template – Keyword Replacer

  1. Pingback: My Unity C# Script Template | Sarper Soher

Leave a Reply

Your email address will not be published. Required fields are marked *