Unity自定义脚本的 初始模版

参考博主:Unity修改创建的脚本模板,Unity脚本模板路径_unity hub 怎么改脚本模板_先生沉默先的博客-CSDN博客

【100个 Unity实用技能】 ☀️ | Unity自定义脚本的初始模版_unity 模板脚本_呆呆敲代码的小Y的博客-CSDN博客 

一,将脚本放到Editor文件夹下

using UnityEditor;
using UnityEngine;
using System.IO;

public class AddFileHeadComment : UnityEditor.AssetModificationProcessor
{
    /// <summary>  
    /// 此函数在asset被创建完,文件已经生成到磁盘上,但是没有生成.meta文件和import之前被调用  
    /// </summary>  
    /// <param name="newFileMeta">newfilemeta 是由创建文件的path加上.meta组成的</param>  
    public static void OnWillCreateAsset(string newFileMeta)
    {
        string newFilePath = newFileMeta.Replace(".meta", "");
        string fileExt = Path.GetExtension(newFilePath);
        if (fileExt != ".cs")
        {
            return;
        }
        //注意,Application.datapath会根据使用平台不同而不同  
        string realPath = Application.dataPath.Replace("Assets", "") + newFilePath;
        string scriptContent = File.ReadAllText(realPath);

        //这里实现自定义的一些规则  
        scriptContent = scriptContent.Replace("#SCRIPTNAME#", Path.GetFileName(newFilePath));
        scriptContent = scriptContent.Replace("#COMPANY#", PlayerSettings.companyName);
        scriptContent = scriptContent.Replace("#AUTHOR#", "Passion");
        scriptContent = scriptContent.Replace("#VERSION#", "1.0");
        scriptContent = scriptContent.Replace("#UNITYVERSION#", Application.unityVersion);
        scriptContent = scriptContent.Replace("#CREATETIME#", System.DateTime.Now.ToString("F"));

        File.WriteAllText(realPath, scriptContent);
    }
}

二,找到unity自带脚本模板

 找到文件名为:81-C# Script-NewBehaviourScript.cs

用一下代码区修改它(可以自行添加一些常用的饮用)

/**********************************************************************
 文件信息
 文件名(File Name):                #SCRIPTNAME#.cs
 作者(Author):                      TianWenQuan
 创建时间(CreateTime):             #CREATETIME#
 Unity版本(UnityVersion):         #UNITYVERSION#
 项目:**虚拟仿真实验
 **********************************************************************/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
namespace Twq
{
 public class #SCRIPTNAME# : MonoBehaviour
 {
   
    void Awake()
    {
        #NOTRIM#
    }

  
    void Update()
    {
        #NOTRIM#
    }
 }
}



猜你喜欢

转载自blog.csdn.net/qq_37524903/article/details/132346401