-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWrapContentHeightFitter.cs
More file actions
134 lines (104 loc) · 3.83 KB
/
WrapContentHeightFitter.cs
File metadata and controls
134 lines (104 loc) · 3.83 KB
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
130
131
132
133
134
using UnityEngine;
using UnityEngine.UI;
namespace ExtendUI
{
[DisallowMultipleComponent]
[ExecuteAlways]
[RequireComponent(typeof(RectTransform))]
[AddComponentMenu("Layout/Wrap Content Height Fitter", 150)]
public sealed class WrapContentHeightFitter : LayoutGroup, ILayoutSelfController
{
[Tooltip("如果目标有Text|LayoutGroup则控制其高度")]
[SerializeField, Header("包围目标")]
private RectTransform m_wrapRect;
private Text m_Text;
private Text text
{
get
{
if (!m_Text)
m_Text = m_wrapRect.GetComponent<Text>();
return m_Text;
}
}
private LayoutGroup m_layoutGroup;
private LayoutGroup layoutGroup
{
get
{
if (!m_layoutGroup)
m_layoutGroup = m_wrapRect.GetComponent<LayoutGroup>();
return m_layoutGroup;
}
}
private Vector2 m_cachedSize;
public override void CalculateLayoutInputHorizontal()
{
if (!m_wrapRect)
return;
// 缓存来保存原始宽度
m_cachedSize = m_wrapRect.rect.size;
var width = m_cachedSize.x;
if (layoutGroup)
width = LayoutUtility.GetPreferredWidth(m_wrapRect);
width += m_Padding.left + m_Padding.right;
SetLayoutInputForAxis(width, width, 0, 0);
m_Tracker.Clear();
}
public override void CalculateLayoutInputVertical()
{
if (!m_wrapRect)
return;
var height = m_cachedSize.y;
if (text || layoutGroup)
height = LayoutUtility.GetPreferredHeight(m_wrapRect);
height += m_Padding.top + m_Padding.bottom;
SetLayoutInputForAxis(height, height, 0, 1);
m_Tracker.Clear();
}
public override void SetLayoutHorizontal()
{
if (!m_wrapRect)
return;
if (!text && !layoutGroup)
rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, preferredWidth);
}
public override void SetLayoutVertical()
{
if (!m_wrapRect)
return;
rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, preferredHeight);
// 子对象设置
m_wrapRect.anchorMax = Vector2.one;
m_wrapRect.anchorMin = Vector2.zero;
m_wrapRect.offsetMax = new Vector2(-m_Padding.right, -m_Padding.top);
m_wrapRect.offsetMin = new Vector2(m_Padding.left, m_Padding.bottom);
var flags = text || layoutGroup ? DrivenTransformProperties.SizeDeltaY : DrivenTransformProperties.SizeDelta;
m_Tracker.Add(this, rectTransform, flags);
flags = DrivenTransformProperties.AnchorMin | DrivenTransformProperties.AnchorMax | DrivenTransformProperties.AnchoredPosition;
if (text || layoutGroup)
flags |= DrivenTransformProperties.SizeDelta;
m_Tracker.Add(this, m_wrapRect, flags);
}
protected override void OnValidate()
{
if (!m_wrapRect && rectTransform.childCount != 0)
m_wrapRect = rectTransform.GetChild(0) as RectTransform;
base.OnValidate();
}
#if UNITY_EDITOR
[ContextMenu("设置目标宽度")]
private void SetWrapWidth()
{
m_wrapRect.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, _width);
}
[ContextMenu("获得目标宽度")]
private void GetWrapWidth()
{
_width = m_wrapRect.rect.width;
}
[SerializeField]
private float _width = 200;
#endif
}
}