-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMultiTags.cs
176 lines (142 loc) · 4.47 KB
/
MultiTags.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*
* ========================================================
*
* MultiTags.cs
* MultiTags v1.0
*
* Created by Aiden De Loryn on 06/05/2015.
*
* ========================================================
*
* - (static) taggedObjects: List<MultiTags>
* + tags: Tag[]
*
* - (static) GetMultiTagsComponent(GameObject): MultiTags
* + (static) GameObjectHasTag(GameObject, Tag): bool
* + (static) TagGameObject(GameObject, Tag): void
* + (static) UntagGameObject(GameObject, Tag): void
* + (static) FindGameObjectWithTag(Tag): GameObject
* + (static) FindGameObjectWithTags(Tag[]): GameObject
* + (static) FindGameObjectsWithTag(Tag): GameObject[]
* + (static) FindGameObjectsWithTags(Tag[]): GameObject[]
*
* ========================================================
*
* ========================================================
*/
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public enum Tag {
MainCamera,
Player,
Enemy,
Pickup,
Bullet
// ADD TAGS HERE
}
public class MultiTags : MonoBehaviour {
static private List<MultiTags> taggedObjects = new List<MultiTags>();
public Tag[] tags = new Tag[0];
void Awake () {
taggedObjects.Add (this);
}
void OnDestroy() {
for (int i = 0; i < taggedObjects.Count; i++) {
if(taggedObjects[i].Equals(this)) {
taggedObjects.RemoveAt(i);
return;
}
}
}
private static MultiTags GetMultiTagsComponent(GameObject gameObject) {
MultiTags multiTags = gameObject.GetComponent<MultiTags>();
if (multiTags == null) {
multiTags = gameObject.AddComponent<MultiTags>();
}
return multiTags;
}
public static bool GameObjectHasTag(GameObject gameObject, Tag searchTag) {
bool hasTag = false;
MultiTags multiTags = GetMultiTagsComponent(gameObject);
foreach (Tag objectTag in multiTags.tags) {
if (objectTag == searchTag) {
hasTag = true;
break;
}
}
return hasTag;
}
public static void TagGameObject(GameObject gameObject, Tag newTag) {
MultiTags multiTags = GetMultiTagsComponent(gameObject);
Tag[] newTags = new Tag[multiTags.tags.Length + 1];
for (int i = 0; i < multiTags.tags.Length; i++) {
newTags[i] = multiTags.tags[i];
}
newTags [multiTags.tags.Length] = newTag;
multiTags.tags = newTags;
}
public static void UntagGameObject(GameObject gameObject, Tag target) {
MultiTags multiTags = GetMultiTagsComponent(gameObject);
List<Tag> newTags = new List<Tag> (multiTags.tags);
while (GameObjectHasTag(gameObject, target)) {
newTags.Remove (target);
multiTags.tags = newTags.ToArray ();
}
}
public static GameObject FindGameObjectWithTag(Tag searchTag) {
GameObject[] searchResult = FindGameObjectsWithTag(searchTag);
if (searchResult.Length > 1) {
Debug.LogWarning("WARNING: MultiTags.FindGameObjectWithTag(Tag) found more than 1 GameObject.");
} else if (searchResult.Length < 1) {
Debug.LogError ("ERROR: MultiTags.FindGameObjectWithTag(Tag) didn't find a GameObject with MultiTag '" + searchTag.ToString() + "'.");
return new GameObject();
}
return searchResult[0];
}
public static GameObject FindGameObjectWithTags(Tag[] searchTags) {
GameObject[] searchResult = FindGameObjectsWithTags(searchTags);
if (searchResult.Length > 1) {
Debug.LogWarning ("WARNING: MultiTags.FindGameObjectWithTags(Tag[]) found more than 1 GameObject.");
} else if (searchResult.Length < 1) {
Debug.LogError ("ERROR: MultiTags.FindGameObjectWithTags(Tag[]) didn't find a GameObject.");
return new GameObject();
}
return searchResult[0];
}
public static GameObject[] FindGameObjectsWithTag(Tag searchTag) {
List<GameObject> searchResult = new List<GameObject> ();
foreach (MultiTags taggedObject in taggedObjects) {
for(int i = 0; i < taggedObject.tags.Length; i++) {
if(taggedObject.tags[i] == searchTag) {
searchResult.Add(taggedObject.gameObject);
break;
}
}
}
return searchResult.ToArray();
}
public static GameObject[] FindGameObjectsWithTags(Tag[] searchTags) {
List<GameObject> searchResult = new List<GameObject> ();
foreach (MultiTags taggedObject in taggedObjects) {
bool tagsFound = true;
foreach(Tag searchTag in searchTags) {
bool tagFound = false;
foreach(Tag objectTag in taggedObject.tags) {
if(objectTag == searchTag) {
tagFound = true;
break;
}
}
if (!tagFound) {
tagsFound = false;
break;
}
}
if (tagsFound) {
searchResult.Add(taggedObject.gameObject);
}
}
return searchResult.ToArray();
}
}