I got a question. I just found Unitys Script template for C# Scripts.
And 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 ()
{
}
}
Than it would create the script with the right name, but is there somthing like #FOLDERNAME# So I can put it in the right namespace dirrectly when creating the script?
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.
Using zwcloud's answer and some other resources I was able to generate a namespace on my script files:
First step, navigate:
And open the file
81-C# Script-NewBehaviourScript.cs.txt
And make the following change:
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()
andStart()
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: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.
OK, so this question was already answered by two wonderful people, zwcloud and Draco18s, and their solution works, I'm just showing another version of the same code that, I hope, will be a little more clear in terms of what exactly happening.
Quick notes:
Yes, in this method we are getting not the actual file path, but the path of its meta file as a parameter
No, you can not use
AssetModificationProcessor
withoutUnityEditor
prefix, it is deprecatedOnWillCreateAsset
method is not shown via Ctrl+Shift+M, 'override' typing or base class metadata_
And this is the contents of my file
c:\Program Files\Unity\Editor\Data\Resources\ScriptTemplates\81-C# Script-NewBehaviourScript.cs.txt