Search Unity

  1. We've closed the job boards. If you're looking for work, or looking to hire check out Unity Connect. You can see more information here.
    Dismiss Notice
  2. Unity 2017.3 has arrived! Read about it here.
    Dismiss Notice
  3. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

C# Script Template - how to make custom changes?

Discussion in 'Scripting' started by hgeghamyan, Oct 10, 2014.

  1. hgeghamyan

    hgeghamyan

    Joined:
    Oct 10, 2014
    Posts:
    3
    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#)?
     
    klesun and Ryuuguu like this.
  2. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    1,922
    MAybe you can write an asset postprocessor to do your own custom string replaces
     
  3. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    5,950
    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.)
     
  4. casimps1

    casimps1

    Joined:
    Jul 28, 2012
    Posts:
    253
    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:

    http://u3d.as/content/unity-technologies/create-script-dialog/2B6

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

    hgeghamyan

    Joined:
    Oct 10, 2014
    Posts:
    3
    Thanks a lot @casimps1, looks like there are only 3 magic variables:
    "#NAME#"
    "#SCRIPTNAME#"
    "#SCRIPTNAME_LOWER#"
     
  6. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    1,922
    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.

    Code (CSharp):
    1. //Assets/Editor/KeywordReplace.cs
    2. using UnityEngine;
    3. using UnityEditor;
    4. using System.Collections;
    5.  
    6. public class KeywordReplace : UnityEditor.AssetModificationProcessor {
    7.  
    8.    public static void OnWillCreateAsset ( string path ) {
    9.      path = path.Replace( ".meta", "" );
    10.      int index = path.LastIndexOf( "." );
    11.      string file = path.Substring( index );
    12.      if ( file != ".cs" && file != ".js" && file != ".boo" ) return;
    13.      index = Application.dataPath.LastIndexOf( "Assets" );
    14.      path = Application.dataPath.Substring( 0, index ) + path;
    15.      file = System.IO.File.ReadAllText( path );
    16.  
    17.      file = file.Replace( "#CREATIONDATE#", System.DateTime.Now + "" );
    18.      file = file.Replace( "#PROJECTNAME#", PlayerSettings.productName );
    19.      file = file.Replace( "#SMARTDEVELOPERS#", PlayerSettings.companyName );
    20.  
    21.      System.IO.File.WriteAllText( path, file );
    22.    }
    23. }
    It won't work on old versions where meta files aren't created - this function doesn't actually call on the script files themselves
     
    Last edited: Oct 12, 2014
  7. hgeghamyan

    hgeghamyan

    Joined:
    Oct 10, 2014
    Posts:
    3
    @hpjohn thanks a lot for your help. This is exactly the thing I was looking for.
     
  8. Marc-Zaku

    Marc-Zaku

    Joined:
    Jan 30, 2015
    Posts:
    1
    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.


    Code (CSharp):
    1. public static void OnWillCreateAsset (string path) {
    2.         path = path.Replace( ".meta", "" );
    3.         int index = path.LastIndexOf( "." );
    4.         if (index < 0)
    5.             return;
    6.  
    7.         string file = path.Substring( index );
    8.         if (file != ".cs" && file != ".js" && file != ".boo")
    9.             return;
    10.  
    11.         index = Application.dataPath.LastIndexOf( "Assets" );
    12.         path = Application.dataPath.Substring( 0, index ) + path;
    13.         if (!System.IO.File.Exists( path ))
    14.             return;
    15.  
    16.         string fileContent = System.IO.File.ReadAllText( path );
    17.         fileContent = fileContent.Replace( "#AUTHOR#", AuthorName );
    18.         fileContent = fileContent.Replace( "#NAMESPACE#", GetNamespaceForPath(path) );
    19.  
    20.         System.IO.File.WriteAllText( path, fileContent );
    21.         AssetDatabase.Refresh();
    22.     }
     
    JoaoBorks likes this.
  9. shy-chris

    shy-chris

    Joined:
    Jan 5, 2016
    Posts:
    1
    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!
     
  10. pixeye

    pixeye

    Joined:
    Jul 2, 2011
    Posts:
    179
    @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.
     
  11. AlanMattano

    AlanMattano

    Joined:
    Aug 22, 2013
    Posts:
    644
    Does anybody know #NOTRIM# what is it for?
     
  12. adamk33n3r

    adamk33n3r

    Joined:
    Mar 19, 2014
    Posts:
    1
    I believe all it is used for is to prevent the tabs from getting trimmed.