Skip to content

Commit 8d4f8dc

Browse files
committed
v1.4
- Added Custom Inspector for Taggable component - Added Editor support for all runtime API (AddTag, RemoveTag, HasTag, etc) - Renamed TagsContainer to ComposteTag
1 parent d123c4e commit 8d4f8dc

File tree

9 files changed

+325
-90
lines changed

9 files changed

+325
-90
lines changed

Runtime/TagsContainer.cs renamed to Runtime/CompositeTag.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
#if ODIN_INSPECTOR
22
using Sirenix.OdinInspector;
33
#endif
4+
using System;
45
using UnityEngine;
56

67
namespace ToolBox.Tags
78
{
8-
[CreateAssetMenu(menuName = "ToolBox/MultiTags/Tags Container")]
9+
[CreateAssetMenu(menuName = "ToolBox/Tags/Composite Tag")]
910
#if ODIN_INSPECTOR
1011
[AssetSelector, Required]
1112
#endif
12-
public sealed class TagsContainer : ScriptableObject
13+
public sealed class CompositeTag : ScriptableObject
1314
{
14-
[SerializeField] private Tag[] _tags = new Tag[0];
15+
[SerializeField] private Tag[] _tags = Array.Empty<Tag>();
16+
17+
internal Tag[] Tags => _tags;
1518

1619
internal void Add(GameObject instance, int hash)
1720
{
@@ -25,7 +28,9 @@ internal void Remove(GameObject instance, int hash)
2528
_tags[i].Remove(instance, hash);
2629
}
2730

28-
internal bool HasInstance(GameObject instance, bool allRequired) =>
29-
instance.HasTags(_tags, allRequired);
31+
internal bool HasInstance(GameObject instance, bool allRequired)
32+
{
33+
return instance.HasTags(_tags, allRequired);
34+
}
3035
}
3136
}
File renamed without changes.

Runtime/Tag.cs

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,59 +6,53 @@
66

77
namespace ToolBox.Tags
88
{
9-
[CreateAssetMenu(menuName = "ToolBox/MultiTags/Tag")]
9+
[CreateAssetMenu(menuName = "ToolBox/Tags/Tag")]
1010
#if ODIN_INSPECTOR
1111
[AssetSelector, Required]
1212
#endif
1313
public sealed class Tag : ScriptableObject
1414
{
1515
private readonly HashSet<int> _instancesHash = new HashSet<int>();
1616
private readonly List<GameObject> _instances = new List<GameObject>(128);
17-
private readonly List<GameObject> _temp = new List<GameObject>(128);
18-
private IReadOnlyList<GameObject> _activeInstances = null;
1917

2018
internal void Add(GameObject instance, int hash)
2119
{
22-
if (!_instancesHash.Contains(hash))
23-
{
24-
_instances.Add(instance);
25-
_instancesHash.Add(hash);
26-
}
20+
if (_instancesHash.Contains(hash))
21+
return;
22+
23+
_instances.Add(instance);
24+
_instancesHash.Add(hash);
2725
}
2826

2927
internal void Remove(GameObject instance, int hash)
3028
{
31-
if (_instancesHash.Contains(hash))
32-
{
33-
_instances.Remove(instance);
34-
_instancesHash.Remove(hash);
35-
}
29+
if (!_instancesHash.Contains(hash))
30+
return;
31+
32+
_instances.Remove(instance);
33+
_instancesHash.Remove(hash);
3634
}
3735

38-
internal bool HasInstance(int hash) =>
39-
_instancesHash.Contains(hash);
40-
41-
public IReadOnlyList<GameObject> GetInstances()
36+
internal bool HasInstance(int hash)
4237
{
43-
_temp.Clear();
38+
return _instancesHash.Contains(hash);
39+
}
4440

45-
for (int i = _instances.Count - 1; i >= 0; i--)
41+
public IEnumerable<GameObject> GetInstances()
42+
{
43+
int instancesCount = _instances.Count - 1;
44+
for (int i = instancesCount; i >= 0; i--)
4645
{
4746
var instance = _instances[i];
48-
47+
4948
if (instance == null)
5049
{
5150
_instances.RemoveAt(i);
5251
continue;
5352
}
5453

55-
if (instance.activeInHierarchy)
56-
_temp.Add(instance);
54+
yield return instance;
5755
}
58-
59-
_activeInstances = _temp;
60-
61-
return _activeInstances;
6256
}
6357
}
6458
}

Runtime/TagExtensions.cs

Lines changed: 0 additions & 55 deletions
This file was deleted.

