Search Unity

Make Script Templates global for all Unity versions

Discussion in 'Editor & General Support' started by mostlyhuman, Feb 14, 2020.

  1. mostlyhuman

    mostlyhuman

    Joined:
    Mar 16, 2013
    Posts:
    53
    For those that customize their script templates, every time a new Unity version comes out we have to go back into the Script Template folder for that version and address our changes.

    I am surprised this wasn't already made global for all Unity versions the way some of the user settings now carry over between Unity versions.

    If there are some script templates that could cause things to break if they are global then consider dividing them up into ones that can be global and ones that cannot. At the very least making 81-C# Script-NewBehaviourScript.cs.txt global would be very helpful.

    Thank you for the consideration.
     
  2. Ardenian

    Ardenian

    Joined:
    Dec 7, 2016
    Posts:
    313
    If you add your templates into a folder of the installation of Unity, there are better solutions nowadays. Take a look on How to create your own C# Script Template? for instance. It centers around the (undocumented?)
    UnityEditor.ProjectWindowUtil.CreateScriptAssetFromTemplateFile
    and in the linked thread there is an example on how to set them up.

    Here are two of the templates that I created that way.
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. [CreateAssetMenu(menuName = "MyGame/Data/#SCRIPTNAME#", fileName = "New #SCRIPTNAME#", order = 0)]
    4. public class #SCRIPTNAME# : ScriptableObject
    5. {
    6.  
    7. }
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3.  
    4. [CustomEditor(typeof(YourType))]
    5. //[CanEditMultipleObjects]
    6. public class #SCRIPTNAME# : Editor
    7. {
    8.     //SerializedProperty value;
    9.  
    10.     public void OnEnable()
    11.     {
    12.         //value = serializedObject.FindProperty("Value");
    13.     }
    14.  
    15.     public override void OnInspectorGUI()
    16.     {
    17.         serializedObject.Update();
    18.  
    19.         // EditorGUI content
    20.  
    21.         serializedObject.ApplyModifiedProperties();
    22.     }
    23. }
    Code (CSharp):
    1. using UnityEditor;
    2.  
    3. public static class ScriptTemplateUtility
    4. {
    5.     private static readonly string path = "Assets/Editor/ScriptTemplates";
    6.  
    7.     [MenuItem(itemName: "Assets/Create/Templates/ScriptableObject", isValidateFunction: false, priority: 51)]
    8.     public static void CreateScriptableObjectFromTemplate()
    9.     {
    10.         ProjectWindowUtil.CreateScriptAssetFromTemplateFile($"{path}/ScriptableObject.cs.txt", "NewScriptableObject.cs");
    11.     }
    12.  
    13.     [MenuItem(itemName: "Assets/Create/Templates/Editor", isValidateFunction: false, priority: 52)]
    14.     public static void CreateEditorFromTemplate()
    15.     {
    16.         ProjectWindowUtil.CreateScriptAssetFromTemplateFile($"{path}/Editor.cs.txt", "NewEditor.cs");
    17.     }
    18. }
     
    kaiyum likes this.