/// ---------------------------------------------
/// Ultimate Character Controller
/// Copyright (c) Opsive. All Rights Reserved.
/// https://www.opsive.com
/// ---------------------------------------------
namespace Opsive.UltimateCharacterController.Utility
{
using UnityEngine;
///
/// Extension methods for the UnityEngine.Transform class.
///
public static class TransformExtensions
{
///
/// Sets the parent of the transform object to the specified parent.
///
/// The transform to set the parent of.
/// The parent of the transform.
public static void SetParentOrigin(this Transform transform, Transform parent)
{
transform.parent = parent;
transform.localPosition = Vector3.zero;
transform.localRotation = Quaternion.identity;
transform.localScale = Vector3.one;
}
///
/// Recursively sets the layer on all of the children.
///
/// The transform to set the layer on.
/// The layer to set.
public static void SetLayerRecursively(this Transform transform, int layer)
{
transform.gameObject.layer = layer;
for (int i = 0; i < transform.childCount; ++i) {
transform.GetChild(i).SetLayerRecursively(layer);
}
}
///
/// Returns the component of the specified type in the GameObject or any of its parents.
///
/// The transform to get the component on.
/// The type of component to return.
/// THe component of the specified type in the GameObject or any of its parents. Can be null.
public static T GetComponentInParentIncludeInactive(this Transform transform) where T : Component
{
var parent = transform;
T component;
while (parent != null) {
if ((component = parent.GetComponent()) != null) {
return component;
}
parent = parent.parent;
}
return null;
}
}
}