Created
March 21, 2016 13:41
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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