using System;
using System.Collections.Generic;
using UnityEngine;
namespace Boxfriend.Extensions
{
public static class GameObjectExtensions
{
///
/// Recursively changes game object and its children to specified layer
///
/// Layer to change all objects to
public static void SetLayerRecursively(this GameObject obj, int layer)
{
obj.layer = layer;
foreach (Transform child in obj.transform)
{
child.gameObject.SetLayerRecursively(layer);
}
}
///
/// Returns the first child of the current GameObject that has the specified tag. Does not include itself.
///
public static GameObject FindChildWithTag(this GameObject obj, string tag)
{
foreach(Transform child in obj.transform)
{
if(child.gameObject == obj) continue;
if(child.CompareTag(tag))
return child.gameObject;
}
return null;
}
///
/// Returns an array containing all children of the current GameObject that have the specified tag. Does not include itself.
///
public static GameObject[] FindChildrenWithTag(this GameObject obj, string tag)
{
var taggedArray = new GameObject[obj.transform.childCount];
var index = 0;
foreach(Transform child in obj.transform)
{
if(child.CompareTag(tag))
{
taggedArray[index] = child.gameObject;
index++;
}
}
if(index == 0) return null;
Array.Resize(ref taggedArray, index);
return taggedArray;
}
///
/// Returns a List containing all children of the current GameObject that have the specified tag. Does not include itself.
///
public static List FindChildrenWithTagList(this GameObject obj, string tag)
{
var taggedList = new List();
foreach(Transform child in obj.transform)
{
if(child.gameObject == obj) continue;
if(child.CompareTag(tag))
taggedList.Add(child.gameObject);
}
return taggedList;
}
///
/// Destroys all children of the GameObject not including itself\
///
public static void DestroyChildren(this GameObject parent)
{
var children = new Transform[parent.transform.childCount];
for (var i = 0; i < parent.transform.childCount; i++)
children[i] = parent.transform.GetChild(i);
for (var i = 0; i < children.Length; i++)
GameObject.Destroy(children[i].gameObject);
}
///
/// Checks if a GameObject is tagged with any of the strings in the provided collection
///
public static bool CompareTags(this GameObject go, IEnumerable tags)
{
foreach (var tag in tags)
{
if (go.CompareTag(tag))
return true;
}
return false;
}
}
}