Add project files.
This commit is contained in:
36
Assets/Cryville/Common/Unity/UI/GridLayoutSizeFitter.cs
Normal file
36
Assets/Cryville/Common/Unity/UI/GridLayoutSizeFitter.cs
Normal file
@@ -0,0 +1,36 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace Cryville.Common.Unity.UI {
|
||||
[RequireComponent(typeof(GridLayoutGroup))]
|
||||
public class GridLayoutSizeFitter : MonoBehaviour {
|
||||
RectTransform rectTransform;
|
||||
GridLayoutGroup gridLayoutGroup;
|
||||
Canvas canvas;
|
||||
public int GroupItemCount = 3;
|
||||
|
||||
#pragma warning disable IDE0051
|
||||
void Awake() {
|
||||
rectTransform = GetComponent<RectTransform>();
|
||||
gridLayoutGroup = GetComponent<GridLayoutGroup>();
|
||||
canvas = GetComponentInParent<Canvas>();
|
||||
}
|
||||
void Update() {
|
||||
var cellSize = gridLayoutGroup.cellSize;
|
||||
var frameSize = rectTransform.sizeDelta;
|
||||
int lineCount = Mathf.CeilToInt((float)transform.childCount / GroupItemCount);
|
||||
var rect = RectTransformUtility.PixelAdjustRect((RectTransform)transform, canvas);
|
||||
if (gridLayoutGroup.startAxis == GridLayoutGroup.Axis.Horizontal) {
|
||||
cellSize.x = rect.width / GroupItemCount;
|
||||
frameSize.y = cellSize.y * lineCount;
|
||||
}
|
||||
else {
|
||||
cellSize.y = rect.height / GroupItemCount;
|
||||
frameSize.x = cellSize.x * lineCount;
|
||||
}
|
||||
gridLayoutGroup.cellSize = cellSize;
|
||||
rectTransform.sizeDelta = frameSize;
|
||||
}
|
||||
#pragma warning restore IDE0051
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user