Unity腳本一鍵重命名插件

Unity有個(gè)很煩人的規(guī)則:只要繼承于MonoBehaviour類的腳本必須要達(dá)成文件名與類名相等,然而在Unity編輯器中重命名一個(gè)集成與MonoBehaviour的腳本并不會(huì)重命名腳本里的class name。于是我們經(jīng)常重命名完一個(gè)腳本的文件名,Unity編譯一次;然后進(jìn)入腳本編輯軟件把該腳本的class name重命名保存,Unity再編譯一次。

This is such a waste of time and human resources!

所以我今天忍不住寫了一段一鍵重命名的Editor小腳本分享給大家。

腳本的限制:
a. 該腳本只重命名與文件名相同的類名。
b. 重命名一個(gè)腳本并不會(huì)更新其它腳本對(duì)該腳本名稱的引用。

using UnityEngine;
using UnityEditor;
using System.Text.RegularExpressions;
using System.IO;

[CustomEditor(typeof(MonoScript))]
public class ScriptRenameEditor : Editor
{
    private string fileName = "";
    public override void OnInspectorGUI()
    {
        base.OnInspectorGUI();
        var t = target as MonoScript;
        EditorGUILayout.BeginHorizontal();
        var width = Screen.width * 2f / 3f;
        //顯示文本框讓用戶輸入新的文件名
        fileName = EditorGUILayout.TextField("New Filename", fileName, GUILayout.Width(width));
        if (GUILayout.Button("Rename"))
        {
            //匹配文件名和父類的名字
            var match = Regex.Match(t.text, @"class\s*(?<filename>" + t.name + @")\s*(\s*:\s*(?<basetype>[a-zA-Z0-9\_]+))?\s*\{");
            if (match.Success)
            {
                var name = t.name;
                var index = match.Groups["filename"].Index;
                var finalText = t.text.Substring(0, index) + fileName + t.text.Substring(index + name.Length);
                var filePath = AssetDatabase.GetAssetPath(t);
                Debug.Log("renaming " + filePath);
                //如果不存在父類,或者父類的名字不叫MonoBehaviour就彈窗警告用戶.
                if (match.Groups["basetype"] == null || match.Groups["basetype"].Value != "MonoBehaviour")
                {
                    if (!EditorUtility.DisplayDialog("This script is not derived by MonoBehaviour", "do you really want to rename it?", "yes", "no"))
                    {
                        return;
                    }
                }
                using (var fs = File.Open(filePath, FileMode.Truncate))
                using (var wr = new StreamWriter(fs, System.Text.Encoding.UTF8))
                {
                    wr.Write(finalText);
                }
                var fi = new FileInfo(filePath);
                File.Move(filePath, fi.DirectoryName + Path.DirectorySeparatorChar + fileName + fi.Extension);
                AssetDatabase.Refresh();
                Selection.activeObject = AssetDatabase.LoadAssetAtPath<Object>(filePath.Replace(name, fileName));
            }
            else
            {
                Debug.Log("no class named \"" + t.name + "\" was found.");
            }
        }
        EditorGUILayout.EndHorizontal();
        //顯示文件的預(yù)覽內(nèi)容
        if (t != null)
        {
            var text = t.text;
            if (text.Length > 1000)
                text = text.Substring(0, 1000) + "\n\n\n  ...";
            GUILayout.TextArea(text);
        }
    }

    private void OnEnable()
    {
        var t = target as MonoScript;
        fileName = t.name;
    }
}

最終的效果如下:

Paste_Image.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開(kāi)發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見(jiàn)模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,539評(píng)論 19 139
  • 1、特殊文件夾(unity doc : Special Folders) Unity工程根目錄下,有三個(gè)特殊文件夾...
    小飛不會(huì)飛_閱讀 6,528評(píng)論 2 27
  • 閑下來(lái)的幾天現(xiàn)在就是瘋狂看書模式,感覺(jué)我每天都在吃好多記憶面包似的,決定以后晚上都回去看書背書好了,暖和還可以出聲...
    云時(shí)之間閱讀 302評(píng)論 0 5
  • 熱鬧而有序的候車大廳,跟往日并沒(méi)有什么區(qū)別。安然坐在候車廳的凳子上,心里波濤洶涌。她不是第一次來(lái),也不是第一次有這...
    天然之樂(lè)閱讀 259評(píng)論 0 1
  • 我時(shí)常覺(jué)得自己就是一個(gè)大傻逼,看不懂利害,分不清方向,處理不好人際關(guān)系。 搞不明白,現(xiàn)在怎么會(huì)變成現(xiàn)在這個(gè)樣子呢?...
    阿如花閱讀 682評(píng)論 1 1

友情鏈接更多精彩內(nèi)容