Unity在编辑器中添加标签tags

没有添加标签时初始状态

接下来我们编辑ControlAddtag代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
public class ControlAddtag {

    private static string[] tags = new string[]
  {
        "Terrain",
        "Cloud",
        "Shore",
  };

    [MenuItem("TagController/AddTag")]
    public static void AddTag()
    {
        //打开标签管理器
        SerializedObject tagManager = new SerializedObject(AssetDatabase.LoadAllAssetsAtPath("ProjectSettings/TagManager.asset")[0]);
        //找到标签属性
        SerializedProperty tagsProp = tagManager.FindProperty("tags");
        Debug.LogError("TagsPorp Size:" + tagsProp.arraySize);
        tagsProp.ClearArray();
        tagManager.ApplyModifiedProperties();
        Debug.LogError("TagsPorp Size现在:" + tagsProp.arraySize);
        for (int i = 0; i < tags.Length; i++)
        {
            // 在标签属性数组中指定的索引处插入一个空元素。
            tagsProp.InsertArrayElementAtIndex(i);
            //得到数组中指定索引处的元素
            SerializedProperty sp = tagsProp.GetArrayElementAtIndex(i);
            //给标签属性数组中刚才插入的空元素赋值
            sp.stringValue = tags[i];
            //添加完之后应用一下
            tagManager.ApplyModifiedProperties();
        }

    }

}

点击按钮结果如下:

感谢这篇文章:https://blog.csdn.net/qq_36848370/article/details/93505171

发布了122 篇原创文章 · 获赞 13 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/qq_40229737/article/details/103412047