(转载)Unity为脚本模版添加自定义版权头

原文地址:(23条消息) 如何修改新建脚本模板-ScriptTemplates(Unity3D开发之十五)_Cocos2der的博客-CSDN博客_unity新建脚本

unity添加新脚本的时候,没有版权文件头信息,主要是没有脚本创建人的姓名,在项目开发中,如果想知道这个脚本是谁写的,呼来唤去搞半天才发现是自己写的!!!

用习惯了xcode,所以准备给unity的新建脚本添加一个信息头内容。

Unity自己就有新建脚本模板文件,但是这个文件里面的预定义key很少,不够咱们使用。所以需要自己添加几个key。然后在新建脚本的时候替换这个几个key为对应的内容。

一、修改模板文件

修改模板文件详细教程

打开模板文件 编辑器下载路径2021.3.1f1c1\Editor\Data\Resources\ScriptTemplates/81-C# Script-NewBehaviourScript.cs.txt
修改为:

//
//  #SCRIPTNAME##FILEEXTENSION#
//  #PROJECTNAME#
//
//  Created by #SMARTDEVELOPERS# on #CREATIONDATE#.
//
//

using UnityEngine;
using System.Collections;

public class #SCRIPTNAME# : MonoBehaviour {

    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {

    }

}

上面的就是版权头信息,新建的脚本就这下面的效果了:

//
//  TestClass.cs
//  DomoJump
//
//  Created by YanghuiLiu on 04/09/2015.
//
//

using UnityEngine;
using System.Collections;

public class TestClass : MonoBehaviour {

    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {

    }

}

二、添加Editor脚本,用来解析上面我们自己添加的key

//
//  HEScriptKeywordReplace.cs
//  HEUnityExtensionLib
//
//  Created by YanghuiLiu on 04/09/2015.
//
//

using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif
using System.Collections;

public class HEScriptKeywordReplace : UnityEditor.AssetModificationProcessor
{
#if UNITY_EDITOR
    public static void OnWillCreateAsset(string path)
    {
        path = path.Replace(".meta", "");
        int index = path.LastIndexOf(".");
        string file = path.Substring(index);
        if (file != ".cs" && file != ".js" && file != ".boo") return;
        string fileExtension = file;

        index = Application.dataPath.LastIndexOf("Assets");
        path = Application.dataPath.Substring(0, index) + path;
        file = System.IO.File.ReadAllText(path);

        file = file.Replace("#CREATIONDATE#", System.DateTime.Now.ToString("d"));
        file = file.Replace("#PROJECTNAME#", PlayerSettings.productName);
        file = file.Replace("#SMARTDEVELOPERS#", PlayerSettings.companyName);
        file = file.Replace("#FILEEXTENSION#", fileExtension);

        System.IO.File.WriteAllText(path, file);
        AssetDatabase.Refresh();
    }
#endif
}

三、设置PlayerSettings的属性

点击Edit/Project Settings/Player,修改Company Name为所需要的名字。

OK,你也可以自己根据需求自己修改了。

4.注意点

·创建脚本时请保证控制台没有报错,如果有脚本报错,会导致解析脚本无法使用

最后修改:2023 年 11 月 12 日
如果觉得我的文章对你有用,请随意赞赏