public static void SetupForTagger() { //Setup the current tags List <EditorTag> tagsToAdd = new List <EditorTag>(); for (int i = 0; i < MultiTagManager.EditorTags.Count(); i++) { tagsToAdd.Add(MultiTagManager.EditorTags[i]); } //Loop trough all game objects in the scene object[] obj = GameObject.FindObjectsOfType(typeof(GameObject)); foreach (object o in obj) { //Make sure they have a tagger component attached GameObject g = (GameObject)o; TagFrenzyList comp = (TagFrenzyList)g.GetComponent(typeof(TagFrenzyList)); if (comp == null) { comp = g.AddComponent <TagFrenzyList>(); } //Find any tags on each current object string tag = g.tag; if (tag != null && tag.ToLower() != "untagged") { //Does the tag exist in our global list of tags? If not, add it. if (!MultiTagManager.TagNameMatch(tag)) { EditorTag et = new EditorTag(); et.Id = "0"; et.Tag = tag; tagsToAdd.Add(et); MultiTagManager.Update(tagsToAdd); } //Add it to the local tagger script g.AddTag(g.tag); } } AssetDatabase.Refresh(ImportAssetOptions.ForceUpdate); }
/// <summary> /// Find game objects that match the given tags /// </summary> /// <param name="tags">A string list of tags to match</param> /// <param name="tagMatchType">Match tags by "Or" (any of the given tags can be selected on the object) or by "And" (all of the given tags must be selected on the object)</param> /// <returns>A list of matching game objects</returns> public static List <GameObject> FindGameObjectsWithTags(List <string> tags, TagMatch tagMatchType) { TagFrenzyList[] tagged = GameObject.FindObjectsOfType <TagFrenzyList>(); List <GameObject> matches = new List <GameObject>(); //Look through all objects with a tagger attached for (int i = 0; i < tagged.Length; i++) { TagFrenzyList obj = tagged[i]; if (tagMatchType == TagMatch.Or) { //Look through all the entered tags, and see any of them match for (int j = 0; j < tags.Count; j++) { if (obj.SelectedEditorTags.Where(et => et.Tag.ToLower().Trim() == tags[j].ToLower().Trim()).Any()) { //If so, return it matches.Add(obj.gameObject); break; } } } else if (tagMatchType == TagMatch.And) { //Look through all the entered tags, and see if all of them match bool addMatch = true; for (int j = 0; j < tags.Count; j++) { if (!obj.SelectedEditorTags.Where(et => et.Tag.ToLower().Trim() == tags[j].ToLower().Trim()).Any()) { addMatch = false; } } if (addMatch) { matches.Add(obj.gameObject); } } else if (tagMatchType == TagMatch.Not) { bool addMatch = true; for (int j = 0; j < tags.Count; j++) { if (obj.SelectedEditorTags.Where(et => et.Tag.ToLower().Trim() == tags[j].ToLower().Trim()).Any()) { addMatch = false; } } if (addMatch) { matches.Add(obj.gameObject); } } else if (tagMatchType == TagMatch.Exact) { bool addMatch = true; int matchCount = 0; for (int j = 0; j < tags.Count; j++) { //Make sure all the tags match if (!obj.SelectedEditorTags.Where(et => et.Tag.ToLower().Trim() == tags[j].ToLower().Trim()).Any()) { addMatch = false; } else { matchCount += 1; } } //And make sure there are only matching tags in the result if (matchCount != obj.SelectedEditorTags.Count()) { addMatch = false; } if (addMatch) { matches.Add(obj.gameObject); } } } return(matches); }