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

后端 未结 7 935
面向向阳花
面向向阳花 2020-12-24 15:03

\"enter

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

Tried diffe

7条回答
  •  感动是毒
    2020-12-24 15:26

    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().rect.width, GetComponent().rect.height);
        ZoomedOutPos = new Vector2(GetComponent().localPosition.x, GetComponent().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().MaxSize.x - ZoomedOutSize.x, gameObject.GetComponentInParent().MaxSize.y - ZoomedOutSize.y);
        ZoomPosRate = -GetComponent().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().sizeDelta = new Vector2(ZoomedOutSize.x + ZoomSizeRate.x * Zoom, ZoomedOutSize.y + ZoomSizeRate.y * Zoom);
        GetComponent().localPosition = new Vector2(ZoomedOutPos.x + ZoomPosRate.x * Zoom, ZoomedOutPos.y + ZoomPosRate.y * Zoom);
    }
    

提交回复
热议问题