unity创建新脚本自动添加指定注释像作者,创建时间,版权,文件名等

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lxt610/article/details/85336250

1、引言

  在我们创建脚本的时候,自动添加我们需要的注释内容像作者,创建时间,版权,文件名,会有很多好处!省去很多不必要的重复工作。

2、修改81-C# Script-NewBehaviourScript.cs.txt文件

  打开unity安装目录对应.\Unity\Editor\Data\Resources\ScriptTemplates\81-C# Script-NewBehaviourScript.cs.txt文件!
  我的目录是:D:\Tools\Unity\Unity2017.3.1f1\Editor\Data\Resources\ScriptTemplates\81-C# Script-NewBehaviourScript.cs.txt。
我的文件修改如下:

//=====================================================
// - FileName:      #SCRIPTNAME#.cs
// - Created:       #AuthorName#
// - CreateTime:      #CreateTime#
// - Email:         #AuthorEmail#
// - Description:   
// -  (C) Copyright 2008 - 2018,wegame ,Inc.
// -  All Rights Reserved.
//======================================================
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class #SCRIPTNAME# : MonoBehaviour {

    // Use this for initialization
    void Start () {
        #NOTRIM#
    }
    
    // Update is called once per frame
    void Update () {
        #NOTRIM#
    }
}

3、添加脚本Copyright.cs

  在工程中的Editor目录下添加脚本Copyright.cs

//=====================================================
// - FileName:      #SCRIPTNAME#.cs
// - Created:       #AuthorName#
// - CreateTime:      #CreateTime#
// - Email:         #AuthorEmail#
// - Description:   
// -  (C) Copyright 2008 - 2018,wegame ,Inc.
// -  All Rights Reserved.
//======================================================
using UnityEngine;
using System.Collections;
using System.IO;

public class Copyright: UnityEditor.AssetModificationProcessor
{
    private const string AuthorName="EricLee";
    private const string AuthorEmail = "[email protected]";

    private const string DateFormat = "yyyy/MM/dd HH:mm:ss";
    private static void OnWillCreateAsset(string path)
    {
        path = path.Replace(".meta", "");
        if (path.EndsWith(".cs"))
        {
            string allText = File.ReadAllText(path);
            allText = allText.Replace("#AuthorName#", AuthorName);
            allText = allText.Replace("#AuthorEmail#", AuthorEmail);
            allText = allText.Replace("#CreateTime#", System.DateTime.Now.ToString(DateFormat));            
            File.WriteAllText(path, allText);
            UnityEditor.AssetDatabase.Refresh();
        }
    }
}

4、使用效果

//=====================================================
// - FileName:       	test.cs
// - Created:        		EricLee
// - CreateTime:     2018/12/29 11:30:12
// - Email:         	 [email protected]
// - Description:    这是一个测试脚本
// -  (C) Copyright 2008 - 2018,wegame ,Inc.
// -  All Rights Reserved.
//======================================================
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class test : MonoBehaviour {

    // Use this for initialization
    void Start () {
      
    }
    
    // Update is called once per frame
    void Update () {
       
    }
}

5、结束语

The End
  好了,今天的分享就到这里,如有不足之处,还望大家及时指正,随时欢迎探讨交流!!!


喜欢的朋友们,请帮顶、点赞、评论!您的肯定是我写作的不竭动力!

猜你喜欢

转载自blog.csdn.net/lxt610/article/details/85336250