12345678910111213141516171819202122232425262728293031323334 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- public class UIContentFitter : MonoBehaviour
- {
- // Start is called before the first frame update
- void Start()
- {
- Fit();
- }
- // Update is called once per frame
- void Update()
- {
- }
- public void Fit()
- {
- HorizontalLayoutGroup hg = GetComponent<HorizontalLayoutGroup>();
- int childCount = transform.childCount - 1;
- float childWidth = transform.GetChild(0).GetComponent<RectTransform>().rect.width;
- float width = hg.spacing * childCount +
- childCount * childWidth +
- hg.padding.left +
- childWidth;
- Vector2 size = GetComponent<RectTransform>().sizeDelta;
- GetComponent<RectTransform>().sizeDelta = new Vector2(width, size.y);
- }
- }
|