using UnityEngine;
using System.Collections;
[RequireComponent
(typeof(UIGrid
))] public class ExpandGridToFit : MonoBehaviour {
//WARNING: THIS COMPONENT WILL ONLY WORK ON PORTRAIT SCREENS
public float marginHeight;
private UIGrid grid =null;
private UIRoot root;
// Use this for initialization
void Start ()
{
root
= GameObject
.FindObjectOfType(typeof(UIRoot
)) as UIRoot
; grid = this.gameObject.GetComponent<UIGrid>();
}
// Update is called once per frame
void Update ()
{
//get available height
int rows = (int)Mathf.Round(((float)grid.transform.childCount / (float)grid.maxPerLine));
float availableHeight = ((float)root.manualHeight - marginHeight) / rows;
if (grid.cellHeight != availableHeight)
{
//set cellHeight
grid.cellHeight = availableHeight;
//refresh!
grid.Reposition();
//refresh childrens if I should
foreach (Transform t in transform)
{
ExpandToCellSize etcs = t.GetComponent<ExpandToCellSize>();
if (etcs != null)
{
etcs.resizeNow = true;
}
}
}
}
}