using UnityEngine;
using System.Linq;
namespace UnityUtils
{
public static class GameObjectExtensions
{
///
/// This method is used to hide the GameObject in the Hierarchy view.
///
///
public static void HideInHierarchy(this GameObject gameObject)
{
gameObject.hideFlags = HideFlags.HideInHierarchy;
}
///
/// Gets a component of the given type attached to the GameObject. If that type of component does not exist, it adds one.
///
///
/// This method is useful when you don't know if a GameObject has a specific type of component,
/// but you want to work with that component regardless. Instead of checking and adding the component manually,
/// you can use this method to do both operations in one line.
///
/// The type of the component to get or add.
/// The GameObject to get the component from or add the component to.
/// The existing component of the given type, or a new one if no such component exists.
public static T GetOrAdd(this GameObject gameObject) where T : Component
{
T component = gameObject.GetComponent();
if (!component) component = gameObject.AddComponent();
return component;
}
///
/// Returns the object itself if it exists, null otherwise.
///
///
/// This method helps differentiate between a null reference and a destroyed Unity object. Unity's "== null" check
/// can incorrectly return true for destroyed objects, leading to misleading behaviour. The OrNull method use
/// Unity's "null check", and if the object has been marked for destruction, it ensures an actual null reference is returned,
/// aiding in correctly chaining operations and preventing NullReferenceExceptions.
///
/// The type of the object.
/// The object being checked.
/// The object itself if it exists and not destroyed, null otherwise.
public static T OrNull(this T obj) where T : Object => obj ? obj : null;
///
/// Destroys all children of the game object
///
/// GameObject whose children are to be destroyed.
public static void DestroyChildren(this GameObject gameObject)
{
gameObject.transform.DestroyChildren();
}
///
/// Immediately destroys all children of the given GameObject.
///
/// GameObject whose children are to be destroyed.
public static void DestroyChildrenImmediate(this GameObject gameObject)
{
gameObject.transform.DestroyChildrenImmediate();
}
///
/// Enables all child GameObjects associated with the given GameObject.
///
/// GameObject whose child GameObjects are to be enabled.
public static void EnableChildren(this GameObject gameObject)
{
gameObject.transform.EnableChildren();
}
///
/// Disables all child GameObjects associated with the given GameObject.
///
/// GameObject whose child GameObjects are to be disabled.
public static void DisableChildren(this GameObject gameObject)
{
gameObject.transform.DisableChildren();
}
///
/// Resets the GameObject's transform's position, rotation, and scale to their default values.
///
/// GameObject whose transformation is to be reset.
public static void ResetTransformation(this GameObject gameObject)
{
gameObject.transform.Reset();
}
///
/// Returns the hierarchical path in the Unity scene hierarchy for this GameObject.
///
/// The GameObject to get the path for.
/// A string representing the full hierarchical path of this GameObject in the Unity scene.
/// This is a '/'-separated string where each part is the name of a parent, starting from the root parent and ending
/// with the name of the specified GameObjects parent.
public static string Path(this GameObject gameObject)
{
return "/" + string.Join("/",
gameObject.GetComponentsInParent().Select(t => t.name).Reverse().ToArray());
}
///
/// Returns the full hierarchical path in the Unity scene hierarchy for this GameObject.
///
/// The GameObject to get the path for.
/// A string representing the full hierarchical path of this GameObject in the Unity scene.
/// This is a '/'-separated string where each part is the name of a parent, starting from the root parent and ending
/// with the name of the specified GameObject itself.
public static string PathFull(this GameObject gameObject)
{
return gameObject.Path() + "/" + gameObject.name;
}
///
/// Recursively sets the provided layer for this GameObject and all of its descendants in the Unity scene hierarchy.
///
/// The GameObject to set layers for.
/// The layer number to set for GameObject and all of its descendants.
public static void SetLayersRecursively(this GameObject gameObject, int layer)
{
gameObject.layer = layer;
gameObject.transform.ForEveryChild(child => child.gameObject.SetLayersRecursively(layer));
}
}
}