当需要查找场景中某个物体时,可能会使用GameObject.FindGameObjectsWithTag()方法。但是该方法存在一个弊端,如果当前项目的标签库中不存在写入的标签就会报错。此时可以通过以下方法先进行是否存在某标签的判断,在使用该标签进行查找物体。
using UnityEditorInternal;
using System;
/// <summary>
/// 检查该项目中是否存在某标签
/// </summary>
/// <param name="tag"></param>
/// <returns></returns>
private static bool IsHasTag(string tag)
{
string[] tags = InternalEditorUtility.tags;
if (Array.IndexOf(tags, tag) >= 0) return true;
else return false;
}
层标记亦可用此方法进行判断。
更正!!!
该方法使用了UnityEditorInternal命名空间下的方法,只能在编译器中使用,发布时会报错。