Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unity Add Default Namespace to Script Template?

Tags:

c#

unity3d

I just found Unity's script template for C# scripts. To get the script name you write #SCRIPTNAME# so it looks like this:

using UnityEngine;
using System.Collections;

public class #SCRIPTNAME# : MonoBehaviour 
{
    void Start () 
    {
    
    }
    
    void Update () 
    {
    
    }
}

Then it would create the script with the right name, but is there something like #FOLDERNAME# so that I can put it in the right namespace directly when creating the script?

like image 856
Joakim Carlsson Avatar asked Sep 13 '16 03:09

Joakim Carlsson


People also ask

How do I change the default script in Unity?

To change the default IDE, an instance of the Unity Editor must be open. Open a pre-existing Unity project or create a new one via the Hub. Changing the IDE will apply to all other Unity projects which use that Unity version automatically. Check that none of the scripts within the project are currently open.

Should I use namespace Unity?

Use a namespace to ensure your scoping of classes/enum/interface/etc won't conflict with existing ones from other namespaces or the global namespace. Namespaces scope classes, this is important due to many times similar naming conventions are used for class names.

What is using UnityEngine?

UnityEngine for example is a collection of all the classes related to Unity. System. Collections is all the classes in . Net related to holding groups of data such as hashtable and array list. It is required whenever you use a class in that namespace.

How do I change the namespace of a Unity script template?

The script templates are located in your Unity install folder under > Editor > Data > Resources > ScriptTemplates (Unity 5.3 on Windows OS). Now any new scripts created in Unity will have your namespace by default. Note: You will probably need to change the read/write permissions on the template file before you can save any changes.

Where can I find unity's default script templates?

Unity's default templates can be found under your Unity installation's directory in Editor\Data\Resources\ScriptTemplates for Windows and /Contents/Resources/ScriptTemplates for OSX. And open the file 81-C# Script-NewBehaviourScript.cs.txt

How do I add my own code to a Unity script?

You can add whatever code you want to newly created scripts automatically by editing one of Unity's script template files. The script templates are located in your Unity install folder under > Editor > Data > Resources > ScriptTemplates (Unity 5.3 on Windows OS). Now any new scripts created in Unity will have your namespace by default.

Is it possible to change script template for Unity game engine?

I'm familiar with the ability to change script template for Unity game engine. However, it is very limited and only allows one keyword: #SCRIPTNAME#. As project gets more and more complicated, namespaces becomes crucial part.


2 Answers

There is no built-in template variables like #FOLDERNAME#.

According to this post, there are only 3 magic variables.

  • "#NAME#"
  • "#SCRIPTNAME#"
  • "#SCRIPTNAME_LOWER#"

But you can always hook into the creation process of a script and append the namespace yourself using AssetModificationProcessor.

Here is an example that adds some custom data to the created script.

//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();
   }
}
like image 183
zwcloud Avatar answered Sep 21 '22 06:09

zwcloud


Using zwcloud's answer and some other resources I was able to generate a namespace on my script files:

First step, navigate:

Unity's default templates can be found under your Unity installation's directory in Editor\Data\Resources\ScriptTemplates for Windows and /Contents/Resources/ScriptTemplates for OSX.

And open the file 81-C# Script-NewBehaviourScript.cs.txt

And make the following change:

namespace #NAMESPACE# {

At the top and

}

At the bottom. Indent the rest so that the whitespace is as desired. Don't save this just yet. If you wish, you can make other changes to the template, such as removing the default comments, making Update() and Start() private, or even removing them entirely.

Again, do not save this file yet or Unity will throw an error on the next step. If you saved, just hit ctrl-Z to undo and then resave, then ctrl-Y to re-apply the changes.

Now create a new script inside an Editor folder inside your Unity Assets directory and call it AddNameSpace. Replace the contents with this:

using UnityEngine;
using UnityEditor;

public class AddNameSpace : UnityEditor.AssetModificationProcessor {

    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;
        file = System.IO.File.ReadAllText(path);

        string lastPart = path.Substring(path.IndexOf("Assets"));
        string _namespace = lastPart.Substring(0, lastPart.LastIndexOf('/'));
        _namespace = _namespace.Replace('/', '.');
        file = file.Replace("#NAMESPACE#", _namespace);

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

Save this script as well as saving the changes to 81-C# Script-NewBehaviourScript.cs.txt

And you're done! You can test it by creating a new C# script inside any series of folders inside Assets and it will generate the new namespace definition we created.

like image 20
Draco18s no longer trusts SE Avatar answered Sep 19 '22 06:09

Draco18s no longer trusts SE