Unity 及 VS2015 创建脚本时自动添加头部注释

本文介绍了如何在Unity中创建脚本时自动添加头部注释,以及在VS2015中通过修改模板文件实现新脚本创建时添加注释的方法,从而提高开发效率。
摘要由CSDN通过智能技术生成

首先来看下Unity:

在Editor文件夹下创建脚本如下:

using UnityEngine;
using System.Collections;
using System.IO;

namespace UGUIFrameWorkEditor
{
    public class ChangeScriptTemplates : UnityEditor.AssetModificationProcessor
    {

        // 添加脚本注释模板
        private static string str =
        "// ========================================================\r\n"
        + "// Des:\r\n"
        + "// Autor:阿童木 \r\n"
        + "// CreateTime:#CreateTime#\r\n"
        + "// 版 本:v 1.0\r\n"
        + "// ========================================================\r\n";

        // 创建资源调用
        public static void OnWillCreateAsset(string path)
        {
            // 只修改C#脚本
            path = path.Replace(".meta", "");
            if (path.EndsWith(".cs"))
            {
                string allText = str;
                allText += File.ReadAllText(path);
                // 替换字符串为系统时间
                allText = allText.Replace("#CreateTime#", System.DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"));
                File.WriteAllText(path, allText);
            }
        }
    }
}

当我们创建脚本的时候就会自动添加头部注释了。

再来看看VS2015

因为我们有时候不想在unity中创建脚本,想在vs中进行创建,这样就不需要每次创建脚本unity都要进行编译。 我们找到F:\vs2015\Common7\IDE\ItemTemplatesCache\CSharp\Code\1033\WebClass(这是我的路径)下的Class脚本,打开是这样:

using System;
using System.Collections.Generic;
$if$ ($targetframeworkversion$ >= 3.5)using System.Linq;
$endif$using System.Web;

namespace $rootnamespace$
{
    public class $safeitemrootname$
    {
    }
}

我们需要在它上方添加如下:

/**
*
* 功 能:
* 类 名: $safeitemname$
*
* Ver Time Autor Des
* ───────────────────────────────────
* V0.01 $time$ 阿童木 初版
*
*/
using System;
using System.Collections.Generic;
$if$ ($targetframeworkversion$ >= 3.5)using System.Linq;
$endif$using System.Web;

namespace $rootnamespace$
{
    public class $safeitemrootname$
    {
    }
}

接着再找到F:\vs2015\Common7\IDE\ItemTemplatesCache\CSharp\Code\2052\Class路径下的Class,
将它修改为:

/**
*
* 功 能:
* 类 名: $safeitemname$
*
* Ver Time Autor Des
* ───────────────────────────────────
* V0.01 $time$ 阿童木 初版
*
*/

using System;
using System.Collections.Generic;
$if$ ($targetframeworkversion$ >= 3.5)using System.Linq;
$endif$using System.Text;
$if$ ($targetframeworkversion$ >= 4.5)using System.Threading.Tasks;
$endif$
namespace $rootnamespace$
{
    class $safeitemrootname$
    {
    }
}

然后我们每次在vs中创建一个脚本都会自动添加头部注释了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值