Version: Unity 6.7 Alpha (6000.7)
LanguageEnglish
  • C#

ObjectNames.GetUniqueObjectName

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Declaration

public static string GetUniqueObjectName(List<Object> existingObjects, string name);

Parameters

Parameter Description
existingObjects A list of existing objects whose names define the set of already-taken names.
name Desired name to use as-is, or as a base for a unique name.

Returns

string A name not used by any object in existingObjects.

Description

Returns a unique name using the provided name as a base, derived from the names of a list of existing objects.

If the provided name matches the name of any object in existingObjects, a unique name is generated by appending the next available numerical increment.

Use this method instead of ObjectNames.GetUniqueName when you already hold a List<Object> of existing objects. It avoids allocating a temporary string array, making it suitable for allocation-sensitive contexts such as large-hierarchy operations and frequently called Editor tools.

Additional resources: ObjectNames.GetUniqueName, GameObjectUtility.GetUniqueNameForSibling, GameObjectUtility.EnsureUniqueNameForSibling.

using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

public class ExampleClass { public void Example() { // Build a list of objects without extracting their names into a string array. var existingObjects = new List<Object> { new GameObject("Object"), new GameObject("Thing"), new GameObject("Thing (1)") };

// Displays "Object (1)" Debug.Log(ObjectNames.GetUniqueObjectName(existingObjects, "Object"));

// Displays "Thing (2)" Debug.Log(ObjectNames.GetUniqueObjectName(existingObjects, "Thing"));

// Displays "Other" Debug.Log(ObjectNames.GetUniqueObjectName(existingObjects, "Other"));

foreach (Object obj in existingObjects) Object.DestroyImmediate(obj); } }
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961