Recommended reading

  • CSDN home page
  • GitHub open source address
  • Unity3D plugin sharing
  • Jane’s address book
  • My personal blog
  • QQ group: 1040082875

One, foreword

Some code has a bunch of comments in the header:

(1) State who wrote it

(2) When was it created

(3) What version

(4) What role

(5) Version change time

In this way, you can clearly see who wrote the script, what was written, the time version of the change, etc., which is good for development.

Always always write a script, copy the past, change, also feel a little cumbersome.

I’ll show you how to automatically add headers to scripts.

Second, the implementation

using System.IO;

namespace Editor
{
    /// <summary>
    ///Create scripts to automatically add headers
    /// </summary>
    public class FirstComment : UnityEditor.AssetModificationProcessor
    {
        /// <summary>
        ///Called when the resource is created and.meta is generated
        /// </summary>
        /// <param name="path">Automatically passing in the resource path</param>
        public static void OnWillCreateAsset(string path)
        {
            path = path.Replace(".meta"."");
            if(! path.EndsWith(".cs")) return;
            string allText = "// ========================================================\r\n"
                             + "// description: \r\n"
                             + // function: \r\n"
                             + "// author: XXX \r\n"
                             + "// create time: #CreateTime#\r\n"
                             + "// version: 1.0\r\n"
                             + "// Change time: \r\n"
                             + "// Change version: #CreateTime2#\r\n"
                             + // script path: #ScripsPath#\r\n"
                             + "// ========================================================\r\n";
            allText += File.ReadAllText(path);
            allText =  allText.Replace("#CreateTime#", System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            allText = allText.Replace("#ScripsPath#", path); File.WriteAllText(path, allText); }}}Copy the code

Effect: