Skip to content

Transform Extensions

Andrzej Kebab edited this page Feb 2, 2024 · 3 revisions

UtilityLibrary.Unity.Runtime

Position

SetPositionX

Sets the X-coordinate of the transform's position.

transform.SetPositionX(2.0f);

Parameters:

  • x: The new X-coordinate.

SetPositionY

Sets the Y-coordinate of the transform's position.

transform.SetPositionY(-1.0f);

Parameters:

  • y: The new Y-coordinate.

SetPositionZ

Sets the Z-coordinate of the transform's position.

transform.SetPositionZ(5.0f);

Parameters:

  • z: The new Z-coordinate.

SetPositionXY

Sets the X and Y coordinates of the transform's position.

transform.SetPositionXY(2.0f, -1.0f);

Parameters:

  • x: The new X-coordinate.
  • y: The new Y-coordinate.

SetPositionXZ

Sets the X and Z coordinates of the transform's position.

transform.SetPositionXZ(2.0f, 5.0f);

Parameters:

  • x: The new X-coordinate.
  • z: The new Z-coordinate.

SetPositionYZ

Sets the Y and Z coordinates of the transform's position.

transform.SetPositionYZ(-1.0f, 5.0f);

Parameters:

  • y: The new Y-coordinate.
  • z: The new Z-coordinate.

GetPositionX

Gets the X-coordinate of the transform's position.

float x = transform.GetPositionX();

Returns:

  • x: The X-coordinate of the position.

GetPositionY

Gets the Y-coordinate of the transform's position.

float y = transform.GetPositionY();

Returns:

  • y: The Y-coordinate of the position.

GetPositionZ

Gets the Z-coordinate of the transform's position.

float z = transform.GetPositionZ();

Returns:

  • z: The Z-coordinate of the position.

GetPositionXY

Gets the X and Y coordinates of the transform's position as a Vector2.

Vector2 xy = transform.GetPositionXY();

Returns:

  • xy: The X and Y coordinates as a Vector2.

GetPositionXZ

Gets the X and Z coordinates of the transform's position as a Vector2.

Vector2 xz = transform.GetPositionXZ();

Returns:

  • xz: The X and Z coordinates as a Vector2.

GetPositionYZ

Gets the Y and Z coordinates of the transform's position as a Vector2.

Vector2 yz = transform.GetPositionYZ();

Returns:

  • yz: The Y and Z coordinates as a Vector2.

Rotation

GetRotationX

Gets the X-component of the transform's rotation.

float x = transform.GetRotationX();

Returns:

  • x: The X-component of the rotation.

GetRotationY

Gets the Y-component of the transform's rotation.

float y = transform.GetRotationY();

Returns:

  • y: The Y-component of the rotation.

GetRotationZ

Gets the Z-component of the transform's rotation.

float z = transform.GetRotationZ();

Returns:

  • z: The Z-component of the rotation.

GetRotationW

Gets the W-component of the transform's rotation.

float w = transform.GetRotationW();

Returns:

  • w: The W-component of the rotation.

SetRotationX

Sets the X-component of the transform's rotation.

transform.SetRotationX(45.0f);

Parameters:

  • x: The new X-component of the rotation.

SetRotationY

Sets the Y-component of the transform's rotation.

transform.SetRotationY(180.0f);

Parameters:

  • y: The new Y-component of the rotation.

SetRotationZ

Sets the Z-component of the transform's rotation.

transform.SetRotationZ(-90.0f);

Parameters:

  • z: The new Z-component of the rotation.

SetRotationW

Sets the W-component of the transform's rotation.

transform.SetRotationW(0.5f);

Parameters:

  • w: The new W-component of the rotation.

EulerAngles

GetEulerAnglesX

Gets the X-component of the transform's Euler angles.

float x = transform.GetEulerAnglesX();

Returns:

  • x: The X-component of the Euler angles.

GetEulerAnglesY

Gets the Y-component of the transform's Euler angles.

float y = transform.GetEulerAnglesY();

