using UnityEngine;
using System.Collections;
using System.Collections.Generic;
[ExecuteInEditMode]
public class UIGridFollowsResize : MonoBehaviour
{
private UIWidget m_Widget;
private UIPanel m_Panel;
private UIGrid m_uiGrid;
public float numOfColumns = 4.0f;
public float numOfRows = 3.0f;
private List
<Transform
> m_childList
= new List
<Transform
>();
void Start()
{
m_Widget = GetComponent<UIWidget>();
m_uiGrid = GetComponent<UIGrid>();
}
public void updateGrid()
{
if (m_uiGrid == null) return;
float newHeight;
float newWidth;
float newScale;
if (numOfRows > 0)
{
newHeight = m_Widget.height / numOfRows;
}else{
newHeight = 0;
}
newWidth = m_Widget.width / numOfColumns;
m_uiGrid.cellWidth = newWidth ;
m_uiGrid.cellHeight = newHeight;
m_childList = m_uiGrid.GetChildList();
foreach (Transform child in m_childList)
{
UIWidget childWidget = child.GetComponent<UIWidget>();
if((newWidth - (AMTools.isHD ? 80f : 40f)) / childWidget.width < (newHeight - (AMTools.isHD ? 60f : 30f)) / childWidget.height){
newScale = (newWidth - (AMTools.isHD ? 80f : 40f)) / childWidget.width;
}else{
newScale = (newHeight - (AMTools.isHD ? 60f : 30f)) / childWidget.height;
}
child.localScale = Vector3.one * newScale;
}
}
void Update()
{
if (m_Panel == null) m_Panel = m_Widget.panel;
}
public void ExecuteUpdateAction(){
updateGrid();
}
public bool update = false;
void OnValidate(){
if(update == true){
updateGrid();
update = false;
}
}
}