zoukankan      html  css  js  c++  java
  • 不使用插件 修改Unity和C#创建时的默认模板




    1、修改Visual中创建类时的默认模板

    C:Program Files (x86)->
    Microsoft Visual Studio 14.0-> (选择对应自己版本的文件夹)
    Common7 -> IDE -> ItemTemplates -> CSharp -> Code -> 2052->
    Class->Class.cs

    注意此处的文件可能会因为保护的原因不让直接修改,我们可以先复制一份到其他地方,修改完之后再把源文件替换掉。

    /****************************************************
        文件:#SCRIPTNAME#.cs
        作者:Ffly
        邮箱: jitengfeiwork@gmail.com
        日期:#CreateTime#
        功能:Nothing
    *****************************************************/
    
    using System;
    using System.Collections.Generic;
    $if$ ($targetframeworkversion$ >= 3.5)using System.Linq;
    $endif$using System.Text;
    $if$ ($targetframeworkversion$ >= 4.5)using System.Threading.Tasks;
    $endif$
    namespace $rootnamespace$
    {
        class $safeitemrootname$
        {
        }
    }
    
    
    //显示出当前文件的名称
    #SCRIPTNAME#
    //显示当前文件创建的时间
    #CreateTime#
    

    效果图

    2、修改Unity中创建脚本时的默认模板

    C:Program Files (x86)->
    Microsoft Visual Studio 14.0-> (选择对应自己版本的文件夹)
    Common7 -> IDE -> ItemTemplates -> CSharp -> Code -> 2052->
    Class->Class.cs

    安装目录:UnityEditorDataResources
    ScriptTemplates81-C# Script-NewBehaviourScript.cs.txt

    /****************************************************
        文件:#SCRIPTNAME#.cs
        作者:Ffly
        邮箱: jitengfeiwork@gmail.com
        日期:#CreateTime#
        功能:Nothing
    *****************************************************/
    
    using UnityEngine;
    
    public class #SCRIPTNAME# : MonoBehaviour 
    {
    	private void Start(){
    		
    	}
    	private void Update(){
    		
    	}
    }
    

    但是注意,此处的日期在我们创建文件的时候并不会转换为当前的时间。
    所以我们需要一些代码的帮助。

    1、在项目目录下创建Editor文件夹
    2、新建脚本ScriptCreateInit.cs

    using System;
    using System.IO;
    
    public class ScriptCreateInit: UnityEditor.AssetModificationProcessor
    {
        private static void OnWillCreateAsset(string path)
        {
            path = path.Replace(".meta", "");
            if (path.EndsWith(".cs"))
            {
                string str = File.ReadAllText(path);
                str = str.Replace("Plane", Environment.UserName).Replace(
                                  "#CreateTime#", string.Concat(DateTime.Now.Year, "/", DateTime.Now.Month, "/",
                                    DateTime.Now.Day, " ", DateTime.Now.Hour, ":", DateTime.Now.Minute, ":", DateTime.Now.Second));
                File.WriteAllText(path, str);
            }
        }
    }
    

    这段代码主要作用就是把#CreateTime#转换为当前创建文件的时间。

  • 相关阅读:
    力扣Leetcode 3. 无重复字符的最长子串
    力扣Leetcode 21. 合并两个有序链表
    力扣Leetcode 202. 快乐数 -快慢指针 快乐就完事了
    力扣Leetcode 面试题56
    力扣Leetcode 33. 搜索旋转排序数组
    力扣Leetcode 46. 全排列
    python123期末四题编程题 -无空隙回声输出-文件关键行数-字典翻转输出-《沉默的羔羊》之最多单词
    深入理解ReentrantLock的实现原理
    深入图解AQS实现原理和源码分析
    Java:CAS(乐观锁)
  • 原文地址:https://www.cnblogs.com/Fflyqaq/p/12071616.html
Copyright © 2011-2022 走看看