Returns:

  • y: The Y-component of the Euler angles.

GetEulerAnglesZ

Gets the Z-component of the transform's Euler angles.

float z = transform.GetEulerAnglesZ();

Returns:

  • z: The Z-component of the Euler angles.

GetEulerAnglesXY

Gets the X and Y components of the transform's Euler angles as a Vector2.

Vector2 xy = transform.GetEulerAnglesXY();

Returns:

  • xy: The X and Y components as a Vector2.

GetEulerAnglesXZ

Gets the X and Z components of the transform's Euler angles as a Vector2.

Vector2 xz = transform.GetEulerAnglesXZ();

Returns:

  • xz: The X and Z components as a Vector2.

GetEulerAnglesYZ

Gets the Y and Z components of the transform's Euler angles as a Vector2.

Vector2 yz = transform.GetEulerAnglesYZ();

Returns:

  • yz: The Y and Z components as a Vector2.

SetEulerAnglesX

Sets the X-component of the transform's Euler angles.

transform.SetEulerAnglesX(30.0f);

Parameters:

  • x: The new X-component of the Euler angles.

SetEulerAnglesY

Sets the Y-component of the transform's Euler angles.

transform.SetEulerAnglesY(-45.0f);

Parameters:

  • y: The new Y-component of the Euler angles.

SetEulerAnglesZ

Sets the Z-component of the transform's Euler angles.

transform.SetEulerAnglesZ(90.0f);

Parameters:

  • z: The new Z-component of the Euler angles.

SetEulerAnglesXY

Sets the X and Y components of the transform's Euler angles.

transform.SetEulerAnglesXY(30.0f, -45.0f);

Parameters:

  • x: The new X-component of the Euler angles.
  • y: The new Y-component of the Euler angles.

SetEulerAnglesXZ

Sets the X and Z components of the transform's Euler angles.

transform.SetEulerAnglesXZ(30.0f, 90.0f);

Parameters:

  • x: The new X-component of the Euler angles.
  • z: The new Z-component of the Euler angles.

SetEulerAnglesYZ

Sets the Y and Z components of the transform's Euler angles.

transform.SetEulerAnglesYZ(-45.0f, 90.0f);

Parameters:

  • y: The new Y-component of the Euler angles.
  • z: The new Z-component of the Euler angles.

LocalPosition

SetLocalPositionX

Sets the X-coordinate of the transform's local position.

transform.SetLocalPositionX(2.0f);

Parameters:

  • x: The new X-coordinate.

SetLocalPositionY

Sets the Y-coordinate of the transform's local position.

transform.SetLocalPositionY(-1.0f);

Parameters:

  • y: The new Y-coordinate.

SetLocalPositionZ

Sets the Z-coordinate of the transform's local position.

transform.SetLocalPositionZ(5.0f);

Parameters:

  • z: The new Z-coordinate.

SetLocalPositionXY

Sets the X and Y coordinates of the transform's local position.

transform.SetLocalPositionXY(2.0f, -1.0f);

Parameters:

  • x: The new X-coordinate.
  • y: The new Y-coordinate.

SetLocalPositionXZ

Sets the X and Z coordinates of the transform's local position.

transform.SetLocalPositionXZ(2.0f, 5.0f);

Parameters:

  • x: The new X-coordinate.
  • z: The new Z-coordinate.

SetLocalPositionYZ

Sets the Y and Z coordinates of the transform's local position.

transform.SetLocalPositionYZ(-1.0f, 5.0f);

Parameters:

  • y: The new Y-coordinate.
  • z: The new Z-coordinate.

GetLocalPositionX

Gets the X-coordinate of the transform's local position.

float x = transform.GetLocalPositionX();

Returns:

  • x: The X-coordinate of the local position.

GetLocalPositionY

Gets the Y-coordinate of the transform's local position.

float y = transform.GetLocalPositionY();

Returns:

  • y: The Y-coordinate of the local position.

GetLocalPositionZ

Gets the Z-coordinate of the transform's local position.

