using System.Linq;
using UnityEngine;
namespace OmiLAXR.Extensions
{
public static class Object_Ext
{
///
/// Gets the full hierarchy path of the GameObject.
///
public static string GetFullHierarchyPath(this Object obj)
{
GameObject gameObject;
var isComponent = obj is Component comp;
if (isComponent)
{
gameObject = ((Component)obj).gameObject;
}
else
{
gameObject = obj as GameObject;
}
if (gameObject == null)
return string.Empty;
var path = gameObject.name;
var current = gameObject.transform.parent;
while (current != null)
{
path = current.name + "/" + path;
current = current.parent;
}
if (isComponent)
{
path += "/" + ((Component)obj).GetType().Name;
}
return path;
}
///
/// Removes a specific type T from objects. It checks if the type is equal, if it's a subclass, or if a component of the type exists.
///
/// Array of objects to filter
/// Target type to exclude
/// An array without objects of type T
public static Object[] Exclude(this Object[] objects) where T : Object
{
var gameObjectType = typeof(GameObject);
var componentType = typeof(Component);
var targetType = typeof(T);
return objects.Where(o =>
{
var objectType = o.GetType();
// Direct match or subclass of T
if (objectType == targetType || objectType.IsSubclassOf(targetType))
return false;
// Check if the object is a Component and has the target type as a component
if (objectType == componentType || objectType.IsSubclassOf(componentType))
{
var component = o as Component;
if (component && component.GetComponent())
return false;
}
// Check if the object is a GameObject and has the target type as a component
else if (objectType == gameObjectType || objectType.IsSubclassOf(gameObjectType))
{
var gameObject = o as GameObject;
if (gameObject && gameObject.GetComponent())
return false;
}
return true;
}).ToArray();
}
}
}