Skip to content

Instantly share code, notes, and snippets.

@nicoplv
Last active December 8, 2023 20:22
Show Gist options
  • Save nicoplv/df2d1064be62cf894b202b33d050d54f to your computer and use it in GitHub Desktop.
Save nicoplv/df2d1064be62cf894b202b33d050d54f to your computer and use it in GitHub Desktop.
A little script to get a clean name after a duplication or a copy of a gameobject in your unity scene. Drag and drop it into an editor folder !
using System.Text.RegularExpressions;
using UnityEditor;
using UnityEngine;
[InitializeOnLoad]
public class CleanDuplicateName
{
static bool eventDuplicateAppend;
static CleanDuplicateName()
{
EditorApplication.hierarchyWindowItemOnGUI += (int instanceID, Rect selectionRect) =>
{
if(Event.current != null && Event.current.type == EventType.ExecuteCommand && (Event.current.commandName == "Duplicate" || Event.current.commandName == "Paste"))
eventDuplicateAppend = true;
};
EditorApplication.hierarchyChanged += () =>
{
if(eventDuplicateAppend)
{
foreach (GameObject iGamebject in Selection.gameObjects)
iGamebject.name = Regex.Replace(iGamebject.name, @"(.*)\s\([0-9]*\)", @"$1");
}
eventDuplicateAppend = false;
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment