C# Script Template - how to make custom changes?

Hi people.

I found template files in “[Unity-installation-folder]\Editor\Data\Resources\ScriptTemplates” and edited 81-C# Script-NewBehaviourScript.cs.txt file as I wish (Note: Use for example Notepad++ as administrator for editing).
Now I want to add Current Date, Developer Name, Project Name and other project corresponding data by default. Can someone post full list of available Key Vars (like we have now #SCRIPTNAME#)?

3 Likes

MAybe you can write an asset postprocessor to do your own custom string replaces

Hey, @hgeghamyan , thanks for pointing this out! I’ve now edited my own C# template file, and I’m much happier for it.

In case anyone else wonders, on the Mac you find these inside the Unity application bundle, under Contents/Resources/ScriptTemplates.

(I’m sorry I don’t know the answer to your question, though.)

1 Like

I don’t think there are any other magic variables that get processed in those templates…

Another option - you can create new templates. For example, if you put a new file in that folder named “85-C# Singleton Script-NewBehaviourScript.cs.txt” then you would get a new menu option in the editor for Create->C# Singleton Script (Note - Unity needs to be restarted to recognize new templates). In your case you could maybe make a template for each project to get what you want? Not a great solution though…

You should maybe also check out the free Create Script Dialog package in the asset store here:

You could probably extend that package to do what you want fairly easily.

Thanks a lot @casimps1 , looks like there are only 3 magic variables:
#NAME#”
#SCRIPTNAME#”
#SCRIPTNAME_LOWER#”

Done!
Just put this into your Editor folder, and change/duplicate lines 17-19 to fit your keywords, and add those keywords to your script templates.

//Assets/Editor/KeywordReplace.cs
using UnityEngine;
using UnityEditor;
using System.Collections;

public class KeywordReplace : UnityEditor.AssetModificationProcessor {

   public static void OnWillCreateAsset ( string path ) {
     path = path.Replace( ".meta", "" );
     int index = path.LastIndexOf( "." );
     string file = path.Substring( index );
     if ( file != ".cs" && file != ".js" && file != ".boo" ) return;
     index = Application.dataPath.LastIndexOf( "Assets" );
     path = Application.dataPath.Substring( 0, index ) + path;
     file = System.IO.File.ReadAllText( path );

     file = file.Replace( "#CREATIONDATE#", System.DateTime.Now + "" );
     file = file.Replace( "#PROJECTNAME#", PlayerSettings.productName );
     file = file.Replace( "#SMARTDEVELOPERS#", PlayerSettings.companyName );

     System.IO.File.WriteAllText( path, file );
     AssetDatabase.Refresh();
   }
}

It won’t work on old versions where meta files aren’t created - this function doesn’t actually call on the script files themselves

12 Likes

@hpjohn thanks a lot for your help. This is exactly the thing I was looking for.

Great Post, hpjohn. I know, this thread is old, but I thought, I’d revive it to fix the code snippet, as it is one of the first and best results for certain searches.
The two new return-statements are important for not causing exceptions when Folder-Assets named WhatEver (first return condition) or WhatEver.cs (third return condition) are added to the project.

public static void OnWillCreateAsset (string path) {
        path = path.Replace( ".meta", "" );
        int index = path.LastIndexOf( "." );
        if (index < 0)
            return;

        string file = path.Substring( index );
        if (file != ".cs" && file != ".js" && file != ".boo")
            return;

        index = Application.dataPath.LastIndexOf( "Assets" );
        path = Application.dataPath.Substring( 0, index ) + path;
        if (!System.IO.File.Exists( path ))
            return;

        string fileContent = System.IO.File.ReadAllText( path );
        fileContent = fileContent.Replace( "#AUTHOR#", AuthorName );
        fileContent = fileContent.Replace( "#NAMESPACE#", GetNamespaceForPath(path) );

        System.IO.File.WriteAllText( path, fileContent );
        AssetDatabase.Refresh();
    }
2 Likes

WOW… just recently stumbled upon the ability to create own script templates.
AWESOME script the you guys wrote! helped me so so much to make the templates even more useful!
THANKS a lot!

@hgeghamyan @Marc_Zaku I’ve slightly improved script template to make it more automate. There is no need to edit something in script anymore, just edit special setting txt file and add templates in special folder : )
I’ve described it in my blog, check out.

Does anybody know #NOTRIM# what is it for?

I believe all it is used for is to prevent the tabs from getting trimmed.

2 Likes