You can find the script templates for automatically generating code in your Unity installation folder. I find the templates under "Unity/Editor/Data/Resources/ScriptTemplates", while other sources have found it under "Unity/Editor/Data/Resources".
The generic UnityScript and C# templates are identified as the files "82-Javascript-NewBehaviourScript.js.txt" and "81-C# Script-NewBehaviourScript.cs.txt", respectively. You can directly edit these files to change the way in which Unity auto generates script.
You can also include additional templates, which will appear when you select "Create" from the "Project" window. Templates do not appear to require unique numbering, and use the initial string to determine the menu hierarchy, where "__" denotes a submenu. For example, having a file named "81-C# Script__Editor Script-NewBehaviourScript.cs.txt" will give you an additional "C# Script" menu, with the sub option to create an "Editor Script" using this template.
Do not rename the original templates; these are used more directly, by the engine. For example, renaming "81-C# Script-NewBehaviourScript.cs.txt" will prevent you from adding new C# scripts as components, directly through the inspector.
Below is my own example, though it demonstrates particular practices that I am most custom to. For example, I prefer to have my custom editor script in the same file as the target class, so I encapsulate it in #if UNITY_EDITOR .. #endif
, instead of placing it in a generic "do not compile in build" editor folder.
I am not sure if it is even possible to provide the context of a custom namespace; I simply use "NAMESPACE", as this allows me to provide the correct namespace post-creation, using the commonly-inbuilt "find..replace all" function.
The Template:
/* Created by Gnemlock */
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif
namespace NAMESPACE
{
public class #SCRIPTNAME# : MonoBehaviour
{
/// <summary>This method will be called at the start of each frame where this
/// instance of <see cref="NAMESPACE.#SCRIPTNAME#"/> is enabled.</summary>
void Update ()
{
#NOTRIM#
}
}
}
namespace NAMESPACE.UTILITY
{
#if UNITY_EDITOR
[CustomEditor(typeof(#SCRIPTNAME#))] public class #SCRIPTNAME#Editor : Editor
{
public override void OnInspectorGUI()
{
DrawDefaultInspector();
#SCRIPTNAME# s#SCRIPTNAME# = target as #SCRIPTNAME#;
}
}
#endif
}
The Output:
/* Created by Gnemlock */
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif
namespace MyNamespace
{
public class UpdatedClass : MonoBehaviour
{
/// <summary>This method will be called at the start of each frame where this
/// instance of <see cref="MyNamespace.UpdatedClass"/> is enabled.</summary>
void Update ()
{
}
}
}
namespace MyNamespace.UTILITY
{
#if UNITY_EDITOR
[CustomEditor(typeof(UpdatedClass))] public class UpdatedClassEditor : Editor
{
public override void OnInspectorGUI()
{
DrawDefaultInspector();
UpdatedClass sUpdatedClass = target as UpdatedClass;
}
}
#endif
}