Last active
April 12, 2021 03:26
-
-
Save kanchi0914/af937e71da132d53ed8290f4fd27e25a to your computer and use it in GitHub Desktop.
test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using UnityEngine.UI; | |
using UniRx; | |
public class GridItemSizeSetter : MonoBehaviour | |
{ | |
[SerializeField] | |
private int rowCount = 3; | |
[SerializeField] | |
private int columnCount = 3; | |
public float CellHeight | |
{ | |
get | |
{ | |
return (int)((rectTransform.sizeDelta.y - (gridLayout.padding.top + gridLayout.padding.bottom) | |
- gridLayout.spacing.y * (rowCount - 1)) / rowCount); | |
} | |
} | |
public int CellWidth | |
{ | |
get | |
{ | |
return (int)((rectTransform.sizeDelta.x - (gridLayout.padding.left + gridLayout.padding.right) | |
- gridLayout.spacing.x * (columnCount - 1)) / columnCount); | |
} | |
} | |
private RectTransform rectTransform; | |
private GridLayoutGroup gridLayout; | |
void Start() | |
{ | |
rectTransform = GetComponent<RectTransform>(); | |
gridLayout = GetComponent<GridLayoutGroup>(); | |
gameObject.ObserveEveryValueChanged(_ => rectTransform.sizeDelta).Subscribe(_ => UpdateCellSize()); | |
gameObject.ObserveEveryValueChanged(_ => gridLayout.spacing).Subscribe(_ => UpdateCellSize()); | |
gameObject.ObserveEveryValueChanged(_ => gridLayout.padding.left).Subscribe(_ => UpdateCellSize()); | |
gameObject.ObserveEveryValueChanged(_ => gridLayout.padding.right).Subscribe(_ => UpdateCellSize()); | |
gameObject.ObserveEveryValueChanged(_ => gridLayout.padding.top).Subscribe(_ => UpdateCellSize()); | |
gameObject.ObserveEveryValueChanged(_ => gridLayout.padding.bottom).Subscribe(_ => UpdateCellSize()); | |
} | |
private void UpdateCellSize() | |
{ | |
gridLayout.cellSize = new Vector2(CellWidth, CellHeight); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment