Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
Help us understand the problem. What is going on with this article?

【Unity拡張】ヒエラルキーで選択しているオブジェクトを正規表現で一括リネームするツールを作ってみた

More than 3 years have passed since last update.

はじめに

ヒエラルキー上のオブジェクトを手作業でリネームしていてよく思うのは


「めんどくさい!」



ある時、「正規表現でリネームすることができれば少しは楽ができるのではないか?」と思いついたので、ツールをサクッと作ってみました。

環境

Unity 5.5.0f3
Windows 10

作ったものについて

ヒエラルキー上で選択しているオブジェクトをRegexで一括リネームするエディターウィンドウを作ってみました。

regex_delete_sample.gif

ソースコード

UnityプロジェクトにEditorフォルダを作成し、以下のスクリプトをEditorフォルダ内へ入れてください

HierarchyRegexRenamer.cs
namespace HierarchyRegexRenamer
{
    using UnityEngine;
    using UnityEditor;
    using System.Linq;
    using System.Text.RegularExpressions;

    public class HierarchyRegexRenamer : EditorWindow
    {
        [SerializeField] string pattern = ""; // Regexパターン
        [SerializeField] string replacement = ""; // 置換文字列

        // Hierarchy上で選択しているオブジェクトをリネームする
        void DoRename()
        {
            var gameObjects = Selection.gameObjects.Where(go => !AssetDatabase.IsMainAsset(go)).ToArray(); // リネーム対象のGameObject

            // Undoに登録
            Undo.RecordObjects(gameObjects, "Regex Rename");

            // 名前を変える
            foreach (var go in gameObjects)
            {
                go.name = Regex.Replace(go.name, this.pattern, this.replacement);
            }
        }

        // EditorWindowの描画処理
        void OnGUI()
        {
            EditorGUILayout.LabelField("Hierarchy上で選択しているオブジェクトをリネームします");
            GUILayout.Space(2f);

            this.pattern = EditorGUILayout.TextField("Regex", this.pattern);
            this.replacement = EditorGUILayout.TextField("Replacement", this.replacement);

            EditorGUI.BeginDisabledGroup(Selection.gameObjects.Length == 0);

            // ボタンを表示
            if (GUILayout.Button("リネーム"))
            {
                this.DoRename();
            }

            EditorGUI.EndDisabledGroup();
        }

        // ウィンドウを開く
        [MenuItem("EditorWindow/Hierarchy Regex Renamer")]
        static void Open()
        {
            GetWindow<HierarchyRegexRenamer>();
        }
    }
}

ツールを使う

画面上部のメニューの EditorWindow/Hierarchy Regex Renamer を選択するとウィンドウが開きます。
image

あとはお望みのRegexと置換文字列を入力して、リネームボタンを押すだけです。

Regexのサンプル

これは使えるかもって思ったRegexパターンをいくつか載せてみました。

1. 半角スペースと全角スペースを消す

Regex = \s
Replacement には何も指定しません

regex_delete_space.gif

2. ()で囲まれた半角数字を()ごと消す

Regex = \(\d+\)
Replacement には何も指定しません

GameObject(2)の場合、 (2)が消えて GameObject になります。

regex_delete_number_and_bracket.gif

3. (半角数字)の()の部分だけを消す

Regex = \((\d+)\)
Replacement = $1

GameObject(HOGE) (2) の場合、 (2)の()が消えて GameObject(HOGE) 2 になります。

regex_delete_bracket.gif

4. 名前全体を()で括弧で囲む

Regex = ^.*
Replacement = ($0)

$0を使うことでRegexにマッチした文字列を使うことができるという正規表現の機能を利用しています。

regex_add_bracket.gif

今後の課題

正規表現だけでは実現が難しい機能もあります。例えば以下のような機能
・選択しているオブジェクトに連番をふる

特殊な文字を用意して、リネーム時にそこを連番に置き換えるという処理を入れれば実現できそうですが...

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away