動的にtagを追加する

  • 使い方
AddTag("HogeTag01");

すでにあればスルーする。
動的追加なので、プログラムが終了すると消えている。
初期状態で、unityのexportPackageはtag情報を出してくれないので、
動的に追加するか、tagを含んだexportをする必要がある。
scriptで追加しておけば、色々考えずに済む。

  • 追加メソッド
static void AddTag(string tagname) {
       UnityEngine.Object[] asset = AssetDatabase.LoadAllAssetsAtPath("ProjectSettings/TagManager.asset");
       if ((asset != null) && (asset.Length > 0)) {
           SerializedObject so = new SerializedObject(asset[0]);
           SerializedProperty tags = so.FindProperty("tags");

           for (int i = 0; i < tags.arraySize; ++i) {
               if (tags.GetArrayElementAtIndex(i).stringValue == tagname) {
                   return;
               }
           }

           int index = tags.arraySize;
           tags.InsertArrayElementAtIndex(index);
           tags.GetArrayElementAtIndex(index).stringValue = tagname;
           so.ApplyModifiedProperties();
           so.Update();
       }
   }

タグ:

sample
最終更新:2015年06月21日 19:00