Version: 2020.2
public static string GetUniqueName (string[] existingNames, string name);

参数

existingNames 预先存在的名称列表。
name 要按原样或以其为基础使用的名称。

返回

string 未包含在预先存在的名称列表中的名称。

描述

根据提供的名称创建唯一名称。

如果目标名称位于提供的现有名称列表中,则通过附加下一个可用数值增量来生成唯一名称。

using UnityEngine;
using UnityEditor;

public class ExampleClass { public void Example() { string[] names = new string[] { "Object", "Thing", "Thing (1)" };

// will display "Object (1)" Debug.Log(ObjectNames.GetUniqueName(names, "Object"));

// will display "Thing (2)" Debug.Log(ObjectNames.GetUniqueName(names, "Thing"));

// will display "Other" Debug.Log(ObjectNames.GetUniqueName(names, "Other")); } }