微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

找不到UI布局元素的最大大小

在Unity UI中,LayoutElement具有最小,首选和固定大小,但没有max size属性.

例如,如果我有一个text1和

layoutElement.flxibleWith = 1
layoutElement.minHeight  = 19

具有一行txt的text1:

text1 with one line txt

但是,当我在text1中加载文本时,它将继续无限制地扩展其高度:

enter image description here

我已经编写了一个脚本:

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;
            }
        }
    }
}}

但是还没有完全提交我的要求,请看一下..

解决方法:

我知道这是一个老问题,但是我一直在寻找类似的东西,所以我最终重写了您的课程,现在对我来说很好用.我不仅使另一个MonoBehavIoUr覆盖了LayoutElement,而且还添加一个自定义检查器以使其易于编辑.希望我的解决方案可以帮助您或任何其他想要这样的人.

using UnityEngine;
using UnityEngine.UI;

#if UNITY_EDITOR
using UnityEditor;
#endif

[RequireComponent(typeof(RectTransform))]
[System.Serializable]
public class LayoutMaxSize : LayoutElement
{
    public float maxHeight = -1;
    public float maxWidth = -1;

    public override void CalculateLayoutInputHorizontal()
    {
        base.CalculateLayoutInputHorizontal();
        UpdateMaxSizes();
    }

    public override void CalculateLayoutInputVertical()
    {
        base.CalculateLayoutInputVertical();
        UpdateMaxSizes();
    }

    protected override void OnRectTransformDimensionsChange()
    {
        base.OnRectTransformDimensionsChange();
        UpdateMaxSizes();
    }

    protected override void OnValidate()
    {
        base.OnValidate();
        UpdateMaxSizes();
    }

    private void UpdateMaxSizes()
    {
        if (maxHeight != -1)
        {
            if (preferredHeight == -1 && maxHeight < GetComponent<RectTransform>().sizeDelta.y)
            {
                preferredHeight = maxHeight;
            }
            else if (preferredHeight != -1 && transform.childCount > 0)
            {
                bool first = true;
                float biggestY = 0;
                float lowestY = 0;
                for (int i = 0; i < transform.childCount; i++)
                {
                    var childrenTransform = transform.GetChild(i).GetComponent<RectTransform>();
                    if (childrenTransform == null) continue;
                    var childPos = childrenTransform.localPosition;
                    var childSize = childrenTransform.sizeDelta;
                    var childPivot = childrenTransform.pivot;
                    if(first)
                    {
                        biggestY = childPos.y + (childSize.y * (1f - childPivot.y));
                        lowestY = childPos.y - (childSize.y * childPivot.y);
                    }
                    else
                    {
                        biggestY = Mathf.Max(biggestY, childPos.y + (childSize.y * (1f - childPivot.y)));
                        lowestY = Mathf.Min(lowestY, childPos.y - (childSize.y * childPivot.y));
                    }
                    first = false;
                }
                if (first) return;
                var childrenYSize = Mathf.Abs(biggestY - lowestY);
                if(preferredHeight > childrenYSize)
                {
                    preferredHeight = -1;
                }
            }
        }
        if (maxWidth != -1)
        {
            if (preferredWidth == -1 && maxWidth < GetComponent<RectTransform>().sizeDelta.x)
            {
                preferredWidth = maxWidth;
            }
            else if (preferredWidth != -1 && transform.childCount > 0)
            {
                bool first = true;
                float biggestX = 0;
                float lowestX = 0;
                for (int i = 0; i < transform.childCount; i++)
                {
                    var childrenTransform = transform.GetChild(i).GetComponent<RectTransform>();
                    if (childrenTransform == null) continue;
                    var childPos = childrenTransform.localPosition;
                    var childSize = childrenTransform.sizeDelta;
                    var childPivot = childrenTransform.pivot;
                    if (first)
                    {
                        biggestX = childPos.x + (childSize.x * (1f - childPivot.x));
                        lowestX = childPos.x - (childSize.x * childPivot.x);
                    }
                    else
                    {
                        biggestX = Mathf.Max(biggestX, childPos.x + (childSize.x * (1f - childPivot.x)));
                        lowestX = Mathf.Min(lowestX, childPos.x - (childSize.x * childPivot.x));
                    }
                    first = false;
                }
                if (first) return;
                var childrenXSize = Mathf.Abs(biggestX - lowestX);
                if (preferredWidth > childrenXSize)
                {
                    preferredWidth = -1;
                }
            }
        }
    }
}

#if UNITY_EDITOR
[CustomEditor(typeof(LayoutMaxSize))]
public class LayoutMaxSizeEditor : Editor
{
    public override void OnInspectorGUI()
    {
        LayoutMaxSize layoutMax = target as LayoutMaxSize;
        EditorGUILayout.BeginHorizontal();
        EditorGUILayout.PrefixLabel("Ignore Layout");
        layoutMax.ignoreLayout = EditorGUILayout.Toggle(layoutMax.ignoreLayout);
        EditorGUILayout.EndHorizontal();
        if (!layoutMax.ignoreLayout)
        {
            EditorGUILayout.Space();
            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.PrefixLabel("Min Width");
            var allowMinWidth = EditorGUILayout.Toggle(layoutMax.minWidth != -1);
            if (allowMinWidth)
            {
                if (layoutMax.minWidth == -1) layoutMax.minWidth = 0;
                layoutMax.minWidth = EditorGUILayout.FloatField(layoutMax.minWidth);
            }
            else if (layoutMax.minWidth != -1)
            {
                layoutMax.minWidth = -1;
            }
            EditorGUILayout.EndHorizontal();

            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.PrefixLabel("Min Height");
            var allowMinHeight = EditorGUILayout.Toggle(layoutMax.minHeight != -1);
            if (allowMinHeight)
            {
                if (layoutMax.minHeight == -1) layoutMax.minHeight = 0;
                layoutMax.minHeight = EditorGUILayout.FloatField(layoutMax.minHeight);
            }
            else if (layoutMax.minHeight != -1)
            {
                layoutMax.minHeight = -1;
            }
            EditorGUILayout.EndHorizontal();

            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.PrefixLabel("Max Width");
            var allowMaxWidth = EditorGUILayout.Toggle(layoutMax.maxWidth != -1);
            if (allowMaxWidth)
            {
                if (layoutMax.maxWidth == -1) layoutMax.maxWidth = Mathf.Max(0, layoutMax.minWidth);
                layoutMax.maxWidth = Mathf.Max(EditorGUILayout.FloatField(layoutMax.maxWidth), layoutMax.minWidth);
            }
            else if(layoutMax.maxWidth != -1)
            {
                layoutMax.maxWidth = -1;
            }
            EditorGUILayout.EndHorizontal();

            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.PrefixLabel("Max Height");
            var allowMaxHeight = EditorGUILayout.Toggle(layoutMax.maxHeight != -1);
            if (allowMaxHeight)
            {
                if (layoutMax.maxHeight == -1) layoutMax.maxHeight = Mathf.Max(0, layoutMax.minHeight);
                layoutMax.maxHeight = Mathf.Max(EditorGUILayout.FloatField(layoutMax.maxHeight), layoutMax.minHeight);
            }
            else if (layoutMax.maxHeight != -1)
            {
                layoutMax.maxHeight = -1;
            }
            EditorGUILayout.EndHorizontal();
        }
    }
}
#endif

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