blog games developers documentation portfolio gallery

More in this category:

RectTransform extensions

When your head hurts after setting up a user interface with the new UI from Unity this is likely caused by the offsetMin, offsetMax, pivot, anchorMin and anchorMax properties that together defines the position and size of a user interface control. If you long for the days when you could simply position a button by telling it the Rect where it should be drawn, here's a little something to help.

I wrote most of these functions when I build the ListBox package for the Unity3D AssetStore. All functions can be found in that package as well. here's the link to the ListBox package in the AssetStore and here's where you can find the package documentation.

void SetDefaultScale()


Set the scale to 1,1,1

void SetPivotAndAnchors(Vector2 aVec)


Set the point in which both anchors and the pivot should be placed. This makes it very easy to set positions and scales, but it destroys autoscaling.

Vector2 GetSize()


Get the current size of the RectTransform as a Vector2

float GetWidth() float GetHeight()


Get either width or height of the RectTransform

void SetPositionOfPivot(Vector2 newPos)


Set the position of the RectTransform within it's parent's coordinates. Depending on the position of the pivot, the RectTransform actual position will differ.

void SetLeftBottomPosition(Vector2 newPos)
void SetLeftTopPosition(Vector2 newPos)
void SetRightBottomPosition(Vector2 newPos)
void SetRightTopPosition(Vector2 newPos)


Now we're talking. Set the position of a specific corner of the RectTransform within it's parent's coordinates.

void SetSize(Vector2 newSize)
void SetWidth(float newWidth)
void SetHeight(float newHeight)


Set the dimensions of the RectTransform regardless of its anchors, pivot and offsets.


using UnityEngine;
using System;
using System.Collections;

namespace MyExtensions
{
public static class RectTransformExtensions
{
public static void SetDefaultScale(this RectTransform trans) {
trans.localScale = new Vector3(1, 1, 1);
}
public static void SetPivotAndAnchors(this RectTransform trans, Vector2 aVec) {
trans.pivot = aVec;
trans.anchorMin = aVec;
trans.anchorMax = aVec;
}

public static Vector2 GetSize(this RectTransform trans) {
return trans.rect.size;
}
public static float GetWidth(this RectTransform trans) {
return trans.rect.width;
}
public static float GetHeight(this RectTransform trans) {
return trans.rect.height;
}

public static void SetPositionOfPivot(this RectTransform trans, Vector2 newPos) {
trans.localPosition = new Vector3(newPos.x, newPos.y, trans.localPosition.z);
}

public static void SetLeftBottomPosition(this RectTransform trans, Vector2 newPos) {
trans.localPosition = new Vector3(newPos.x + (trans.pivot.x * trans.rect.width), newPos.y + (trans.pivot.y * trans.rect.height), trans.localPosition.z);
}
public static void SetLeftTopPosition(this RectTransform trans, Vector2 newPos) {
trans.localPosition = new Vector3(newPos.x + (trans.pivot.x * trans.rect.width), newPos.y - ((1f - trans.pivot.y) * trans.rect.height), trans.localPosition.z);
}
public static void SetRightBottomPosition(this RectTransform trans, Vector2 newPos) {
trans.localPosition = new Vector3(newPos.x - ((1f - trans.pivot.x) * trans.rect.width), newPos.y + (trans.pivot.y * trans.rect.height), trans.localPosition.z);
}
public static void SetRightTopPosition(this RectTransform trans, Vector2 newPos) {
trans.localPosition = new Vector3(newPos.x - ((1f - trans.pivot.x) * trans.rect.width), newPos.y - ((1f - trans.pivot.y) * trans.rect.height), trans.localPosition.z);
}

public static void SetSize(this RectTransform trans, Vector2 newSize) {
Vector2 oldSize = trans.rect.size;
Vector2 deltaSize = newSize - oldSize;
trans.offsetMin = trans.offsetMin - new Vector2(deltaSize.x * trans.pivot.x, deltaSize.y * trans.pivot.y);
trans.offsetMax = trans.offsetMax + new Vector2(deltaSize.x * (1f - trans.pivot.x), deltaSize.y * (1f - trans.pivot.y));
}
public static void SetWidth(this RectTransform trans, float newSize) {
SetSize(trans, new Vector2(newSize, trans.rect.size.y));
}
public static void SetHeight(this RectTransform trans, float newSize) {
SetSize(trans, new Vector2(trans.rect.size.x, newSize));
}

}
}






follow us