Skip to content

Instantly share code, notes, and snippets.

@UrbanNuke
Last active July 13, 2021 19:34
Show Gist options
  • Save UrbanNuke/205ec318426ba8093d46fbbe64628b7f to your computer and use it in GitHub Desktop.
Save UrbanNuke/205ec318426ba8093d46fbbe64628b7f to your computer and use it in GitHub Desktop.
Extend Unity script templates

Extend Unity script templates.

  1. Put .txt file into %UNITY_EDITOR_PATH&\Data\Resources\ScriptTemplates

  2. Put .cs file into Assets\Editor

/*
* #CREATIONDATE#
* #PROJECTNAME# created by #COMPANYNAME#
*/
using UnityEngine;
#ROOTNAMESPACEBEGIN#
namespace #PROJECTNAME#
{
/// <summary>
/// Brief summary of what the class does
/// </summary>
public class #SCRIPTNAME# : MonoBehaviour
{
#region Fields
#NOTRIM#
#endregion
#NOTRIM#
#region Properties
#NOTRIM#
#endregion
#NOTRIM#
#region LifeCycles
private void Awake()
{
#NOTRIM#
}
private void Start()
{
#NOTRIM#
}
private void Update()
{
#NOTRIM#
}
#endregion
#NOTRIM#
#region PublicMethods
#NOTRIM#
#endregion
#NOTRIM#
}
}
#ROOTNAMESPACEEND#
using UnityEditor;
using System.IO;
public class ScriptPreprocessor : UnityEditor.AssetModificationProcessor
{
public static void OnWillCreateAsset(string metaFilePath)
{
string fileName = Path.GetFileNameWithoutExtension(metaFilePath);
if (!fileName.EndsWith(".cs"))
return;
string actualFilePath = $"{Path.GetDirectoryName(metaFilePath)}{Path.DirectorySeparatorChar}{fileName}";
string content = File.ReadAllText(actualFilePath);
string newContent = content.Replace("#PROJECTNAME#", PlayerSettings.productName);
newContent = newContent.Replace("#CREATIONDATE#", System.DateTime.Now + "");
newContent = newContent.Replace("#COMPANYNAME#", PlayerSettings.companyName);
if (content == newContent)
return;
File.WriteAllText(actualFilePath, newContent);
AssetDatabase.Refresh();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment