Unity3D 建立指令碼自動新增模板註釋作者資訊等
阿新 • • 發佈:2018-11-08
首先找到Unity安裝目錄下的(注意:修改的是Unity指令碼模板而不是vs或者momo裡的)
“Unity\Editor\Data\Resources\ScriptTemplates ”
開啟“81-C# Script-NewBehaviourScript.cs”檔案,如下:
using UnityEngine; using System.Collections; /* ********************************************************************* Copyright (C) 2018 The Company Name File Name: #SCRIPTNAME#.cs Author: #AuthorName CreateTime: #CreateTime ********************************************************************* */ public class #SCRIPTNAME# : MonoBehaviour { // Use this for initialization void Start () { } // Update is called once per frame void Update () { } }
#SCRIPTNAME#,這個巨集會跟隨我們建立的指令碼名變化,這個功能也是我們自定義指令碼模板的重要功能,#SCRIPTNAME#是Unity自定義的一個巨集,所以你想新增時間,必須也要自定義。如果修改本地檔案的話,每次建立新專案都會生成這個新指令碼模板,同時還要在專案裡修改新增時間自定義。模板只新增到專案裡的方法:該指令碼需放到Editor資料夾
using UnityEngine; using System.Collections; using System.IO; /* ********************************************************************* Copyright (C) 2018 The Company Name File Name: SpriteTitleChange.cs Author: #AuthorName CreateTime: #CreateTime ********************************************************************* */ public class SpriteTitleChange : UnityEditor.AssetModificationProcessor { private static void OnWillCreateAsset(string path) { path = path.Replace(".meta", ""); if(path.EndsWith(".cs")) { string allText = File.ReadAllText(path); allText = allText.Replace("#AuthorName", "mwt") .Replace("#CreateTime", System.DateTime.Now.Year + "/" + System.DateTime.Now.Month + "/" + System.DateTime.Now.Day + " " + System.DateTime.Now.Hour + ":" + System.DateTime.Now.Minute + ":" + System.DateTime.Now.Second); File.WriteAllText(path, allText); } } }