Search Unity

  1. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Let us know a bit about your interests, and if you'd like to become more directly involved. Take our survey!
    Dismiss Notice
  4. Don't miss out on the Unite LA Keynote for the latest information from Unity! Set a reminder!
    Dismiss Notice
  5. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  6. Want to see the most recent patch releases? Take a peek at the patch release page.
    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

    TheoSabattie

    Joined:
    Mar 11, 2016
    Posts:
    13


    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! :)
    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:









     
    Last edited: Jul 10, 2018
  2. Karsten

    Karsten

    Joined:
    Apr 8, 2012
    Posts:
    91
    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

    TheoSabattie

    Joined:
    Mar 11, 2016
    Posts:
    13
    I'm not sure to understand.
    Do you want organize Menu?
    -> http://theo.sabattie.com/projects/script-template-settings/documentation/add-your-own-template.html

    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
     
    Last edited: Aug 14, 2018
  4. Karsten

    Karsten

    Joined:
    Apr 8, 2012
    Posts:
    91
    @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

    TheoSabattie

    Joined:
    Mar 11, 2016
    Posts:
    13
    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 : http://theo.sabattie.com/projects/s...riptTemplateSettings.ScriptTemplateUtils.html

    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

    TheoSabattie

    Joined:
    Mar 11, 2016
    Posts:
    13
    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. ///-----------------------------------------------------------------
    5.  
    6. using System;
    7. using System.IO;
    8. using UnityEngine;
    9.  
    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;
    15.  
    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. ///-----------------------------------------------------------------
    5.  
    6. using System.IO;
    7. using UnityEditor;
    8. using UnityEngine;
    9.  
    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;
    16.  
    17.         protected override bool DrawValue { get { return false; } }
    18.  
    19.         private void OnEnable()
    20.         {
    21.             filePathProp = serializedObject.FindProperty(FILE_PATH_PROP);
    22.         }
    23.  
    24.         protected override void DrawFields()
    25.         {
    26.             using (new EditorGUI.DisabledScope(true)) {
    27.                 EditorGUILayout.PropertyField(filePathProp);
    28.             }
    29.  
    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", "*" });
    33.  
    34.                 if (!string.IsNullOrEmpty(selectedFile))
    35.                     filePathProp.stringValue = selectedFile;
    36.             }
    37.  
    38.             base.DrawFields();
    39.         }
    40.     }
    41. }
    Then, create a ScriptTemplate/Include Keyword

    upload_2018-8-16_17-14-24.png

    select the file that you want.

    upload_2018-8-16_17-15-30.png

    And add the keyword to settings

    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:
    https://github.com/Unity-Technologi...ditor/Mono/ProjectWindow/ProjectWindowUtil.cs
     
    Last edited: Aug 16, 2018