Feature Request: Multiple Script Templates

NOTE: I’ve searched and searched for the right place to post a feature request without luck, so if there’s a better place, just let me know.

Whenever I write an Editor script I have to start from the MonoBehaviour template. Not the end of the world, but a little tiresome having to write in boiler-plate code. So here’s my idea…

When creating a new script file, if any of a list of user-defined keywords exists in the filename, use an associated template. e.g., When I create “FoobarInspector.cs” or “FoobarEditor.cs” a custom inspector template would be used, or “FoobarDrawer.cs” would use a custom property drawer template.

I’m sure there are a few other use-cases, maybe more specific to an individual developer, that would make it nice to be able to customize this behavior.

can do same with visual studio snippets

If you use the MenuItem attribute and name it “Assets/Create/****” you can create an entry that shows up in the Assets->Create menu that calls a static method in one of your editor scripts.

Then in that method just copy some template file to the location you want.

Something like:

        [MenuItem("Assets/Create/Create Something", priority = 100)] //priority 100 puts it in the same general region as C# Script
        public static void CreateFuck()
        {
            var rootPath = System.IO.Path.GetDirectoryName(Application.dataPath); //strips the /Assets from dataPath
            string folder = rootPath + "/" + AssetDatabase.GetAssetPath(Selection.activeObject);
            if (System.IO.File.Exists(folder)) folder = System.IO.Path.GetDirectoryName(folder);

            var path = folder + "/NewFile.cs";
            int i = 0;
            while(System.IO.File.Exists(path))
            {
                i++;
                path = folder + string.Format("/NewFile ({0}).cs", i);
            }
            System.IO.File.Copy("*location of template*", path , false);
        }
2 Likes

Nice! I’ll give that a go.

It’s not the same as auto-generating editor boiler-plate, but you can put templates in this directory: Unity\Editor\Data\Resources\ScriptTemplates

They can’t have the same number as Unity ones, so empty C# script would look like this:

Editor\Data\Resources\ScriptTemplates\94-C# Class-NewClass.cs.txt

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class #SCRIPTNAME#
{
   
}

I know about those template files, and have occasionally customized the default MonoBehaviour template. …but are you suggesting that making additional templates in that folder would be accessible from the Editor? Or are you suggesting that folder in combination with lordofduct’s tool script above?

Both their suggestions work. You can place your own templates in the installation folder of Unity, however, they are gone every time you install a new version. You can also use lordofduct’s solution, however, there is an easier solution to that, since there does exist code around creating files from templates. Take a look on How to create your own C# Script Template?

I posted examples for it here: https://discussions.unity.com/t/776670/2

You can stuff those templates directly into the project if you want them to be per-project rather than per-install. See here .

@Ardenian & @Baste Thanks for the tips! I didn’t know about UnityEditor.ProjectWindowUtil.CreateScriptAssetFromTemplateFile but perhaps even better than documentation, I found the source on GitHub: https://github.com/Unity-Technologies/UnityCsReference/blob/master/Editor/Mono/ProjectWindow/ProjectWindowUtil.cs

I’ll use that to write my own solution. Mine will strip away a keyword (e.g. “Inspector”) to determine which template to use, and then allow templates to be written with two auto-fill parameters instead of just the one – #SCRIPTNAME# (e.g., “MyScriptInspector”) as well as #BASESCRIPTNAME# (e.g., “MyScript”) A ScriptableObject will allow templates to be mapped to keywords.

They appear as additional templates in the right-click menu in Unity. The name of the template dictates which submenu it appears in.