自动在脚本头部添加开发信息

10 篇文章 1 订阅
7 篇文章 0 订阅

效果图

在这里插入图片描述

制作

Editor目录下新建脚本,我这里取名为ScriptHeadComment

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;

public class ScriptHeadComment : UnityEditor.AssetModificationProcessor
{
    public static void OnWillCreateAsset(string metaName)
    {
        string filePath = metaName.Replace(".meta", ""); // "Assets/Scripts/ScriptHeadCommentTest.cs"
        string fileExt = Path.GetExtension(filePath); // ".cs"
        if (fileExt != ".cs")
        {
            return; // 不是脚本
        }
        string fileFullPath = Application.dataPath.Replace("Assets", "") + filePath; // "D:/Program Files/UnityProjects/UnityTest/Assets/Scripts/ScriptHeadCommentTest.cs"
        string fileContent = File.ReadAllText(fileFullPath); // "using System.Collections;\r\nusing System.Collections.Generic;\r\nusing UnityEngine;\r\n\r\npublic class ScriptHeadCommentTest : MonoBehaviour {\r\n\r\n\t// Use this for initialization\r\n\tvoid Start () {\r\n\t\t\r\n\t}\r\n\t\r\n\t// Update is called once per frame\r\n\tvoid Update () {\r\n\t\t\r\n\t}\r\n}\r\n"
        string commentContent = "/*\n *FileName:      #FILENAME#\n *Author:        #AUTHOR#\n *Date:          #DATE#\n *UnityVersion:  #UNITYVERSION#\n */\n"; // 按照自己的设计添加需要自动生成的信息,调整好间距
        commentContent = commentContent.Replace("#FILENAME#", Path.GetFileName(fileFullPath));
        commentContent = commentContent.Replace("#AUTHOR#", Environment.UserName);
        //commentContent = commentContent.Replace("#DATE#", DateTime.Now.ToString("yyyy/mm/dd hh:mm:ss")); mm表示分钟,hh表示12进制的小时
        commentContent = commentContent.Replace("#DATE#", DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"));
        commentContent = commentContent.Replace("#UNITYVERSION#", Application.unityVersion);
        //fileContent.Insert(0, commentContent); 这里容易出错,不改变原值
        fileContent = fileContent.Insert(0, commentContent); // "/*\n *FileName:      ScriptHeadCommentTest.cs\n *Author:        tangmingzhe\n *Date:          2020/11/12 20:00:16\n *UnityVersion:  2018.2.0f2\n */\nusing System.Collections;\r\nusing System.Collections.Generic;\r\nusing UnityEngine;\r\n\r\npublic class ScriptHeadCommentTest : MonoBehaviour {\r\n\r\n\t// Use this for initialization\r\n\tvoid Start () {\r\n\t\t\r\n\t}\r\n\t\r\n\t// Update is called once per frame\r\n\tvoid Update () {\r\n\t\t\r\n\t}\r\n}\r\n"
        File.WriteAllText(fileFullPath, fileContent);
    }
}

测试

新建测试脚本ScriptHeadCommentTest,等待编译完成

/*
 *FileName:      ScriptHeadCommentTest.cs
 *Author:        ---------
 *Date:          2020/11/12 20:00:16
 *UnityVersion:  2018.2.0f2
 */
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ScriptHeadCommentTest : MonoBehaviour {

	// Use this for initialization
	void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {
		
	}
}

最终代码显示如上,名字我特殊处理了,这样处理的好处是只要提交了ScriptHeadComment 这个脚本,团队中的所有人在新建脚本时都会自动生成这些信息,方便记录与追责
如果想自定义脚本模板请移步我这篇文章自定义脚本模板

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阿斯提尼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值