42 lines
1.4 KiB
C#
42 lines
1.4 KiB
C#
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
public class Alignment : MonoBehaviour
|
|
{
|
|
private GridLayoutGroup _gridLayoutGroup;
|
|
private RectTransform _rectTransform;
|
|
|
|
void Start()
|
|
{
|
|
_gridLayoutGroup = GetComponent<GridLayoutGroup>();
|
|
_rectTransform = GetComponent<RectTransform>();
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
// 아바타가 하나도 없으면 계산할 필요 없음
|
|
if (transform.childCount == 0) return;
|
|
|
|
// 1. 가로, 세로에 각각 '온전히' 몇 개가 들어갈 수 있는지 계산 (내림 처리)
|
|
// (괄호 위치를 정확히 맞추었습니다!)
|
|
float cols = Mathf.Floor(_rectTransform.rect.width / (_gridLayoutGroup.cellSize.x + _gridLayoutGroup.spacing.x));
|
|
float rows = Mathf.Floor(_rectTransform.rect.height / (_gridLayoutGroup.cellSize.y + _gridLayoutGroup.spacing.y));
|
|
|
|
// 최소 1줄은 보장되도록 안전장치
|
|
if (cols < 1) cols = 1;
|
|
if (rows < 1) rows = 1;
|
|
|
|
// 2. 현재 화면에 들어갈 수 있는 최대 아바타 개수
|
|
float max = cols * rows;
|
|
float current = transform.childCount;
|
|
|
|
// 3. 꽉 차기 직전(80% 이상)이면 셀 사이즈를 10%씩 축소
|
|
if (current / max >= 0.8f)
|
|
{
|
|
_gridLayoutGroup.cellSize = new Vector2(
|
|
_gridLayoutGroup.cellSize.x * 0.9f,
|
|
_gridLayoutGroup.cellSize.y * 0.9f
|
|
);
|
|
}
|
|
}
|
|
} |