yyl
2025-08-29 bb463ef0b13236738a1da9cd04f57def3e7e2c7c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
using Cysharp.Threading.Tasks;
using UnityEngine;
using UnityEngine.UI;
 
//控制目标的最大最小尺寸
[ExecuteAlways]
public class LayoutElementSizeClamp : LayoutElement
{
    [SerializeField] bool m_ClampEnable;
    public bool clampEnable { get { return m_ClampEnable; } set { m_ClampEnable = value; } }
 
    [SerializeField] Clamp m_Clamp;
    public Clamp clamp { get { return m_Clamp; } }
 
    [SerializeField] RectTransform m_Target;
 
 
    protected override void OnEnable()
    {
        base.OnEnable();
        if (clampEnable && m_Target != null)
        {
            UpdateRect().Forget();
        }
 
    }
 
    async UniTask UpdateRect()
    {
        await UniTask.DelayFrame(1);
        preferredHeight = -1;
        preferredWidth = -1;
        //频繁调用ForceRebuildLayoutImmediate的原因是
        // 如Text要自适应宽高又要限制宽高, 先要处理宽度刷新后才能计算换行
        // 强制重新计算布局
        LayoutRebuilder.ForceRebuildLayoutImmediate(transform.parent as RectTransform);
        preferredWidth = Mathf.Clamp(m_Target.rect.width, m_Clamp.minX, m_Clamp.maxX);
        // 强制重新计算布局
        LayoutRebuilder.ForceRebuildLayoutImmediate(transform.parent as RectTransform);
        preferredHeight = Mathf.Clamp(m_Target.rect.height, m_Clamp.minY, m_Clamp.maxY);
 
    }
 
 
    [System.Serializable]
    public struct Clamp
    {
        public float minX;
        public float maxX;
        public float minY;
        public float maxY;
    }
 
}