美文网首页
Untiy3D脚本开头自动添加模板注释

Untiy3D脚本开头自动添加模板注释

作者: 小明小明哉 | 来源:发表于2018-08-15 15:51 被阅读0次

UnityEditor里中的类AssetModificationProcessor可以用来监听Project视图中资源的创建、删除、移动、保存,可以把如下脚本放在Editor目录中对新建脚本开发自动添加模板 注释。

using UnityEngine;
using UnityEditor;
using System.IO;

public class AddHeadComment : UnityEditor.AssetModificationProcessor 
{
   /// <summary>
   /// asset已经被创建完,文件已经生成到磁盘上,但是没有生成.meta文件和import之前被调用
   /// </summary>
   /// <param name="path">path的值是文件的路径+ .meta</param>
   public static void OnWillCreateAsset(string path)
   {
       string filePath = path.Replace(".meta", "");
       string fileExtension = Path.GetExtension(filePath);
       if (fileExtension != ".cs")
       {
           return;
       }
       string realPath = Application.dataPath.Replace("Assets", "") + filePath;
       string scriptContent = File.ReadAllText(realPath);

       string headComment = "/**\r\n *Copyright(C) 2018 by #COMPANY#\r\n *All rights reserved.\r\n *FileName:      #SCRIPTFULLNAME#\r\n *Author:        #AUTHOR# \r\n *Description:\r\n *Date:          #DATE#\r\n */\r\n";
       headComment = headComment.Replace("#COMPANY#", PlayerSettings.companyName);
       headComment = headComment.Replace("#SCRIPTFULLNAME#", Path.GetFileName(filePath));
       headComment = headComment.Replace("#AUTHOR#", System.Environment.UserName);
       headComment = headComment.Replace("#DATE#", System.DateTime.Now.ToString("yyyy-mm-dd"));
       scriptContent = scriptContent.Insert(0, headComment);
       File.WriteAllText(realPath, scriptContent);
   }
}

相关文章

网友评论

      本文标题:Untiy3D脚本开头自动添加模板注释

      本文链接:https://www.haomeiwen.com/subject/fzswbftx.html