The Unity script automatically adds header comments

Posted by ladokha on Tue, 04 Jan 2022 17:30:27 +0100

The Unity script automatically adds header comments

Is to create cs automatically add author name, creation time, file name, etc

Let's take a look at the example. We first create a Scripts folder under the project panel of Unity, and then create a script under the Scripts folder, named: test cs

Then it's like this:

This is because I have successfully set this header comment

1. Modify Unity template

First, find the installation path of your Unity and find such a folder. The file name is related to the version

Then open the folder and find the Editor\Data\Resources\ScriptTemplates folder all the way

Then, open a script called 81-c# script new behavior script cs. Txt file, open

Here's the thing:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

    #ROOTNAMESPACEBEGIN#
public class #SCRIPTNAME# : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        #NOTRIM#
    }

    // Update is called once per frame
    void Update()
    {
        #NOTRIM#
    }
}
#ROOTNAMESPACEEND#

This is the template file created by our script. The length of each script file we create is related to this thing

We just need to add notes on it, of course, it's not enough

add

/**
 * ==========================================
 * FileName: #FileName#
 * Author: #Name#
 * CreatTime: #CreateTime#
 * NowPath: #path#
 * ==========================================
 */

Then save and exit

Try creating a script again, and there will be more of these things. Of course, these are dead and fixed

We can see the effect of this only:

Yeah, it's just dead

2. Change the code to dynamic

Notice the Editor folder I created?

Although I created it myself, some folders in Unity have some special functions

For example, this Editor is used to:

A folder named Editor allows scripts in it to access the API of Unity Editor. If a class or method in the UnityEditor namespace is used in the script, it must be placed in a folder called Editor. Scripts in the Editor folder are not included during build.
You can have multiple Editor folders in a project.

Then we first create an Editor folder, and then create a script under this folder with any name, and then write code

using UnityEngine;    
using UnityEditor;	// The namespace of the inherited class
using System.IO;	// IO file operation namespace
using System;		// C # basic function namespace
using System.Text.RegularExpressions;	// Namespace for regular expressions

public class TitleSet : UnityEditor.AssetModificationProcessor
{
   private static void OnWillCreateAsset(string path)
   {
       path = path.Replace(".meta", "");   // Here, path is the main path of your project Asset/Scripts / file name
       if (path.EndsWith(".cs"))    // Determine whether it is a c# file
       {
           string fileName = Regex.Match(path, @"[^/]*$").Value;    // Get the string containing only the file name through regular
           string str = File.ReadAllText(path);    // Gets the entire contents of the created file name
           str = str.Replace("#Name#", "Bu Xiaochan").Replace("#CreateTime#", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")).Replace("#FileName#", fileName).Replace("#path#", path); / / replace the header comment
           File.WriteAllText(path, str);   // Write the replaced content to the file and overwrite the original content
           AssetDatabase.Refresh();   
       }
   }
}

The comments in the code have made the code very clear, so I won't say more

You can also add other information if you want to add it yourself

epilogue

The brilliance of success is followed by cheap tears and tears

Learning is endless. Only by maintaining the apprenticeship mentality can we climb the peak bravely

Welcome those who love Python and Unity (game development engine). Let's move towards the great God step by step. Success is not far away, just two words, persistence!!

Unity game engine declaration:

Do you love games?

Have you ever dreamed of making your own game one day?

Don't hesitate, study quickly!

Click the link to view the Python community: Python communication community

Click the link to view Unity community: Game development enthusiasts

Topics: C# Unity