您的位置:首页 > 移动开发 > Unity3D

Unity3D自定义脚本模板

2015-11-17 17:03 393 查看
啥也不说直接上代码

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

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);
        System.IO.File.WriteAllText(path, sb.ToString());
        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();
    //}   
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: