Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Unity 2022.2 is now available as the latest Tech release.
    Dismiss Notice
  3. We are making some changes to the DOTS forums.
    Dismiss Notice
  4. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice

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

Discussion in 'Assets and Asset Store' started by TheoSabattie, May 11, 2018.

  1. TheoSabattie


    Mar 11, 2016

    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#
    ■ #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! :)
    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.


    Last edited: Jul 10, 2018
  2. Karsten


    Apr 8, 2012
    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.
  3. TheoSabattie


    Mar 11, 2016
    I'm not sure to understand.
    Do you want organize Menu?

    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:
    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?

    Last edited: Aug 14, 2018
  4. Karsten


    Apr 8, 2012
    @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.
    Last edited: Aug 15, 2018
  5. TheoSabattie


    Mar 11, 2016
    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 :

    Code (CSharp):
    1. public static class TemplatesMenu {
    2.     [MenuItem("Assets/Create/C# Script", priority = 82)]
    3.     private static void CreateScript () {
    4.         ScriptTemplateUtils.CreateScript("Assets/Test.cs");
    5.     }
    6. }
    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.
  6. TheoSabattie


    Mar 11, 2016
    You can also extend keyword class to include your file from a keyword.

    Add those following scripts:

    Code (CSharp):
    1. ///-----------------------------------------------------------------
    2. ///   Author : Théo Sabattié                
    3. ///   Date   : 16/08/2018 17:20
    4. ///-----------------------------------------------------------------
    6. using System;
    7. using System.IO;
    8. using UnityEngine;
    10. namespace Com.Sabattie.Theo.ScriptTemplateSettings {
    11.     [CreateAssetMenu(menuName = "ScriptTemplate/Include Keyword", order = 81)]
    12.     public class IncludeKeyword : Keyword
    13.     {
    14.         [SerializeField] private string filePath;
    16.         public override string GetValue(string scriptPath)
    17.         {
    18.             try {
    19.                 return File.ReadAllText(filePath);
    20.             }
    21.             catch (Exception e) {
    22.                 Debug.LogErrorFormat("Cannot read file at '{0}', error : {1}", filePath, e.Message);
    23.                 return string.Empty;
    24.             }
    25.         }
    26.     }
    27. }
    Code (CSharp):
    1. ///-----------------------------------------------------------------
    2. ///   Author : Théo Sabattié                
    3. ///   Date   : 16/08/2018 17:22
    4. ///-----------------------------------------------------------------
    6. using System.IO;
    7. using UnityEditor;
    8. using UnityEngine;
    10. namespace Com.Sabattie.Theo.ScriptTemplateSettings {
    11.     [CustomEditor(typeof(IncludeKeyword))]
    12.     public class IncludeKeywordEditor : KeywordEditor
    13.     {
    14.         private const string FILE_PATH_PROP = "filePath";
    15.         private SerializedProperty filePathProp;
    17.         protected override bool DrawValue { get { return false; } }
    19.         private void OnEnable()
    20.         {
    21.             filePathProp = serializedObject.FindProperty(FILE_PATH_PROP);
    22.         }
    24.         protected override void DrawFields()
    25.         {
    26.             using (new EditorGUI.DisabledScope(true)) {
    27.                 EditorGUILayout.PropertyField(filePathProp);
    28.             }
    30.             if (GUILayout.Button("Select file")) {
    31.                 string basePath     = string.IsNullOrEmpty(filePathProp.stringValue) ? ScriptTemplateUtils.GetScriptTemplatesFolder() : Path.GetDirectoryName(filePathProp.stringValue);
    32.                 string selectedFile = EditorUtility.OpenFilePanelWithFilters("Select file to include", basePath, new string[] { "Text files", "*" });
    34.                 if (!string.IsNullOrEmpty(selectedFile))
    35.                     filePathProp.stringValue = selectedFile;
    36.             }
    38.             base.DrawFields();
    39.         }
    40.     }
    41. }
    Then, create a ScriptTemplate/Include Keyword


    select the file that you want.


    And add the keyword to settings


    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:
    Last edited: Aug 16, 2018