float z = transform.GetLocalPositionZ();

Returns:

  • z: The Z-coordinate of the local position.

GetLocalPositionXY

Gets the X and Y coordinates of the transform's local position as a Vector2.

Vector2 xy = transform.GetLocalPositionXY();

Returns:

  • xy: The X and Y coordinates as a Vector2.

GetLocalPositionXZ

Gets the X and Z coordinates of the transform's local position as a Vector2.

Vector2 xz = transform.GetLocalPositionXZ();

Returns:

  • xz: The X and Z coordinates as a Vector2.

GetLocalPositionYZ

Gets the Y and Z coordinates of the transform's local position as a Vector2.

Vector2 yz = transform.GetLocalPositionYZ();

Returns:

  • yz: The Y and Z coordinates as a Vector2.

LocalRotation

GetLocalRotationX

Gets the X-component of the transform's local rotation.

float x = transform.GetLocalRotationX();

Returns:

  • x: The X-component of the local rotation.

GetLocalRotationY

Gets the Y-component of the transform's local rotation.

float y = transform.GetLocalRotationY();

Returns:

  • y: The Y-component of the local rotation.

GetLocalRotationZ

Gets the Z-component of the transform's local rotation.

float z = transform.GetLocalRotationZ();

Returns:

  • z: The Z-component of the local rotation.

GetLocalRotationW

Gets the W-component of the transform's local rotation.

float w = transform.GetLocalRotationW();

Returns:

  • w: The W-component of the local rotation.

SetLocalRotationX

Sets the X-component of the transform's local rotation.

transform.SetLocalRotationX(45.0f);

Parameters:

  • x: The new X-component of the local rotation.

SetLocalRotationY

Sets the Y-component of the transform's local rotation.

transform.SetLocalRotationY(-90.0f);

Parameters:

  • y: The new Y-component of the local rotation.

SetLocalRotationZ

Sets the Z-component of the transform's local rotation.

transform.SetLocalRotationZ(180.0f);

Parameters:

  • z: The new Z-component of the local rotation.

SetLocalRotationW

Sets the W-component of the transform's local rotation.

transform.SetLocalRotationW(0.5f);

Parameters:

  • w: The new W-component of the local rotation.

LocalEulerAngles

GetLocalEulerAnglesX

Gets the X-component of the transform's local Euler angles.

float x = transform.GetLocalEulerAnglesX();

Returns:

  • x: The X-component of the local Euler angles.

GetLocalEulerAnglesY

Gets the Y-component of the transform's local Euler angles.

float y = transform.GetLocalEulerAnglesY();

Returns:

  • y: The Y-component of the local Euler angles.

GetLocalEulerAnglesZ

Gets the Z-component of the transform's local Euler angles.

float z = transform.GetLocalEulerAnglesZ();

Returns:

  • z: The Z-component of the local Euler angles.

SetLocalEulerAnglesX

Sets the X-component of the transform's local Euler angles.

transform.SetLocalEulerAnglesX(30.0f);

Parameters:

  • x: The new X-component of the local Euler angles.

SetLocalEulerAnglesY

Sets the Y-component of the transform's local Euler angles.

transform.SetLocalEulerAnglesY(-45.0f);

**Parameters

:**

  • y: The new Y-component of the local Euler angles.

SetLocalEulerAnglesZ

Sets the Z-component of the transform's local Euler angles.

transform.SetLocalEulerAnglesZ(90.0f);

Parameters:

  • z: The new Z-component of the local Euler angles.

SetLocalEulerAnglesXY

Sets the X and Y components of the transform's local Euler angles.

transform.SetLocalEulerAnglesXY(30.0f, -45.0f);

Parameters:

  • x: The new X-component of the local Euler angles.
  • y: The new Y-component of the local Euler angles.

SetLocalEulerAnglesXZ

Sets the X and Z components of the transform's local Euler angles.

transform.SetLocalEulerAnglesXZ(30.0f, 90.0f);

Parameters:

  • x: The new X-component of the local Euler angles.
  • z: The new Z-component of the local Euler angles.

