Skip to content

Extensions

Ilia Petrov-Komotskii edited this page Oct 16, 2026 · 4 revisions

Runtime helpers in Mane.Unity.

GameObject / Component

GetOrAddComponent

Returns the component, or adds one when it is missing. The same call exists on GameObject and on Component (that one uses the component's GameObject).

Rigidbody body = cam.GetOrAddComponent<Rigidbody>();

Duplicate

Instantiates a sibling immediately after the source. The GameObject overload returns the clone. The Component overload clones that component's GameObject and returns the matching component on the clone.

GameObject clone = go.Duplicate();

SetLayerRecursively

Sets GameObject.layer on this object and every descendant.

go.SetLayerRecursively(layer);

SetSortingLayerRecursively

Sets sortingLayerID on each Renderer and Canvas in the hierarchy. Pass newOrder to also set sortingOrder. Omit it and the current order stays.

go.SetSortingLayerRecursively(sortingLayerId);
go.SetSortingLayerRecursively(sortingLayerId, 10);

SetActiveStateRecursively

Calls SetActive on this object and every descendant.

go.SetActiveStateRecursively(false);

DoRecursively

Runs an action on this object, then on every descendant. The recursive layer, sorting, and active helpers use this. A null object or a null action does nothing.

go.DoRecursively(current => current.tag = "Lit");

IsPrefab

True when scene.rootCount is 0. That is the usual case for a prefab asset. A scene object is false. An object with no loaded scene is true as well.

bool prefab = go.IsPrefab();

SafeDestroy

Destroy in play mode. DestroyImmediate in the Editor when not playing, so edit-mode code can remove objects. Player builds always use Destroy.

someObject.SafeDestroy();

Transform

Reset

Sets local position to 0, local scale to 1, and local rotation to identity.

transform.Reset();

RotateAround

Rotates the transform around a world-space pivot. The arguments are a pivot and a quaternion. Position and rotation are both multiplied by that quaternion.

transform.RotateAround(pivot, rotation);

Rect / RectTransform

SetLeftOffset

Writes offsetMin.x. That is the left inset from the anchor.

rt.SetLeftOffset(8f);

SetRightOffset

Writes offsetMax.x as -right. Unity stores the right inset negated.

rt.SetRightOffset(8f);

SetTopOffset

Writes offsetMax.y as -top. Unity stores the top inset negated.

rt.SetTopOffset(8f);

SetBottomOffset

Writes offsetMin.y. That is the bottom inset from the anchor.

rt.SetBottomOffset(8f);

GetWorldCoordinates

Rect from the bottom-left and top-right world corners. An axis-aligned element matches those corners. A rotated element uses only those two corners. A null transform returns Rect.zero.

Rect world = rt.GetWorldCoordinates();

ScaleSizeBy

Scales a Rect about its center, or about a pivot you pass. The rect itself is not modified. The method returns the scaled copy.

Rect scaled = rect.ScaleSizeBy(1.5f);
Rect aroundPivot = rect.ScaleSizeBy(1.5f, pivot);

Vectors

Vector helpers return a new value, so assign the result. SetX, SetY, Translate, FlipX, and FlipY exist on Vector2 and Vector3. SetZ and FlipZ are Vector3 only.

SetX

Returns a copy with X replaced.

Vector2 moved = v2.SetX(1f);
Vector3 moved3 = v3.SetX(1f);

SetY

Returns a copy with Y replaced.

Vector2 moved = v2.SetY(1f);
Vector3 moved3 = v3.SetY(1f);

SetZ

Returns a Vector3 copy with Z replaced.

Vector3 moved = v3.SetZ(1f);

Translate

Adds a delta and returns the copy. Vector2 and Vector3 take components or another vector. Vector3.Translate(Vector2) changes X and Y and leaves Z. Vector2.Translate(dX, dY, dZ) returns a Vector3 whose Z is dZ.

Vector2 moved = v2.Translate(1f, 2f);
Vector3 moved3 = v3.Translate(delta);
Vector3 shifted = v3.Translate(delta2);
Vector3 withZ = v2.Translate(1f, 2f, 3f);

TranslateX

Adds a delta to X and returns the copy.

Vector2 moved = v2.TranslateX(1f);
Vector3 moved3 = v3.TranslateX(1f);

TranslateY

Adds a delta to Y and returns the copy.

Vector2 moved = v2.TranslateY(1f);
Vector3 moved3 = v3.TranslateY(1f);

TranslateZ

Adds a delta to Z and returns the Vector3 copy.

Vector3 moved = v3.TranslateZ(1f);

FlipX

Returns a copy with X negated.

Vector2 flipped = v2.FlipX();
Vector3 flipped3 = v3.FlipX();

FlipY

Returns a copy with Y negated.

Vector2 flipped = v2.FlipY();
Vector3 flipped3 = v3.FlipY();

FlipZ

Returns a Vector3 copy with Z negated.

Vector3 flipped = v3.FlipZ();

AddZ

Returns a Vector3 with this Vector2 as XY and the given Z. Z defaults to 0.

Vector3 flat = v2.AddZ();
Vector3 raised = v2.AddZ(2f);

Clamp

Clamps each component between a and b.

Vector2 clamped2 = v2.Clamp(0f, 10f);
Vector3 clamped3 = v3.Clamp(0f, 10f);

Project

Projects a Vector2 onto another. A near-zero normal returns Vector2.zero.

Vector2 projected = v.Project(normal);

Area

Vector2 width times height (x * y).

float area = size.Area();

Volume

Vector3 x * y * z.

float volume = size.Volume();

Divide

Divides each component by the matching component of the divisor. A zero component in the divisor produces infinity.

Vector2 divided2 = a.Divide(b);
Vector3 divided3 = a3.Divide(b3);

Average

Component-wise mean of a sequence. An empty sequence returns zero. Vector2 and Vector3.

Vector2 mean2 = points2.Average();
Vector3 mean3 = points3.Average();

ClosestPointOnLine

Closest point on the segment from a to b, including the endpoints. Vector3 only.

Vector3 hit = point.ClosestPointOnLine(a, b);

IsInPolygon

True when the point is inside a convex polygon, or on its edge. A point on the line of an edge but past its ends is outside. Corners must be in order, clockwise or counter-clockwise; a repeated corner is ignored. The test uses X and Y only.

bool inside = point.IsInPolygon(corners);

IsInsideRectangle

True when a Vector2 is inside a rectangle. Pass a Rect, four corners, or a four-point array. Any other array length throws. The corner test allows a rotated rectangle. A point on the boundary counts as inside.

bool inRect = point.IsInsideRectangle(rect);
bool inCorners = point.IsInsideRectangle(a, b, c, d);
bool inArray = point.IsInsideRectangle(corners);

RandomBetween

Draws with UnityEngine.Random. Vector2 uses the float Range, so y is excluded. Vector2Int excludes y unless inclusiveMax is true.

float sample = range.RandomBetween();
int index = rangeInt.RandomBetween();
int inclusive = rangeInt.RandomBetween(true);

Scene / coroutines

GetRootComponent

First T under the scene's root objects. Active objects are searched first. Inactive objects are used only when no active match exists. An invalid or unloaded scene returns null.

Player player = scene.GetRootComponent<Player>();

Clone this wiki locally