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 3 years have passed since last update.

【Unity】GameObjectに自動で連番の名前をつけるエディタ拡張

Last updated at Posted at 2021-11-09

概要

Hierarchy View から右クリックでGameObjectに自動で連番の名前をつけるエディタ拡張を作成しました。

AAA
BBB
CCC

みたいなゲームオブジェクトを複数選択で

AAA
AAA (1)
AAA (2)

というUnityの複製(Deplicate)に即した名前に自動で変更してくれます。

配置作業をしていると

AAA
AAA (13)
AAA (2)
AAA (4)
AAA (1)

みたいな感じで名前がバラついてきてイライラすることありますよね。
あります。

機能

ばんごうをふってくれる

使い方

  • ScriptableObjectCreator.csをAssets/Editorに入れてください
  • EditメニューにNameConsecutiveNumberが追加され、これを選択すると勝手に番号を振ります。大体御坂妹みたいな感じです。
ScriptableObjectCreator.cs
using UnityEngine;
using UnityEditor;
using System.Text.RegularExpressions;
using System.Linq;
using System.Collections.Generic;

public class NameConsecutiveNumberEditor : Editor
{
    [MenuItem("Edit/NameConsecutiveNumber", false)]
    static void SetNames()
    {
        Undo.RecordObjects(Selection.objects, "ApplyNameConsecutiveNumber");

        string baseName = string.Empty;
        foreach (var item in Selection.objects.Select((value, index) => new { value, index }))
        {
            if(item.index == 0)
			{
                var fullName = item.value.name;

                baseName = fullName;

                // 末尾の(n)を削除.
                Regex regex = new Regex(@"\(.+?\)");
                var matches = regex.Matches(fullName);
                if (matches != null)
                {
                    var matchList = new List<string>();
                    foreach (var match in matches)
                    {
                        matchList.Add(match.ToString());
                    }

                    if (matchList.Any())
                    {
                        var lastMatch = matchList.Last();
                        int lastIndex = fullName.LastIndexOf(lastMatch);
                        baseName = fullName.Remove(lastIndex);
                    }
                }

                // 末尾の空白を削除.
                for (int i = baseName.Length - 1; i >= 0; i--)
                {
                    var c = baseName[i];
                    if (!char.IsWhiteSpace(c))
                    {
                        if (i != baseName.Length - 1)
                        {
                            baseName = baseName.Remove(i + 1);
                        }
                        break;
                    }
                }

                item.value.name = baseName;
            }
			else
			{
                // 名前を形成.
                item.value.name = string.Format("{0} ({1})", baseName, item.index);
            }
		}
    }
}

補足

あんまりないと思いますが

AAA (13) (たなか) (5)
AAA (2) (6)
AAA (4) (2)
AAA (1)

みたいな状態で使うと

AAA (13) (たなか)
AAA (13) (たなか) (1)
AAA (13) (たなか) (2)
AAA (13) (たなか) (3)

という感じで整理してくれるように作りました(説明雑)
要はいっちばん後ろの(n)だけを変更します。

ご拝読、ありがとうございました。

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?