Unity 自定义脚本样式

#Unity 脚本样式

//***********************************
// UaerName:  龍
// CreateTime:  2020-03-26 20:44:01
// Path:  RootPath\unity5.4\Editor\Data\Resources\ScriptTemplates\81-C# Script-NewBehaviourScript.cs
// 备注:  创建脚本样式
//***********************************

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

public class ScriptCreateInit : UnityEditor.AssetModificationProcessor
{
    private static string projectPath;
    private static bool isBusy;

    //项目名称
    public static string ProjectPath
    {
        get
        {
            if (string.IsNullOrEmpty(projectPath))
            {
                  projectPath = System.IO.Directory.GetParent(UnityEngine.Application.dataPath).Name;
                //Debug.Log("ProjectName:" + projectPath);
            }
            return projectPath;
        }
    }

   //创建Asset调用
    private static void OnWillCreateAsset(string path)
    {
        path = path.Replace(".meta", "");
        if (path.EndsWith(".cs"))
        {   
            if (isBusy) return; 
            isBusy = true;

            string strContent = File.ReadAllText(path);
            //strContent = ReplaceTemp(strContent);
            strContent = AppendStr(strContent);

            File.WriteAllText(path, strContent);
            AssetDatabase.Refresh();
        } 
    }


     //替换模板
    private static string ReplaceTemp(string strContent)
    {
        Dictionary<string, string> dic = new Dictionary<string, string>();
        dic.Add("#AuthorName#", "龍");
        dic.Add("#CreateTime#", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
        //dic.Add("#ProjectName#", string.IsNullOrEmpty(ProjectPath) ? "CWL" : projectPath);

        int index = 0;
        foreach (var item in dic)
        {
            index = strContent.IndexOf(item.Key);
            if (index != -1)
                strContent = strContent.Replace(item.Key, item.Value);
        }

        //string strContent = File.ReadAllText(path);
        //strContent = strContent.Replace("#AuthorName#", "龍");
        //strContent = strContent.Replace("#CreateTime#", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
        //strContent = strContent.Replace("#ProjectName#", string.IsNullOrEmpty(ProjectPath) ? "CWL" : projectPath);

        return strContent;
    }

    //添加 自定义
    private static string AppendStr(string strContent)
    {
        System.Text.StringBuilder sb = new System.Text.StringBuilder();
        sb.AppendLine($"//***********************************");
        sb.AppendLine($"// AuthorName:  龍");
        sb.AppendLine($"// CreateTime:  {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}");
        sb.AppendLine($"// 备注:  ");
        sb.AppendLine($"//***********************************");
        sb.AppendLine();

        sb.AppendLine(strContent);

        return sb.ToString();
    } 
}

你可能感兴趣的:(unity,游戏引擎)