Editor Feature Suggestion

It may not be all that important, but I think that the Create C# Script menu option is too limited, sometimes I at least wish that I could create scripts of a specific type (ie. Interfaces, enums, editor scripts etc.) directly from the editor as opposed to setting up the base framework of the script after creating it. I’ve personally created a fairly large number (15) of script templates, for everything I could reasonably think of. I’ll include the files themselves, to both show what I came up with but also to allow people to add their own opinion on what should or shouldn’t be a part of the suggestion. I’ve also included a script template for creating HLSL files. This would rename/replace the script template for the default C# Script menu option.

7684918–961063–ScriptTemplates.zip (5.17 KB)

2 Likes

They should just make creating script templates less of a pain in the ass honestly. Also maybe clean up the context menu a little because that shit is ten miles long.

I might be misunderstanding, but you can do that already if you mess around with the ScriptTemplates folder:

7685227--960910--upload_2021-11-24_18-17-2.png

I renamed the NewBehaviorScript to 81-C# Script__Default Template Script-NewBehaviourScript.cs and my two custom ones are 203-C# Script__Custom Templates Enum-MyCustomEnumTemplate.cs and 204-C# Script__Custom Templates Interface-MyCustomInterfaceTemplate.cs
Source: here (further comments explain the naming structure)

These do get nuked every time you update though if I remember correctly, or rather, they need to be copied to the new editor version’s script templates and if you don’t nest them you’ll only make the clutter mentioned by @Murgilod even worse.

Yep, you can make your own templates BUT

  • they have to follow a very specific naming convention
  • you CAN make them per-project but it’s kinda a pain in the ass
  • the feature is, to put it mildly, incredibly poorly documented

It’s one of the many, many things that Unity supports but there’s no actual support for.

1 Like

I just started to make my own templating solution (well, just started yesterday). So far I did this:
MenuItems.cs in an Editor folder.

using System;
using UnityEditor;
using UnityEngine;

namespace LurkingNinja.CreateCs.Editor
{
    public static partial class MenuItems
    {
        private const string BASE_MENU = "Assets/Create C#/";
        private const string UNITY_CSHARP_MENU = BASE_MENU + "Unity's C# Script %&o";
        private const string CUSTOM_CSHARP_MENU = BASE_MENU + "C# Script %&n";
        private const string DEFAULT_BEHAVIOR_FILENAME = "NewBehaviorScript.cs";
     
        private static readonly string _baseTemplatePath = Application.dataPath
                + "/Plugins/LurkingNinja/CreateCs/Editor/ScriptTemplates/";
        private static readonly string _unityCsTemplatePath = EditorApplication.applicationContentsPath +
                "/Resources/ScriptTemplates/81-C# Script-NewBehaviourScript.cs.txt";
     
        private static readonly Type _pwuType = typeof(ProjectWindowUtil);
     
        [MenuItem(UNITY_CSHARP_MENU, false, 10001)]
        public static void CreateUnityCsMenu()
        {
            ProjectWindowUtil.CreateScriptAssetFromTemplateFile(
                _unityCsTemplatePath,
                DEFAULT_BEHAVIOR_FILENAME);
        }

        [MenuItem(CUSTOM_CSHARP_MENU, false, 10012)]
        public static void CreateCsMenu()
        {
            ProjectWindowUtil.CreateScriptAssetFromTemplateFile(
                _baseTemplatePath + "NewBehaviourScript.cs.txt",
                DEFAULT_BEHAVIOR_FILENAME);
        }
    }
}

NewBehaviourScript.cs.txt in _baseTemplatePath

using UnityEngine;

    #ROOTNAMESPACEBEGIN#
public class #SCRIPTNAME# : MonoBehaviour
{
    // Start is called before the first frame update
    private void Awake()
    {
        #NOTRIM#
    }
}
#ROOTNAMESPACEEND#

Anyone, feel free to use this as your base if you want to expand your template capabilities if you find it useful.
Obviously you can move the _baseTemplatePath anywhere you like. I do this way because eventually this moves into a package of mine, so the “dynamic”, editable template files have to remain in Assets/ space as opposed the functions which will be in packages (not modifiable) space.

Obviously the CTRL-ALT-O and CTRL-ALT-N shortcuts are redefinable in the Shortcuts window.

1 Like

That is true, and I’ve done that, what I was suggesting was for Unity to include more script creation options, meaning that the C# Script option in the Create Asset Menu would no longer be a “button” for lack of a better term, but a collection of various options of C# Scripts that could be created. The files I included were copied from the ScriptTemplates folder of my Unity Installation.

A fine idea. I don’t think I’d use templates, but can see the benefits.

Perhaps you should check out the product roadmap of Unity Editor. If you want something to be added, which isn’t currently there you should be able to submit a new idea.

There are multiple alternatives to the built-in template creator on the Asset Store, most of which are free. UT even had one of their own (‘Create Script Dialog’) at one point, with source.