Skip to content

Commit 78482be

Browse files
committed
Debug Improvments
Now object inspector will show added and removed tags when game is running. You can also now add and remove tags via inspector when game is running.
1 parent 2d3fb9f commit 78482be

File tree

7 files changed

+110
-13
lines changed

7 files changed

+110
-13
lines changed

Tag.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace ToolBox.Tags
66
{
7-
[CreateAssetMenu(menuName = "ToolBox/Tagging/Tag"), AssetSelector]
7+
[CreateAssetMenu(menuName = "ToolBox/Tagging/Tag"), AssetSelector, Required]
88
public sealed class Tag : ScriptableObject
99
{
1010
[ShowInInspector, ReadOnly] private HashSet<int> _entities = new HashSet<int>();

TagExtensions.cs

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,75 @@ namespace ToolBox.Tags
44
{
55
public static class TagExtensions
66
{
7-
public static void AddTag(this GameObject entity, Tag tag) =>
7+
public static void AddTag(this GameObject entity, Tag tag)
8+
{
89
tag.Add(entity.GetHashCode());
910

10-
public static void RemoveTag(this GameObject entity, Tag tag) =>
11+
#if UNITY_EDITOR
12+
var taggable = entity.GetComponent<Taggable>();
13+
14+
if (taggable == null)
15+
taggable = entity.AddComponent<Taggable>();
16+
17+
taggable.Add(tag);
18+
#endif
19+
}
20+
21+
public static void RemoveTag(this GameObject entity, Tag tag)
22+
{
1123
tag.Remove(entity.GetHashCode());
1224

25+
#if UNITY_EDITOR
26+
var taggable = entity.GetComponent<Taggable>();
27+
28+
if (taggable == null)
29+
taggable = entity.AddComponent<Taggable>();
30+
31+
taggable.Remove(tag);
32+
#endif
33+
}
34+
1335
public static bool HasTag(this GameObject entity, Tag tag) =>
1436
tag.HasEntity(entity.GetHashCode());
1537

1638
public static void AddTags(this GameObject entity, Tag[] tags)
1739
{
1840
int hash = entity.GetHashCode();
41+
#if UNITY_EDITOR
42+
var taggable = entity.GetComponent<Taggable>();
43+
44+
if (taggable == null)
45+
taggable = entity.AddComponent<Taggable>();
46+
#endif
1947

2048
for (int i = 0; i < tags.Length; i++)
21-
tags[i].Add(hash);
49+
{
50+
var tag = tags[i];
51+
tag.Add(hash);
52+
#if UNITY_EDITOR
53+
taggable.Add(tag);
54+
#endif
55+
}
2256
}
2357

2458
public static void RemoveTags(this GameObject entity, Tag[] tags)
2559
{
2660
int hash = entity.GetHashCode();
61+
#if UNITY_EDITOR
62+
var taggable = entity.GetComponent<Taggable>();
63+
64+
if (taggable == null)
65+
taggable = entity.AddComponent<Taggable>();
66+
#endif
2767

2868
for (int i = 0; i < tags.Length; i++)
29-
tags[i].Remove(hash);
69+
{
70+
var tag = tags[i];
71+
tag.Remove(hash);
72+
#if UNITY_EDITOR
73+
taggable.Remove(tag);
74+
#endif
75+
}
3076
}
3177

3278
public static bool HasTags(this GameObject entity, Tag[] tags, bool allRequired)

Taggable.cs

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,30 @@
11
using Sirenix.OdinInspector;
2+
#if UNITY_EDITOR
3+
using UnityEditor;
4+
#endif
25
using UnityEngine;
36

47
namespace ToolBox.Tags
58
{
6-
[DisallowMultipleComponent, DefaultExecutionOrder(-95)]
9+
[DisallowMultipleComponent]
710
public sealed class Taggable : MonoBehaviour
811
{
9-
[SerializeField, Required] private Tag[] _tags = default;
12+
[SerializeField, Required, AssetList] private Tag[] _tags = default;
1013

14+
private Tag[] _all = null;
1115
private int _hash = 0;
1216

13-
private void Awake() =>
17+
private void Awake()
18+
{
19+
#if UNITY_EDITOR
20+
_all = Resources.FindObjectsOfTypeAll<Tag>();
21+
22+
if (_tags == null)
23+
_tags = new Tag[0];
24+
#endif
25+
1426
_hash = gameObject.GetHashCode();
27+
}
1528

1629
private void OnEnable()
1730
{
@@ -24,6 +37,42 @@ private void OnDisable()
2437
for (int i = 0; i < _tags.Length; i++)
2538
_tags[i].Remove(_hash);
2639
}
40+
41+
#if UNITY_EDITOR
42+
public void Add(Tag tag)
43+
{
44+
if (!ArrayUtility.Contains(_tags, tag))
45+
ArrayUtility.Add(ref _tags, tag);
46+
}
47+
48+
public void Remove(Tag tag)
49+
{
50+
if (ArrayUtility.Contains(_tags, tag))
51+
ArrayUtility.Remove(ref _tags, tag);
52+
}
53+
54+
private void OnValidate()
55+
{
56+
if (!Application.isPlaying || _all == null)
57+
return;
58+
59+
var obj = gameObject;
60+
61+
foreach (var tag in _all)
62+
{
63+
if (ArrayUtility.Contains(_tags, tag))
64+
{
65+
if (!obj.HasTag(tag))
66+
obj.AddTag(tag);
67+
}
68+
else
69+
{
70+
if (obj.HasTag(tag))
71+
obj.RemoveTag(tag);
72+
}
73+
}
74+
}
75+
#endif
2776
}
2877
}
2978

Tags Containers/New Tags Container.asset renamed to Tags Containers/Actors.asset

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ MonoBehaviour:
1010
m_Enabled: 1
1111
m_EditorHideFlags: 0
1212
m_Script: {fileID: 11500000, guid: 50e5bc16d2579bc4eaf839cd7c3b08b2, type: 3}
13-
m_Name: New Tags Container
13+
m_Name: DamageableByPlayer
1414
m_EditorClassIdentifier:
15-
_tags: []
15+
_tags:
16+
- {fileID: 11400000, guid: d4e5229eb07fea344bfbf7cf88ce3443, type: 2}
17+
- {fileID: 11400000, guid: 775351c556329704bbb8384fe6ed46c2, type: 2}

Tags Containers/New Tags Container.asset.meta renamed to Tags Containers/Actors.asset.meta

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Tags/New Tag.asset renamed to Tags/Player.asset

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@ MonoBehaviour:
1010
m_Enabled: 1
1111
m_EditorHideFlags: 0
1212
m_Script: {fileID: 11500000, guid: c372fcbe81c655f42b02d97c6a9ef2e0, type: 3}
13-
m_Name: New Tag
13+
m_Name: Player
1414
m_EditorClassIdentifier:

Tags/New Tag.asset.meta renamed to Tags/Player.asset.meta

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)