SetLocalEulerAnglesYZ

Sets the Y and Z components of the transform's local Euler angles.

transform.SetLocalEulerAnglesYZ(-45.0f, 90.0f);

Parameters:

  • y: The new Y-component of the local Euler angles.
  • z: The new Z-component of the local Euler angles.

LocalScale

SetLocalScaleX

Sets the X component of the transform's local scale.

transform.SetLocalScaleX(2.0f);

Parameters:

  • x: The new X component of the local scale.

SetLocalScaleY

Sets the Y component of the transform's local scale.

transform.SetLocalScaleY(0.5f);

Parameters:

  • y: The new Y component of the local scale.

SetLocalScaleZ

Sets the Z component of the transform's local scale.

transform.SetLocalScaleZ(1.5f);

Parameters:

  • z: The new Z component of the local scale.

SetLocalScaleXY

Sets the X and Y components of the transform's local scale.

transform.SetLocalScaleXY(2.0f, 0.5f);

Parameters:

  • x: The new X component of the local scale.
  • y: The new Y component of the local scale.

SetLocalScaleXZ

Sets the X and Z components of the transform's local scale.

transform.SetLocalScaleXZ(2.0f, 1.5f);

Parameters:

  • x: The new X component of the local scale.
  • z: The new Z component of the local scale.

SetLocalScaleYZ

Sets the Y and Z components of the transform's local scale.

transform.SetLocalScaleYZ(0.5f, 1.5f);

Parameters:

  • y: The new Y component of the local scale.
  • z: The new Z component of the local scale.

GetLocalScaleX

Gets the X component of the transform's local scale.

float x = transform.GetLocalScaleX();

Returns:

  • x: The X component of the local scale.

GetLocalScaleY

Gets the Y component of the transform's local scale.

float y = transform.GetLocalScaleY();

Returns:

  • y: The Y component of the local scale.

GetLocalScaleZ

Gets the Z component of the transform's local scale.

float z = transform.GetLocalScaleZ();

Returns:

  • z: The Z component of the local scale.

GetLocalScaleXY

Gets the X and Y components of the transform's local scale as a Vector2.

Vector2 xy = transform.GetLocalScaleXY();

Returns:

  • xy: The X and Y components as a Vector2.

GetLocalScaleXZ

Gets the X and Z components of the transform's local scale as a Vector2.

Vector2 xz = transform.GetLocalScaleXZ();

Returns:

  • xz: The X and Z components as a Vector2.

GetLocalScaleYZ

Gets the Y and Z components of the transform's local scale as a Vector2.

Vector2 yz = transform.GetLocalScaleYZ();

Returns:

  • yz: The Y and Z components as a Vector2.

Component

GetOrAddComponent<T>

Retrieves or adds a component of a specified type to the transform.

var myComponent = transform.GetOrAddComponent<MyComponentType>();

Parameters:

  • transform: The transform to retrieve or add the component to.

Returns:

  • myComponent: The retrieved or added component.

GetOrAddComponent

Retrieves or adds a component of a specified type to the transform.

var myComponent = transform.GetOrAddComponent(typeof(MyComponentType));

Parameters:

  • transform: The transform to retrieve or add the component to.
  • type: The type of the component.

Returns:

  • myComponent: The retrieved or added component.

TryGetComponentInHierarchy<T>

Tries to get a component of a specified type from the transform or its ancestors.

bool success = someTransform.TryGetComponentInHierarchy(out SomeComponentType myComponent);

Parameters:

  • context: The transform to retrieve the component from.
  • target: The retrieved component.

Returns:

  • success: True if the component was found, false otherwise.

Parent

FindComponentInParents<T>

Finds a component of a specified type in the transform or its ancestors.

var myComponent = someTransform.FindComponentInParents<MyComponentType>();

Parameters:

  • transform: The transform to retrieve the component from.

Returns:

  • myComponent: The retrieved component, or null if not found.

Child

GetAllChild<T>

