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?
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.
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.
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.
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.
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
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.
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.
There is no built-in template variables like #FOLDERNAME#.
According to this post, there are only 3 magic variables.
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();
}
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With