• 【Unity】LayoutGroup自动缩放子对象大小


    需求
    如果LayoutGroup的子物体太多,超出layoutGroup的范围,则对子物体进行缩小。确保子物体都在LayoutGroup的范围内

    代码:

    
    [ExecuteInEditMode]
    [RequireComponent(typeof(HorizontalOrVerticalLayoutGroup))]
    public class LayoutGroupChildFitter : MonoBehaviour, ILayoutController
    {
        [Header("最小缩放尺寸,小于0的话脚本失效")]
        public float m_minChildSize = -1f;
        [Header("是否为水平布局")]
        public bool m_isHorizontal = true;
        //layoutGroup
        [SerializeField] HorizontalOrVerticalLayoutGroup m_layout = null;
        [SerializeField] RectTransform m_rectTransform;
        float m_curScale = 0;
    
        //子节点
        List<RectTransform> m_rectChildren = new List<RectTransform>();
        private void Start()
        {
            if (m_layout == null)
                m_layout = this.transform.GetComponent<HorizontalOrVerticalLayoutGroup>();
            if (m_rectTransform == null) 
                m_rectTransform = this.GetComponent<RectTransform>();
            DoFixChildSize();
    
        }
    
    
        void ILayoutController.SetLayoutHorizontal()
        {
            if (m_layout == null)
                m_layout = this.transform.GetComponent<HorizontalOrVerticalLayoutGroup>();
            if (m_rectTransform == null)
                m_rectTransform = this.GetComponent<RectTransform>();
            DoFixChildSize();
        }
    
        void ILayoutController.SetLayoutVertical()
        {
            if (m_layout == null)
                m_layout = this.transform.GetComponent<HorizontalOrVerticalLayoutGroup>();
            if (m_rectTransform == null)
                m_rectTransform = this.GetComponent<RectTransform>();
            DoFixChildSize();
        }
    
        /// 
        /// 子节点缩放
        /// 
        void DoFixChildSize()
        {
            if (m_minChildSize < 0) return;
            GetRectChildren();
            float childrenSize = GetChildrenTotalSize();
            float contentSize = GetContentSize();
            float scale = contentSize / childrenSize;
            scale = Mathf.Clamp(scale, m_minChildSize, 1);
            if (m_curScale != scale)
            {
                Debug.Log($"Layout Child 缩放{contentSize}/{childrenSize}=scale:{scale}");
                Vector3 localScale = Vector3.one * scale;
                SetChildrenChild(localScale);
                m_curScale = scale;
                LayoutRebuilder.ForceRebuildLayoutImmediate(m_rectTransform);
            }
        }
    
        /// 
        /// 把所有子节点设置
        /// 
        /// 
        void SetChildrenChild(Vector3 localScale)
        {
            for (int i = 0; i < m_rectChildren.Count; i++)
            {
                m_rectChildren[i].localScale = localScale;
            }
        }
    
        /// 
        /// 计算容器尺寸
        /// 
        /// 
        float GetContentSize()
        {
            float size = (m_isHorizontal) ? m_rectTransform.rect.width : m_rectTransform.rect.height;
            //边距
            float padding = (m_isHorizontal) ?
                m_layout.padding.right + m_layout.padding.left :
                m_layout.padding.top + m_layout.padding.bottom;
            //总间距
            float spacing = (m_rectChildren.Count - 1) * m_layout.spacing;
            return size - padding - spacing;
        }
    
        /// 
        /// 计算子物体总尺寸
        /// 
        /// 
        float GetChildrenTotalSize()
        {
    
            //总长度
            float total = 0;
            for (int i = 0; i < m_rectChildren.Count; i++)
            {
                if (!m_rectChildren[i].gameObject.activeInHierarchy) continue;  //不考虑隐藏
                if (m_isHorizontal) total += m_rectChildren[i].sizeDelta.x;
                else total += m_rectChildren[i].sizeDelta.y;
            }
    
            return total;
        }
    
    
        /// 
        /// 获取子物体
        /// 
        void GetRectChildren()
        {
            if (transform.childCount == m_rectChildren.Count) return;
            m_rectChildren.Clear();
            for (int i = 0; i < transform.childCount; i++)
            {
                RectTransform rect = transform.GetChild(i).GetComponent<RectTransform>();
                if (!m_rectChildren.Contains(rect)) m_rectChildren.Add(rect);
            }
        }
    }
    
    
    • 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
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
  • 相关阅读:
    vue、vuex状态管理
    1498. 满足条件的子序列数目-快速排序+二分查找+快速幂-力扣双百代码
    webpack基础配置及使用
    I2C通信协议
    GitHub最新发布,阿里十年架构师手写版spring全家桶笔记全新开源
    Python in Visual Studio Code 2023年9月更新
    代码规范性思考
    【转】2022年“云计算的11类顶级威胁”报告
    [管理与领导-108]:IT人看清职场中的隐性规则 - 5 - 你会在不经意间被归属在不同的分类中,一旦分类定型,你就会被打上了某种标签(职场分类方法大全)
    07 单件(Singleton)模式
  • 原文地址:https://blog.csdn.net/qq_33205561/article/details/134326078