Programmatically Setting Panels to Scale In Editor

I wrote two small scripts to control the size of the bottom and right panels based on the left one. After running the game once once these scripts have built, it will automatically resize the other panels whenever you change the left one in editor mode.

Bottom:

using UnityEngine;

[ExecuteInEditMode] //this makes things happen in the editor
public class ArrowResizer : MonoBehaviour {

    private RectTransform leftRect; //left panel's recttransform
    private RectTransform thisRect; //bottom panel's recttransform
    private RectTransform resizedArrow; //arrow's recttransform. it doesn't matter which arrow you grab

    private float resizedArrowHeight; //height of the resized arrow
    private float offset = 10; //magic number to make the arrows not touch the panel
    private float sidePanelWidth; //width of leftRect

    //get things so we don't have to do it constantly during Update()
    void Awake() {
        leftRect = GameObject.Find("LeftPanel").GetComponent<RectTransform>();
        thisRect = gameObject.GetComponent<RectTransform>();
        resizedArrow = gameObject.transform.GetChild(0).GetComponent<RectTransform>();
    }

    void Update() {
        sidePanelWidth = leftRect.rect.width; //width of left panel
        resizedArrowHeight = resizedArrow.rect.height; //height of arrow you're gonna resize
        SetRect(thisRect, sidePanelWidth, resizedArrowHeight + offset, sidePanelWidth, 0); //resize the RectTransform
    }

    /// <summary>
    /// Changes the rect of whatever RectTransform is put inside.
    /// </summary>
    /// <param name="rect">The RectTransform you want to adjust.</param>
    /// <param name="left">How far this panel extends left of center, so it nearly touches the left panel.</param>
    /// <param name="top">How far this panel extends upward, so it can accomodate the arrows when resized.</param>
    /// <param name="right">How far this panel extends left of center, so it nearly touches the right panel.</param>
    /// <param name="bottom">This is being set by the </param>
    void SetRect(RectTransform rect, float left, float top, float right, float bottom) {
        rect.offsetMin = new Vector2(left, bottom);
        rect.offsetMax = new Vector2(-right, top);
    }
}

Right:

using UnityEngine;

[ExecuteInEditMode] //This makes things happen in the editor
public class RightPanelResizer : MonoBehaviour {

    private RectTransform leftRect; //left panel's RectTransform
    private RectTransform thisRect; //

    private float sidePanelWidth; 

    //find these things in Awake() so that we don't have to do it all the time in Update()
    void Awake() {
        leftRect = GameObject.Find("LeftPanel").GetComponent<RectTransform>();
        thisRect = gameObject.GetComponent<RectTransform>();
    }

    void Update() {
        sidePanelWidth = leftRect.rect.width; //get the current width of the left panel
        thisRect.rect.Set(sidePanelWidth, thisRect.rect.position.y, sidePanelWidth * 2, thisRect.rect.height); //set the x, y, width, and height of thisRect
    }
}

Should help people spend less time fiddling with UI stuff if they want to make minor adjustments.

Privacy & Terms