A simple tool to add and manage more script templates.
This tool adds custom Script Templates to Unity that will serve as alternatives for the default "C#Script" that I personally always modify before writing my scripts. In essence, it allows customization of the script templates with dynamic data such as signature and namespace settings. Additionally, it adds templates for other types of scripts rather than just Mono Behaviours. You can customize the templates after they have been copied as well.
This package is available on the OpenUPM registry. Add the package via the openupm-cli:
openupm add com.mygamedevtools.script-template
Installing from Git (requires Git installed and added to the PATH)
- Open
Edit/Project Settings/Package Manager
. - Click +.
- Select
Add package from git URL...
. - Paste
com.mygamedevtools.script-template
into the name. - Click
Add
.
From Assets/Script Templates Editor
you can open the Script Templates Editor Window.
Here you can preview in realtime what your script will look like, customize the values, and copy the templates.
You can decide to enable both the Signature and the Namespace modules, each with its own set of custom settings.
The Signature module adds a simple signature header on your script following the template:
/**
* #SCRIPTNAME#.cs
* Created by: #AUTHOR#
* Created on: #CREATIONDATE#
*/
The #SCRIPTNAME#
is no stranger to custom script templates, as it is replaced by the default Unity asset processor.
However, both the #AUTHOR#
and #CREATIONDATE#
will be replaced by the values you set on the Author
, Email(optional)
, and Use Local Date
fields under the Namespace module group.
The Namespace module adds a namespace to your file, indenting the template content under it. For that to work, you need to have a template such as:
#NAMESPACE#public class #SCRIPTNAME# : MonoBehaviour
{
# #public void Start()
# #{
# ## #
# #}
}
Then, if enabled, the namespace will be replaced according to the values you set.
If you enable Use Assembly Definition
, make sure the script is in the correct location to be linked to an assembly definition, or the Default Namespace
will be applied.
Otherwise, if you decide to just use the Default Namespace
, then it will be applied to all scripts.
The indentation whitespaces must be marked with the # #
keyword, one for each indent level, so they can be converted to tabs or spaces and by how much. So, using the previous example:
#NAMESPACE#public class #SCRIPTNAME# : MonoBehaviour
{
# #public void Start()
# #{
# ## #
# #}
}
By selecting 4 spaces as the indentation settings, the final created script would be:
namespace MyNamespace
{
public class MyClass : MonoBehaviour
{
public void Start()
{
}
}
}
Finally, you should Save
your changes once you're satisfied with your settings, to make sure they will be used whenever creating a new script.
You also have the option to Clear
those settings at any time, if you wish.
This data is saved to the Editor Prefs cache.
This section shows a list of all included Script Templates and you can select them to locate the file.
You also have two buttons: Copy to Project
and Copy to Editor
.
The first one will copy all script templates to the current project Assets/ScriptTemplates
path, limiting the script template usage to that project.
The other will copy all script templates to your Editor folder, and you will be able to use the templates
in all projects.
However, if you don't have this package in a project, it will not replace the custom keywords on the file and might lead to compilation issues.
Also, if you update the Unity Editor version, you will lose the script templates and be required to copy them again. This is not necessary if you copy them to your project.
Both actions of copying to the project and to the editor will require an editor restart to take effect.
- Game Objects
- Editor
- UI Toolkit
- Entities
- System
- Component
- Authoring (Baker Workflow)
- Managed Component
- Managed System
- Plain C#
- Class
- Struct
- Enum
- Interface
The custom Script Templates have custom keywords that will be replaced by the data you entered on the Editor Window and the present date. The keyword recognition and replacing is done by the ScriptKeywordReplacer class.
You can customize your Script Templates to suit your needs, for example, adding default methods or even new keywords to process. Just make sure you add them to the correct folder and add the custom keywords following the same rules as the built-in templates. Keep in mind that if you ever want a feature that it does not have, I may want to include it in this package.
For me, it's more practical to create scripts from premade templates for different usage contexts than starting with the default "CSharp Script" Unity provides. With this package, you can have multiple templates to easily bootstrap your coding.
Don't hesitate to create issues for suggestions and bugs. Have fun!