Skip to content

Instantly share code, notes, and snippets.

@michidk
Created March 21, 2016 13:41
Show Gist options
  • Save michidk/947454f35196f8a86881 to your computer and use it in GitHub Desktop.
Save michidk/947454f35196f8a86881 to your computer and use it in GitHub Desktop.
Unity3D: a class which checks if a tag is defined. also could create new ones or remove them.
public static class TagManager
{
private const string TAG_MANAGER_PATH = "ProjectSettings/TagManager.asset";
public static bool ContainsTag(string tag)
{
SerializedProperty prop;
if (GetAssetDatabase(out prop))
{
for (int i = 0; i < prop.arraySize; i++)
{
var element = prop.GetArrayElementAtIndex(i);
if (element.stringValue.Equals(tag))
{
return true;
}
}
}
return false;
}
private static bool GetAssetDatabase(out SerializedProperty prop)
{
var db = AssetDatabase.LoadAllAssetsAtPath(TAG_MANAGER_PATH);
if (db != null && db.Length > 0)
{
var so = new SerializedObject(db[0]);
prop = so.FindProperty("tags");
return prop != null && prop.arraySize > 0;
}
prop = null;
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment