Script Template Settings (v1.9.1) Improve Unity template by adding your own keyword


Script Template Settings allows you to add your own script template keywords and use them for the default templates Unity or your script templates.

With this extension, your can add namespace, author, date, (…) to your new script automatically on creation.

The Keywords values can be serialized per project or globally for all your project, they can also be computed (Date, ScriptNumber, …).
You can create basic keywords (EditorPrefs keyword, Serialized keyword, Date keyword) or write your keywords to make your own way to compute value.

With some enabled options :
Namespace will be updated automatically when script is moved in another folder.
Type name will be updated automatically when a script file is renamed

Unity keys:
#NAME#
#SCRIPTNAME#
#SCRIPTNAME_LOWER#
#NOTRIM#

Basic added keys:
#DATE# (computed value)
#DEVELOPER_NAME# (personnal value. That value is not a project value, it is saved for each user)
#NAMESPACE# (from editor settings (root namespace))
#RELATIVE_NAMESPACE# (computed value using namespace concatened with the folders where the script is placed)
#PRODUCT# (from player settings (Product Name))

Online documentation

Asset store link

That is free, it’s time to work properly! :slight_smile:
You can share your template or your extended keyword in this topic.
You can also give suggestion to improve that plugin or ask any question.

Gallery:

could you please make it so that there is a simple setting replacing the content of the created file with another file at a configurable path?The problem is that if you have many projects you always need to search for the right menu item, it would be awesome if it could just stay always the same standard workflow using always the menuitem "C# script " and the tool would just replace the content with another file (think . “include”) before processing the keywords.

I’m not sure to understand.
Do you want organize Menu?
Add your own templates

Menu from file name

Composition of the menu from the file name:
82-C# Singleton Script-NewSingletonBehaviourScript.cs.txt
82: Menu Item position.
C# Singleton Script: Menu name
NewSingletonBehaviourScript.cs: Default script name

You can also make submenu:
With:
82-C# Example__Singleton-NewSingletonBehaviourScript.cs.txt

You will get this result below :

Each __ represent a submenu. You can cumulate them.

Or do you want a keyword to replace it with a content of a file?

@Karsten

@TheoSabattie exactly what you show in the spoiler is the workflow problem, the “active template” should be always where the standard Unity menu is,there shall be no extra menus no submenus blabla ,when you have 20 projects you work on you get mad, there should be a easy “switch” in your tool saying “active template is now A” and this gets used when we use the “C# Script” menu wich should be the only item for making a new script.
A bit shorter, i try… : The menu item and position should be always the same as the original Unity “C# script” named menu item, only the script template it uses should be configurable/switchable.

The golden word here is “Projectwise context” , so for Project A we always use script template 1, if we in project B we always use script template 2 and this has to be automatic, we should not need to look thru the menus like “where is the template for my project i am actually in?”

Thanks for your hard work, it really has potential.

The principle of the plugin is to add templates and template menus as many as you want.
The plugin injects project datas into template with keywords.
For me, that is more logic to add keyword to inject your proper project datas instead of change the used script for the menu item “C# Script”

But :

You would like only one Menu Item “C# Script” and an exposed parameter to set what script is used for this menu item.
Your request could exist for other added templates, (C# ScriptableObject, C# Editor, etc…).

Other users may not have those templates or have other templates.

Unfortunatelly, I didn’t find a way to add MenuItem dynamically.

For your specific request, you should add your own solution with menu item.

There is a utils class for you : Class ScriptTemplateUtils

public static class TemplatesMenu {
    [MenuItem("Assets/Create/C# Script", priority = 82)]
    private static void CreateScript () {
        ScriptTemplateUtils.CreateScript("Assets/Test.cs");
    }
}

Note : You have to remove the basic C# Script from the Unity templates folder if you want use that MenuItem

“ScriptTemplateUtils::CreateScript” is a method to create a script from a full path or a relative path from the project folder.
“ScriptTemplateUtils::CreateScriptFromTemplate” is a method to create a script from a path relative to the Unity templates folder.
Now you can add your own settings and change what happen in the CreateScript method.

You can also extend keyword class to include your file from a keyword.

Add those following scripts:

///-----------------------------------------------------------------
///   Author : Théo Sabattié                
///   Date   : 16/08/2018 17:20
///-----------------------------------------------------------------

using System;
using System.IO;
using UnityEngine;

namespace Com.Sabattie.Theo.ScriptTemplateSettings {
    [CreateAssetMenu(menuName = "ScriptTemplate/Include Keyword", order = 81)]
    public class IncludeKeyword : Keyword
    {
        [SerializeField] private string filePath;

        public override string GetValue(string scriptPath)
        {
            try {
                return File.ReadAllText(filePath);
            }
            catch (Exception e) {
                Debug.LogErrorFormat("Cannot read file at '{0}', error : {1}", filePath, e.Message);
                return string.Empty;
            }
        }
    }
}
///-----------------------------------------------------------------
///   Author : Théo Sabattié                
///   Date   : 16/08/2018 17:22
///-----------------------------------------------------------------

using System.IO;
using UnityEditor;
using UnityEngine;

namespace Com.Sabattie.Theo.ScriptTemplateSettings {
    [CustomEditor(typeof(IncludeKeyword))]
    public class IncludeKeywordEditor : KeywordEditor
    {
        private const string FILE_PATH_PROP = "filePath";
        private SerializedProperty filePathProp;

        protected override bool DrawValue { get { return false; } }

        private void OnEnable()
        {
            filePathProp = serializedObject.FindProperty(FILE_PATH_PROP);
        }

        protected override void DrawFields()
        {
            using (new EditorGUI.DisabledScope(true)) {
                EditorGUILayout.PropertyField(filePathProp);
            }

            if (GUILayout.Button("Select file")) {
                string basePath     = string.IsNullOrEmpty(filePathProp.stringValue) ? ScriptTemplateUtils.GetScriptTemplatesFolder() : Path.GetDirectoryName(filePathProp.stringValue);
                string selectedFile = EditorUtility.OpenFilePanelWithFilters("Select file to include", basePath, new string[] { "Text files", "*" });

                if (!string.IsNullOrEmpty(selectedFile))
                    filePathProp.stringValue = selectedFile;
            }

            base.DrawFields();
        }
    }
}

Then, create a ScriptTemplate/Include Keyword

select the file that you want.

And add the keyword to settings

3599253--292088--upload_2018-8-16_17-16-45.png

Now, you template can just contains #C#SCRIPT# and you can set what you want.

Note :
The order of replacement is based on “All Keywords” order.
SCRIPTNAME and NOTRIM are the first replaced keyword because Unity handles them. If you want replace them after, You have to rewrite them with Keyword class.

See UnityEditor.ProjectWindowUtil::CreateScriptAssetFromTemplate:

Here’s the updated Asset Store link, since the one in the OP doesn’t seem to work anymore/ doesn’t redirect to the new url format (for me at least): Script Template Settings | Utilities Tools | Unity Asset Store

@TheoSabattie I found the github repo where you uploaded some templates a few months ago, but would it be possible to upload the Asset Store project as well, ideally with a “package.json” file and fitting folder structure for a UPM package? This would allow importing it directly from the github url instead of having to log into the Unity Asset Store first :wink:

1 Like

Hello @Hurri041 , thank you for report, I updated the link.

This repo is for another purpose. (next update: install templates from any repository / ensure templates have been installed. And other stuff about namespace consideration)

I take note of your suggestions. Thank you! :wink:
My timeline is unfortunatly very busy, I’ll try to dig this kind of things no later than June.

1 Like