Unity3D自定义脚本模板

啥也不说直接上代码

/*******************************************************************
 * 文件名: ScriptTemplateEditor.cs
 * 时  间: 9/9/2015
 * 作  者: lzh
 * 公  司: CompanyName
 * 项目名: ProjectName
 * 描  述: 自定义脚本模板
 *         根据自己的需求,将这个脚本放到项目的Editor目录中即可。
 *******************************************************************/
using UnityEngine;
using UnityEditor;
using System.Text;

public class ScriptTemplateEditor : UnityEditor.AssetModificationProcessor
{
    static string AuthorName = "lzh";           // 可自定义
    static string CompanyName = "CompanyName";  // 可自定义
    static string ProjectName = "ProjectName";  // 可自定义

    public static void OnWillCreateAsset(string path)
    {
        path = path.Replace(".meta", "");
        path = path.Replace("Assets", "");
        path = Application.dataPath+path;

        int index = path.LastIndexOf("/");
        string fileName = path.Substring(index+1);

        string file = System.IO.File.ReadAllText(path);

        if(!file.StartsWith("//[lzh]"))
        {
            // 在【unity的安装目录下】\Unity\Editor\Data\Resources\ScriptTemplates
            // 的脚本模板加前面加上一行标记,我这是://[lzh],可自定义
            return;
        }
        file = file.Replace("//[lzh]", "");

        System.Text.StringBuilder sb = new System.Text.StringBuilder();
        sb.AppendLine("/***************************************************");
        sb.AppendLine(" * 文件名: " + fileName);
        sb.AppendLine(" * 描  述: ");
        sb.AppendLine(" * 时  间: " + System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
        sb.AppendLine(" * 作  者: " + AuthorName);
        sb.AppendLine(" * 公  司: " + CompanyName);
        sb.AppendLine(" * 项目名: " + ProjectName);
        sb.AppendLine(" ***************************************************/");
        sb.AppendLine(file);
        Encoding utf8withoutBom = new UTF8Encoding(false);
        System.IO.File.WriteAllText(path, sb.ToString(), utf8withoutBom );
        AssetDatabase.Refresh();
    }
    //public static void OnWillCreateAsset2(string path)
    //{
    //    path = path.Replace(".meta", "");
    //    int index = path.LastIndexOf(".");
    //    string file = path.Substring(index);
    //    if (file != ".cs" && file != ".js" && file != ".boo") return;
    //    string fileExtension = file;

    //    index = Application.dataPath.LastIndexOf("Assets");
    //    path = Application.dataPath.Substring(0, index) + path;
    //    file = System.IO.File.ReadAllText(path);

    //    file = file.Replace("#CreateDate#", System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
    //    file = file.Replace("#ProjectName#", PlayerSettings.productName);
    //    file = file.Replace("#CompanyName#", PlayerSettings.companyName);
    //    file = file.Replace("#FileExtension#", fileExtension);
    //    file = file.Replace("#AuthorName#", "li.zhihai");

    //    System.IO.File.WriteAllText(path, file);
    //    AssetDatabase.Refresh();
    //}   
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

阿海-程序员

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

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

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

打赏作者

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

抵扣说明:

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

余额充值