Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to modify RectTransform properties in script [Unity 4.6 beta]

Tags:

unity3d

enter image description here

Hello, I'm using the new UI system from Unity 4.6 beta...

Tried different codes, looked at the docs and searched around but can't find the answer…

For example. I have an image and I want to change width and height during runtime.

public GameObject image4;
image4.GetComponent<RectTransform>().rect.Set(0,0,100, 300);

which doesn't work. And image4.GetComponent().rect.y is GET only so cannot be changed at runtime.

I've also tried:

image4.transform.localScale.y = 15;

which doesn't work either.

what is the proper way to change size at runtime? You can give me an example in either JS or C#, doesn't matter.

like image 435
Anh Bảy Avatar asked Oct 17 '14 10:10

Anh Bảy


People also ask

How do you get a rect transform of an object in unity?

RectTransform myRectTransform = btnExit. GetComponent<RectTransform>(); Note: You should only do this after calling SetParent , since SetParent will make Unity automatically attach RectTransform to the btnExit variable if the Parent Object is a UI Object such as Canvas.

How do I change the rect transform position in unity?

For a non-stretching Rect Transform, the position is set most easily by setting the anchoredPosition and the sizeDelta properties. The anchoredPosition specifies the position of the pivot relative to the anchors. The sizeDelta is just the same as the size when there's no stretching.


2 Answers

One day researching. I found an extension that can help us to deal with new UI system. u can improve this extension if you want.

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));
    }
}

The source code I found from here: http://orbcreation.com

like image 87
Anh Bảy Avatar answered Oct 20 '22 15:10

Anh Bảy


Hey Friend Just Try This To Change Position and Width of an UI gameObject

if you want to use instance of an object use This leftButton.GetComponent<RectTransform>().anchoredPosition = new Vector2(-125, -36f); rightButton.GetComponent<RectTransform>().sizeDelta = new Vector2(x, y);

And if want to put script on a UI object then try this to change height and width

GetComponent<RectTransform>().anchoredPosition = new Vector2(-125, -36f);
GetComponent<RectTransform>().sizeDelta = new Vector2(x, y)
like image 23
SogekingSenpai Avatar answered Oct 20 '22 16:10

SogekingSenpai