How to modify RectTransform properties in script [

2019-02-01 11:11发布

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.

标签: unity3d
6条回答
男人必须洒脱
2楼-- · 2019-02-01 11:25

When changing parent (eg after Instantiate), things get messy, I found resetting a few parameters really helped (spent quite a bit of time trying to figure this out):

this.transform.SetParent(content.transform);
this.transform.localScale = Vector3.one;            
this.rectTransform.sizeDelta = Vector2.zero;
this.rectTransform.anchoredPosition = Vector2.zero;

Hope that helps someone else :)

查看更多
可以哭但决不认输i
3楼-- · 2019-02-01 11:28

try to use something like that [in C#]:

 ...

    RectTransform rTrans = (RectTransform) transform.GetComponent<RectTransform>();

    // set new width and height

    rTrans.anchoredPosition = new Vector2(15, 200);

    ...
查看更多
可以哭但决不认输i
4楼-- · 2019-02-01 11:30

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

查看更多
小情绪 Triste *
5楼-- · 2019-02-01 11:33

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)
查看更多
叛逆
6楼-- · 2019-02-01 11:33

You normally don't want to modify directly those values, even more if they come from a prefab you have already setup with the correct coords, if you just want to add it correctly under canvas (or other) but it "changes position" do this:

GameObject instance;

void Start () {
    GameObject canvas = GameObject.Find ("Canvas");
    if (canvas != null) {
        GameObject go = Resources.Load<GameObject>("MyPrefab");
        if(go != null){
            instance = Instantiate(go);
            instance.GetComponent<Transform>().SetParent(canvas.GetComponent<Transform>(), false);
        }
    }
}

Passing false on set parent will avoid the rect transform being changed in weird forms when instantiating the prefab.

查看更多
祖国的老花朵
7楼-- · 2019-02-01 11:37

Actuallly playing with following can help: GetComponent.sizeDelta = new Vector(new_size.x,new_size.y);

I successfully achieved Zooming in new GUI by setting this property.

Here is my own code for scaling:

    void Start()
{
    // Store initial Size and Position;
    ZoomedOutSize = new Vector2(GetComponent<RectTransform>().rect.width, GetComponent<RectTransform>().rect.height);
    ZoomedOutPos = new Vector2(GetComponent<RectTransform>().localPosition.x, GetComponent<RectTransform>().localPosition.y);
}

void Update()
{
    // Calculate the total delta at runtime cause it depends on parent (yah i know it's not optimal to have new vector ecery Update)
    ZoomSizeRate = new Vector2(gameObject.GetComponentInParent<ZoomDialog>().MaxSize.x - ZoomedOutSize.x, gameObject.GetComponentInParent<ZoomDialog>().MaxSize.y - ZoomedOutSize.y);
    ZoomPosRate = -GetComponent<RectTransform>().localPosition;

    // Zoom is the float with range(0,1) 0 - no Zoom, 1 - Fully Zoom to ParentSize;
    //Set the size delta and position as Initial + Rate * Zoom
    GetComponent<RectTransform>().sizeDelta = new Vector2(ZoomedOutSize.x + ZoomSizeRate.x * Zoom, ZoomedOutSize.y + ZoomSizeRate.y * Zoom);
    GetComponent<RectTransform>().localPosition = new Vector2(ZoomedOutPos.x + ZoomPosRate.x * Zoom, ZoomedOutPos.y + ZoomPosRate.y * Zoom);
}
查看更多
登录 后发表回答