Skip to content
This repository was archived by the owner on Jul 11, 2023. It is now read-only.

Commit 72a599f

Browse files
committed
更新内容宽度计算
1 parent 348e9b7 commit 72a599f

File tree

2 files changed

+117
-6
lines changed

2 files changed

+117
-6
lines changed
0 Bytes
Binary file not shown.

Assets/TextInlineSprite/Scripts/InlineText.cs

Lines changed: 117 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,16 +58,75 @@ public class HrefClickEvent : UnityEvent<string, int> { }
5858
private List<HrefInfo> _ListHrefInfos;
5959
#endregion
6060

61+
private float? _pw;
62+
6163
public override float preferredWidth
6264
{
6365
get
6466
{
65-
var settings = GetGenerationSettings(Vector2.zero);
66-
return cachedTextGeneratorForLayout.GetPreferredWidth(_OutputText, settings) / pixelsPerUnit;
67+
if(_pw == null)
68+
{
69+
//its override from uGUI Code ,but has bug?
70+
71+
//var settings = GetGenerationSettings(Vector2.zero);
72+
//return cachedTextGeneratorForLayout.GetPreferredWidth(_OutputText, settings) / pixelsPerUnit;
73+
74+
//next idea
75+
Vector2 extents = rectTransform.rect.size;
76+
77+
var settings = GetGenerationSettings(extents);
78+
cachedTextGenerator.Populate(_OutputText, settings);
79+
80+
if(cachedTextGenerator.lineCount > 1)
81+
{
82+
float? minx = null;
83+
float? maxx = null;
84+
IList<UIVertex> verts = cachedTextGenerator.verts;
85+
int maxIndex = cachedTextGenerator.lines[1].startCharIdx;
86+
87+
for (int i = 0, index = 0; i < verts.Count; i += 4, index++)
88+
{
89+
UIVertex v0 = verts[i];
90+
UIVertex v2 = verts[i + 1];
91+
float min = v0.position.x;
92+
float max = v2.position.x;
93+
94+
if (minx.HasValue == false)
95+
{
96+
minx = min;
97+
}
98+
else
99+
{
100+
minx = Mathf.Min(minx.Value, min);
101+
}
102+
103+
if (maxx.HasValue == false)
104+
{
105+
maxx = max;
106+
}
107+
else
108+
{
109+
maxx = Mathf.Max(maxx.Value, max);
110+
}
111+
112+
if (index > maxIndex)
113+
{
114+
break;
115+
}
116+
}
117+
118+
_pw = (maxx - minx);
119+
}
120+
else
121+
{
122+
_pw = cachedTextGeneratorForLayout.GetPreferredWidth(_OutputText, settings) / pixelsPerUnit;
123+
}
124+
125+
}
126+
return _pw.Value;
67127
}
68128
}
69129

70-
71130
public override float preferredHeight
72131
{
73132
get
@@ -77,6 +136,55 @@ public override float preferredHeight
77136
}
78137
}
79138

139+
void OnDrawGizmos()
140+
{
141+
Gizmos.color = Color.blue;
142+
143+
Vector3 size = new Vector3(preferredWidth, preferredHeight, 0);
144+
Vector3 fixsize = transform.TransformDirection(rectTransform.rect.size);
145+
Vector3 textsize =transform.TransformDirection(size);
146+
147+
Vector3 fixpos = transform.position ;
148+
if(this.alignment == TextAnchor.LowerCenter)
149+
{
150+
fixpos += new Vector3(0, -0.5f * (fixsize.y - textsize.y) , 0);
151+
}
152+
else if(this.alignment == TextAnchor.LowerLeft)
153+
{
154+
fixpos += new Vector3(-0.5f * (fixsize.x - textsize.x), -0.5f * (fixsize.y - textsize.y), 0);
155+
}
156+
else if (this.alignment == TextAnchor.LowerRight)
157+
{
158+
fixpos += new Vector3(0.5f * (fixsize.x - textsize.x), -0.5f * (fixsize.y - textsize.y), 0);
159+
}
160+
else if (this.alignment == TextAnchor.MiddleCenter)
161+
{
162+
fixpos += new Vector3(0, 0, 0);
163+
}
164+
else if (this.alignment == TextAnchor.MiddleLeft)
165+
{
166+
fixpos += new Vector3(-0.5f * (fixsize.x - textsize.x),0, 0);
167+
}
168+
else if (this.alignment == TextAnchor.MiddleRight)
169+
{
170+
fixpos += new Vector3(0.5f * (fixsize.x - textsize.x), 0, 0);
171+
}
172+
else if (this.alignment == TextAnchor.UpperCenter)
173+
{
174+
fixpos += new Vector3(0, 0.5f * (fixsize.y - textsize.y), 0);
175+
}
176+
else if (this.alignment == TextAnchor.UpperLeft)
177+
{
178+
fixpos += new Vector3(-0.5f * (fixsize.x - textsize.x), 0.5f * (fixsize.y - textsize.y), 0);
179+
}
180+
else if (this.alignment == TextAnchor.UpperRight)
181+
{
182+
fixpos += new Vector3(0.5f * (fixsize.x - textsize.x), 0.5f * (fixsize.y - textsize.y), 0);
183+
}
184+
185+
Gizmos.DrawWireCube(fixpos, textsize);
186+
}
187+
80188
protected override void Start()
81189
{
82190
base.Start();
@@ -96,7 +204,8 @@ public override void SetVerticesDirty()
96204
{
97205
base.SetVerticesDirty();
98206
if (!Application.isPlaying || !Manager)
99-
{
207+
{
208+
_pw = null;
100209
_OutputText = m_Text;
101210
return;
102211
}
@@ -105,12 +214,14 @@ public override void SetVerticesDirty()
105214
if(RenderTagList != null && RenderTagList.Count >0)
106215
{
107216
_OutputText = outtext;
108-
needupdate = true;
217+
needupdate = true;
218+
_pw = null;
109219
}
110220
else
111221
{
112222
_OutputText = m_Text;
113-
needupdate = true;
223+
needupdate = true;
224+
_pw = null;
114225
}
115226
}
116227

0 commit comments

Comments
 (0)