Retrieves all children of the transform that have a component of a specified type.

List<MyComponentType> childrenWithComponent = someTransform.GetAllChild<MyComponentType>();

Parameters:

  • transform: The transform to retrieve the children from.

Returns:

  • childrenWithComponent: A list of children with the specified component.

FindInAllChildFuzzy

Finds a child of the transform with a specified name using a fuzzy search.

Transform foundChild = someTransform.FindInAllChildFuzzy("PartialName");

Parameters:

  • transform: The transform to retrieve the child from.
  • name: The name of the child.
  • includeInactive: Whether to include inactive children.

Returns:

  • foundChild: The child with the specified name, or null if not found.

FindInAllChild

Finds a child of the transform with a specified name.

Transform foundChild = someTransform.FindInAllChild("ExactName");

Parameters:

  • transform: The transform to retrieve the child from.
  • name: The name of the child.
  • includeInactive: Whether to include inactive children.

Returns:

  • foundChild: The child with the specified name, or null if not found.

ForeachChild

Performs an action on each child of the transform.

someTransform.ForeachChild(child => Debug.Log(child.name));

Parameters:

  • transform: The transform to perform the action on its children.
  • action: The action to perform.

Returns:

  • transform: The transform.

UpdateChild

Updates the children of the transform based on a specified count and updater function.

someTransform.UpdateChild(5, (index, child) => child.localScale = new Vector3(index + 1, index + 1, index + 1));

Parameters:

  • transform: The transform to update its children.
  • count: The number of children to update.
  • updater: The function to update the children.

Returns:

  • transform: The transform.

DestroyChildren

Destroys all children of the transform.

someTransform.DestroyChildren();

Parameters:

  • transform: The transform to destroy its children.

Returns:

  • transform: The transform.

ScreenPosition

ScreenPosition

Calculates the screen position of the transform.

Vector2 screenPos = someTransform.ScreenPosition();

Parameters:

  • transform: The transform to calculate the screen position for.

Returns:

  • screenPos: A Vector2 representing the screen position of the transform.

ScreenPositionRatio

Calculates the screen position ratio of the transform.

Vector2 screenPosRatio = someTransform.ScreenPositionRatio();

Parameters:

  • transform: The transform to calculate the screen position ratio for.

Returns:

  • screenPosRatio: A Vector2 representing the screen position ratio of the transform.

Copy

CopyTransform

Copies the transform values from a source transform to the current transform.

someTransform.CopyTransform(sourceTransform, true);

Parameters:

  • transform: The transform to copy values to.
  • src: The source transform to copy values from.
  • isWordSpace: If true, copies the world position and rotation. If false, copies the local position and rotation.

Returns:

  • transform: The transform with copied values.

Path

Path

Retrieves the path of the transform in the hierarchy.

string transformPath = someTransform.Path();

Parameters:

  • transform: The transform to retrieve the path for.

Returns:

  • transformPath: A string representing the path of the transform in the hierarchy.

PathFull

Retrieves the full path of the transform in the hierarchy, including the transform's name.

string fullTransformPath = someTransform.PathFull();

Parameters:

  • transform: The transform to retrieve the full path for.

Returns:

  • fullTransformPath: A string representing the full path of the transform in the hierarchy.

Root

Retrieves the root transform of the current transform.

Transform rootTransform = someTransform.Root();

Parameters:

  • go: The transform to retrieve the root for.

Returns:

  • rootTransform: The root transform of the current transform.

Depth

Calculates the depth of the transform in the hierarchy.

int transformDepth = someTransform.Depth();

Parameters:

  • transform: The transform to calculate the depth for.

Returns:

  • transformDepth: An integer representing the depth of the transform in the hierarchy.

Is InView

IsInView

Determines whether the transform is within the view of the specified camera.

bool isInView = someTransform.IsInView(Camera.main);

Parameters:

  • transform: The transform to check.
  • camera: The camera to check the view for.

Returns:

  • isInView: True if the transform is in the view of the camera, false otherwise.

Clone this wiki locally