Skip to content

Commit f7a776e

Browse files
committed
Update
1 parent 094d604 commit f7a776e

File tree

4 files changed

+56
-24
lines changed

4 files changed

+56
-24
lines changed

Tag.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
1+
#if ODIN_INSPECTOR
2+
using Sirenix.OdinInspector;
3+
#endif
14
using System.Collections.Generic;
25
using UnityEngine;
36

47
namespace ToolBox.Tags
58
{
6-
[CreateAssetMenu(menuName = "ToolBox/Tagging/Tag")]
9+
[CreateAssetMenu(menuName = "ToolBox/MultiTags/Tag")]
10+
#if ODIN_INSPECTOR
11+
[AssetSelector, Required]
12+
#endif
713
public sealed class Tag : ScriptableObject
814
{
15+
#if ODIN_INSPECTOR
16+
[ShowInInspector, ReadOnly]
17+
#endif
918
private HashSet<int> _entities = new HashSet<int>();
1019

1120
public void Add(int entity) =>

TagExtensions.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public static void AddTag(this GameObject entity, Tag tag)
1414
if (taggable == null)
1515
taggable = entity.AddComponent<Taggable>();
1616

17-
taggable.Add(tag);
17+
taggable.AddTagInEditor(tag);
1818
#endif
1919
}
2020

@@ -28,7 +28,7 @@ public static void RemoveTag(this GameObject entity, Tag tag)
2828
if (taggable == null)
2929
taggable = entity.AddComponent<Taggable>();
3030

31-
taggable.Remove(tag);
31+
taggable.RemoveTagInEditor(tag);
3232
#endif
3333
}
3434

@@ -50,7 +50,7 @@ public static void AddTags(this GameObject entity, Tag[] tags)
5050
var tag = tags[i];
5151
tag.Add(hash);
5252
#if UNITY_EDITOR
53-
taggable.Add(tag);
53+
taggable.AddTagInEditor(tag);
5454
#endif
5555
}
5656
}
@@ -70,7 +70,7 @@ public static void RemoveTags(this GameObject entity, Tag[] tags)
7070
var tag = tags[i];
7171
tag.Remove(hash);
7272
#if UNITY_EDITOR
73-
taggable.Remove(tag);
73+
taggable.RemoveTagInEditor(tag);
7474
#endif
7575
}
7676
}

Taggable.cs

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,73 @@
1+
#if ODIN_INSPECTOR
2+
using Sirenix.OdinInspector;
3+
#endif
14
#if UNITY_EDITOR
25
using UnityEditor;
36
#endif
47
using UnityEngine;
58

69
namespace ToolBox.Tags
710
{
8-
[DisallowMultipleComponent, DefaultExecutionOrder(-150)]
11+
[DisallowMultipleComponent, DefaultExecutionOrder(-9000), ExecuteInEditMode]
912
public sealed class Taggable : MonoBehaviour
1013
{
14+
#if ODIN_INSPECTOR
15+
[Required, AssetList]
16+
#endif
1117
[SerializeField] private Tag[] _tags = default;
1218

13-
private Tag[] _all = null;
19+
private static Tag[] _all = null;
1420
private int _hash = 0;
1521

1622
private void Awake()
1723
{
1824
#if UNITY_EDITOR
19-
_all = Resources.FindObjectsOfTypeAll<Tag>();
20-
21-
if (_tags == null)
22-
_tags = new Tag[0];
25+
SetupEditorData();
2326
#endif
2427

2528
_hash = gameObject.GetHashCode();
26-
27-
for (int i = 0; i < _tags.Length; i++)
28-
_tags[i].Add(_hash);
29+
AddAll();
2930
}
3031

31-
private void OnEnable()
32+
private void OnDestroy() =>
33+
RemoveAll();
34+
35+
private void AddAll()
3236
{
3337
for (int i = 0; i < _tags.Length; i++)
3438
_tags[i].Add(_hash);
3539
}
3640

37-
private void OnDisable()
41+
private void RemoveAll()
3842
{
3943
for (int i = 0; i < _tags.Length; i++)
4044
_tags[i].Remove(_hash);
4145
}
4246

4347
#if UNITY_EDITOR
44-
public void Add(Tag tag)
48+
public void AddTagInEditor(Tag tag)
4549
{
4650
if (!ArrayUtility.Contains(_tags, tag))
4751
ArrayUtility.Add(ref _tags, tag);
4852
}
4953

50-
public void Remove(Tag tag)
54+
public void RemoveTagInEditor(Tag tag)
5155
{
5256
if (ArrayUtility.Contains(_tags, tag))
5357
ArrayUtility.Remove(ref _tags, tag);
5458
}
5559

60+
// Handle Inspector Changes
5661
private void OnValidate()
5762
{
58-
if (!Application.isPlaying || _all == null)
59-
return;
60-
63+
SetupEditorData();
6164
var obj = gameObject;
65+
AddAll();
6266

63-
foreach (var tag in _all)
67+
for (int i = 0; i < _all.Length; i++)
6468
{
69+
var tag = _all[i];
70+
6571
if (ArrayUtility.Contains(_tags, tag))
6672
{
6773
if (!obj.HasTag(tag))
@@ -74,6 +80,18 @@ private void OnValidate()
7480
}
7581
}
7682
}
83+
84+
private void SetupEditorData()
85+
{
86+
if (_all == null)
87+
_all = Resources.FindObjectsOfTypeAll<Tag>();
88+
89+
if (_tags == null)
90+
_tags = new Tag[0];
91+
92+
if (_hash == 0)
93+
_hash = gameObject.GetHashCode();
94+
}
7795
#endif
7896
}
7997
}

TagsContainer.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
1-
using Sirenix.OdinInspector;
1+
#if ODIN_INSPECTOR
2+
using Sirenix.OdinInspector;
3+
#endif
24
using UnityEngine;
35

46
namespace ToolBox.Tags
57
{
6-
[CreateAssetMenu(menuName = "ToolBox/Tagging/Tags Container"), AssetSelector]
8+
[CreateAssetMenu(menuName = "ToolBox/MultiTags/Tags Container")]
9+
#if ODIN_INSPECTOR
10+
[AssetSelector, Required]
11+
#endif
712
public sealed class TagsContainer : ScriptableObject
813
{
914
[SerializeField] private Tag[] _tags = null;

0 commit comments

Comments
 (0)