Runtime/TagHelper.cs

Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
using UnityEngine;
2+
3+
namespace ToolBox.Tags
4+
{
5+
public static class TagHelper
6+
{
7+
public static void AddTag(this GameObject instance, Tag tag)
8+
{
9+
#if UNITY_EDITOR
10+
var taggable = GetComponent(instance);
11+
if (!taggable.Contains(tag))
12+
taggable.Add(tag);
13+
#endif
14+
15+
tag.Add(instance, instance.GetHashCode());
16+
}
17+
18+
public static void AddTag(this Component instance, Tag tag)
19+
{
20+
instance.gameObject.AddTag(tag);
21+
}
22+
23+
public static void RemoveTag(this GameObject instance, Tag tag)
24+
{
25+
#if UNITY_EDITOR
26+
var taggable = GetComponent(instance);
27+
if (taggable.Contains(tag))
28+
taggable.Remove(tag);
29+
#endif
30+
31+
tag.Remove(instance, instance.GetHashCode());
32+
}
33+
34+
public static void RemoveTag(this Component instance, Tag tag)
35+
{
36+
instance.gameObject.RemoveTag(tag);
37+
}
38+
39+
public static bool HasTag(this GameObject instance, Tag tag)
40+
{
41+
return tag.HasInstance(instance.GetHashCode());
42+
}
43+
44+
public static bool HasTag(this Component instance, Tag tag)
45+
{
46+
return instance.gameObject.HasTag(tag);
47+
}
48+
49+
public static void AddTags(this GameObject instance, Tag[] tags)
50+
{
51+
#if UNITY_EDITOR
52+
var taggable = GetComponent(instance);
53+
#endif
54+
55+
int hash = instance.GetHashCode();
56+
57+
for (int i = 0; i < tags.Length; i++)
58+
{
59+
var tag = tags[i];
60+
61+
#if UNITY_EDITOR
62+
if (!taggable.Contains(tag))
63+
taggable.Add(tag);
64+
#endif
65+
66+
tag.Add(instance, hash);
67+
}
68+
}
69+
70+
public static void AddTags(this Component instance, Tag[] tags)
71+
{
72+
instance.gameObject.AddTags(tags);
73+
}
74+
75+
public static void AddTags(this GameObject instance, CompositeTag composite)
76+
{
77+
#if UNITY_EDITOR
78+
var taggable = GetComponent(instance);
79+
80+
foreach (var tag in composite.Tags)
81+
{
82+
if (taggable.Contains(tag))
83+
continue;
84+
85+
taggable.Add(tag);
86+
}
87+
#endif
88+
89+
composite.Add(instance, instance.GetHashCode());
90+
}
91+
92+
public static void AddTags(this Component instance, CompositeTag composite)
93+
{
94+
instance.gameObject.AddTags(composite);
95+
}
96+
97+
public static void RemoveTags(this GameObject instance, Tag[] tags)
98+
{
99+
#if UNITY_EDITOR
100+
var taggable = GetComponent(instance);
101+
#endif
102+
103+
int hash = instance.GetHashCode();
104+
105+
for (int i = 0; i < tags.Length; i++)
106+
{
107+
var tag = tags[i];
108+
109+
#if UNITY_EDITOR
110+
if (taggable.Contains(tag))
111+
taggable.Remove(tag);
112+
#endif
113+
114+
tag.Remove(instance, hash);
115+
}
116+
}
117+
118+
public static void RemoveTags(this Component instance, Tag[] tags)
119+
{
120+
instance.gameObject.RemoveTags(tags);
121+
}
122+
123+
public static void RemoveTags(this GameObject instance, CompositeTag composite)
124+
{
125+
#if UNITY_EDITOR
126+
var taggable = GetComponent(instance);
127+
128+
foreach (var tag in composite.Tags)
129+
{
130+
if (!taggable.Contains(tag))
131+
continue;
132+
133+
taggable.Remove(tag);
134+
}
135+
#endif
136+
137+
composite.Remove(instance, instance.GetHashCode());
138+
}
139+
140+
public static void RemoveTags(this Component instance, CompositeTag composite)
141+
{
142+
instance.gameObject.RemoveTags(composite);
143+
}
144+
145+
public static bool HasTags(this GameObject instance, Tag[] tags, bool allRequired)
146+
{
147+
int hash = instance.GetHashCode();
148+
149+
for (int i = 0; i < tags.Length; i++)
150+
{
151+
if (tags[i].HasInstance(hash) == !allRequired)
152+
return !allRequired;
153+
}
154+
155+
return allRequired;
156+
}
157+
158+
public static bool HasTags(this Component instance, Tag[] tags, bool allRequired)
159+
{
160+
return instance.gameObject.HasTags(tags, allRequired);
161+
}
162+
163+
public static bool HasTags(this GameObject instance, CompositeTag composite, bool allRequired)
164+
{
165+
return composite.HasInstance(instance, allRequired);
166+
}
167+
168+
public static bool HasTags(this Component instance, CompositeTag composite, bool allRequired)
169+
{
170+
return instance.gameObject.HasTags(composite, allRequired);
171+
}
172+
173+
#if UNITY_EDITOR
174+
private static Taggable GetComponent(GameObject instance)
175+
{
176+
if (!instance.TryGetComponent(out Taggable taggable))
177+
taggable = instance.AddComponent<Taggable>();
178+
179+
return taggable;
180+
}
181+
#endif
182+
}
183+
}
File renamed without changes.

0 commit comments

Comments
 (0)