Prevent expandable object from shrinking
I have a score board. It is relatively sized using the vertical layout group. The container keeps growing and shrinking though since the size of each digit is different.
ie: 2 is wider than 1.
Any suggestions?
I currently have two ideas but am having trouble editing the widths of objects...
1. Set width of container/text if the characters change.
2. Create a min-width script for the container and update it everytime the width increases.
problem2.gif
(483.2 kB)
Comment
Best Answer
Answer by aloften · Jan 15, 2020 at 07:22 AM
Wait. I figured it out...
If anyone else needs a dynamic min-width, this script should work. :)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ScoreSizer : MonoBehaviour
{
private RectTransform thatRect;
private LayoutElement thatLayout;
// Start is called before the first frame update
void Start() {
thatRect = gameObject.GetComponent<RectTransform>();
thatLayout = gameObject.GetComponent<LayoutElement>();
}
// Update is called once per frame
void Update() {
if (thatRect.rect.width > thatLayout.minWidth) {
thatLayout.minWidth = thatRect.rect.width;
}
}
}