- Home /
unity ui Layout Element Max Size
In unity ui , LayoutElement has min , prefered and flxible size . but do not have max size property . for Example if i have a text1 and
layoutElement.flxibleWith = 1
layoutElement.minHeight = 19
text1 with one line txt:
but when i load text in text1 it continue spread it height with no limits.
So i made a script to handle this problem
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
[ExecuteInEditMode]
[RequireComponent(typeof(LayoutElement))]
public class LayoutElementMaxSize : MonoBehaviour
{
private LayoutElement layoutElement;
private ContentSizeFitter contentSizeFitter;
private RectTransform rectransform;
public bool controllWidth;
public bool controllHeight;
public float maxHight;
public float maxWidth;
void Start()
{
layoutElement = GetComponent<LayoutElement>();
rectransform = GetComponent<RectTransform>();
}
public void Update()
{
if(rectransform.hasChanged)
{
rectransform.hasChanged = false;
if (controllHeight)
{
layoutElement.preferredHeight = -1;
layoutElement.CalculateLayoutInputHorizontal();
layoutElement.CalculateLayoutInputVertical();
if (rectransform.rect.height >= maxHight)
{
layoutElement.preferredHeight = maxHight;
}
}
if (controllWidth)
{
if (rectransform.rect.height >= maxWidth)
{
layoutElement.preferredWidth = maxWidth;
}
else
{
layoutElement.preferredWidth = -1;
}
}
}
}
}
but it is not full filing my requirement plz take a look on it what am i doing wrong ....
gf1en.png
(18.8 kB)
untitled.png
(20.3 kB)
Comment
Your answer
Follow this Question
Related Questions
I don't understand "Child Force Expand" on uGUI 2 Answers
UI 4.6: custom anchors breaking sizeDelta 0 Answers
Example for Text -> Material usage 0 Answers
Side scrolling menu 0 Answers
Combining aspect ratio fitter with horizontal layout group 0 Answers