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.
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);
}
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’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.