Unity 用代码自动创建标签

using UnityEditor;
using UnityEngine ;
public class addTag:MonoBehaviour 
{
public GameObject o;//被修改的物体
void Start(){
AddTag("MXLWD",o);
}
//添加tag标签
void AddTag(string tag,GameObject obj)
    {
    if(!isHasTag(tag))
    {
        SerializedObject tagManager = new SerializedObject(obj);//序列化物体
        SerializedProperty it = tagManager.GetIterator();//序列化属性
        while (it.NextVisible(true))//下一属性的可见性
        {
                if(it.name == "m_TagString")
            {
                    Debug .Log (it.stringValue );
                    it.stringValue =tag;
                    tagManager.ApplyModifiedProperties();
                }
            }
        }
    }
bool isHasTag(string tag)
{
    for (int i = 0; i < UnityEditorInternal.InternalEditorUtility.tags.Length; i++) {
        if (UnityEditorInternal.InternalEditorUtility.tags[i].Equals(tag))
            return true;
    }
    return false;
}
}
  • 1

猜你喜欢

转载自blog.csdn.net/martins1994/article/details/80930851