Skip to content

TransformExtensions

Namespace: SideXP.Core

public static class TransformExtensions

Extension functions for Transform instances.

Methods

ForEachChild(Transform, Action<Transform>, bool)

public static void ForEachChild(Transform transform, Action<Transform> action, bool recursive = false)

Performs an operation for each child of a given Transform (excluding itself).

Parameters

  • action: The operation to perform for each child.

GetChildren(Transform, bool)

public static Transform[] GetChildren(Transform transform, bool recursive = false)

Gets all the children of a given Transform (excluding itself).

Parameters

  • recursive: By default, this function processes only direct children. If enabled, gets the children recursively in the hierarchy.
  • transform: The Transform component to process.

Returns

Returns the found children.

ClearHierarchy(Transform)

public static void ClearHierarchy(Transform transform)

Destroy all transform in the given one's hierarchy.

RemoveChildrenOfType(Transform, Type)

public static int RemoveChildrenOfType(Transform transform, Type componentType)

Removes all children in the hierarchy if they have a given component attached.

Parameters

  • transform: The transform of which to clean the hierarchy.
  • componentType: The component type of the children to remove.

Returns

Returns the number of removed children.

RemoveChildrenOfType<T>(Transform)

public static int RemoveChildrenOfType<T>(Transform transform)

Type parameters

  • T: The component type of the children to remove.

Pool<T>(Transform, int, Func<Transform, T>, Action<T, int>, Action<T, int>)

public static void Pool<T>(Transform container, int expectedCount, Func<Transform, T> onInstantiate, Action<T, int> onInit, Action<T, int> onDiscard = null)

Reuse existing instances of a given type of component from a given container, or create new instances to match a given count. Transform container = GetUIContainer(); UI_Item uiItemPrefab = GetUIItemPrefab(); container.Pool(5, uiContainer => { GameObject uiItem = Instantiate(GetUIItemPrefab().gameObject, uiContainer, false); return uiItem.GetComponent{UI_Item}(); }, (uiItem, index) => { uiItem.SetIndex(index); });

Type parameters

  • T: The type of the objects to find or instantiate in the container.

Parameters

  • container: The object that contains the pooled objects, or which will contain the created instances.
  • expectedCount: The expected number of objects in the container.
  • onInstantiate: Called when a new object instance is required.
  • onInit: Called when a pooled or a created instance is reused. First parameter is the reused instance, second parameter is its index in the container.
  • onDiscard: Called when an existing instance won't be reused. First parameter is the instance, second parameter is its index in the container.