0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Hierarchy上のオブジェクトの名前を一括変換するやつ

Last updated at Posted at 2019-07-26

tool.png

HierarchyRenamer.cs

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

public class HierarchyRenamer : EditorWindow
{

    [SerializeField]
    string pattern = "\\((\\d+)\\)"; // Regexパターン (半角数字)の()の部分だけを消す
    [SerializeField]
    string replacement = "$1"; // 置換文字列




    [SerializeField]
    string number = "31";


    // 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);
        }
    }
    //連番追加
    void AddNumber()
    {
        
         

        foreach (GameObject obj in Selection.gameObjects)
        {
            Undo.RegisterCompleteObjectUndo(obj, "Undo Add Number");

            int sibling = obj.transform.GetSiblingIndex();

            obj.name = string.Format("{0}{1}", obj.name + " (", (int.Parse(number) + sibling).ToString()+")");
        }
    }

    void OnGUI()
    {
        EditorGUILayout.LabelField("正規表現で名前変更と連番を追加がボタン押すだけでできます。");
        GUILayout.Space(2f);
        EditorGUILayout.LabelField("半角スペースと全角スペースを消す: \\s");
        GUILayout.Space(2f);
        EditorGUILayout.LabelField("(半角数字)の()の部分だけを消す: \\((\\d +)\\)");
        EditorGUILayout.LabelField("                                    $1");
        GUILayout.Space(2f);

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


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



        this.number = EditorGUILayout.TextField("Start Number", this.number);
        

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

        // ボタンを表示
        if (GUILayout.Button("連番を追加"))
        {
            this.AddNumber();
        }

        EditorGUI.EndDisabledGroup();
    }

    [MenuItem("Tools/Hierarchy上のオブジェクト名を一括変更", false, 10000)]
    static void Open()
    {
        GetWindow<HierarchyRenamer>();
    }

}

作ってみたけど結局ez renameとかいうアセットでいいかも

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?