[feature request] Add GameObject.GetTags() extension
lemoissonneur opened this issue · 2 comments
lemoissonneur commented
Hello !
Thank you for this package.
I think it would be a good addition to be able to get a list of Tags from a GameObject based on a TagContainer.
public static class TagExtensions
{
...
public static Tag[] GetTags(this GameObject entity, TagsContainer tags)
{
Tag[] tagTable = new Tag[tags._tags.Length];
int index = 0;
for (int i = 0; i < tags._tags.Length; ++i)
{
if (entity.HasTag(tags._tags[i]))
{
tagTable[index] = tags._tags[i];
index++;
}
}
System.Array.Resize(ref tagTable, index);
return tagTable;
}
...
}
but it require the TagContainer tag array to be public.
public sealed class TagsContainer : ScriptableObject
{
[SerializeField] public Tag[] _tags = null;
...
}
use example :
using System.Collections.Generic;
using UnityEngine;
using ToolBox.Tags;
public class Example : MonoBehaviour
{
public List<GameObject> myGameObjects = new List<GameObject>();
public TagsContainer relevantTags;
public List<GameObject> GetMatchingGameObjects(GameObject reference)
{
List<GameObject> matchingGameObjects = new List<GameObject>();
Tag[] referenceTags = reference.GetTags(relevantTags);
foreach(GameObject g in myGameObjects)
{
if (g.HasTags(referenceTags, true))
matchingGameObjects.Add(g);
}
return matchingGameObjects;
}
}
IntoTheDev commented
Hello again! :)
You can make fork and then make this changes. Sorry, but i don't found this feature useful to put it in master branch.
lemoissonneur commented
Understood, thanks for the reply.