public class WidgetLimitSize : MonoBehaviour {
private UIWidget widget;
public int maxWidth = 0;
public int maxHeight = 0;
private int aLeft = 0;
private int aRight = 0;
private int aTop = 0;
private int aBottom = 0;
void Awake () {
widget = GetComponent<UIWidget> ();
aLeft = widget.leftAnchor.absolute;
aRight = widget.rightAnchor.absolute;
aTop = widget.topAnchor.absolute;
aBottom = widget.bottomAnchor.absolute;
CameraRotation cam = Camera.main.GetComponent<CameraRotation> ();
cam.OnUpdate += OnUpdate;
}
private void OnUpdate(bool isPortrait, int width, int height){
if (maxWidth != 0) {
widget.leftAnchor.absolute = aLeft;
widget.rightAnchor.absolute = aRight;
}
if (maxHeight != 0) {
widget.topAnchor.absolute = aTop;
widget.bottomAnchor.absolute = aBottom;
}
widget.UpdateAnchors();
}
void LateUpdate () {
CheckBounds ();
}
private void CheckBounds(){
if (maxWidth != 0 && widget.width >= maxWidth) {
widget.width = maxWidth;
}
if (maxHeight != 0 && widget.height >= maxHeight) {
widget.height = maxHeight;
}
}
}