-
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathTextPro.cs
More file actions
208 lines (172 loc) · 6.66 KB
/
TextPro.cs
File metadata and controls
208 lines (172 loc) · 6.66 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*
* Copyright (c) scott.cgi All Rights Reserved.
*
* This source code belongs to project MojoUnity-Packages, which is hosted on GitHub, and licensed under the MIT License.
*
* License: https://github.com/scottcgi/MojoUnity-Packages/blob/main/LICENSE
* GitHub : https://github.com/scottcgi/MojoUnity-Packages
* Package: https://github.com/scottcgi/MojoUnity-Packages/tree/main/MojoUnity-TextPro
*
* Since : 2021-1-11
* Update : 2021-6-3
* Author : scott.cgi
*/
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
namespace MojoUnity
{
/// <summary>
/// Text can add images or control each chars.
///
/// The material must not be 0 (<quad material!=0/>),
/// so that the <quad/> vertex data will at end of VertexHelper when OnPopulateMesh.
///
/// [UGUI Text Bug]:
///
/// 1. If Text VerticalWrapMode is Truncate and the text is truncated,
/// then the <quad/> will not display right and the toFill vertexes are disordered.
///
/// 2. If <quad/> size is lager than 500,
/// then the <quad/> height and position will calculate error
/// by [cachedTextGeneratorForLayout.GetPreferredHeight].
/// </summary>
public class TextPro : Text
{
public struct SpriteInfo
{
public Sprite sprite;
public Vector2 size;
public bool isCenter;
public Action OnClick;
}
/// <summary>
/// The current click Image.
/// </summary>
public Image CurrentClickImage { get; private set; }
/// Sprites display at tag <quad/> position
private readonly List<SpriteInfo> spriteInfoList = new List<SpriteInfo>();
/// Sprite scale when isCenter is true
private const float SpriteCenterScale = 1.35f;
protected override void Start()
{
base.Start();
if (this.horizontalOverflow != HorizontalWrapMode.Wrap)
{
this.horizontalOverflow = HorizontalWrapMode.Wrap;
Debug.LogWarning("TextPro needs set [horizontalOverflow] to [Wrap].");
}
if (this.verticalOverflow != VerticalWrapMode.Overflow)
{
this.verticalOverflow = VerticalWrapMode.Overflow;
Debug.LogWarning
(
"TextPro needs set [verticalOverflow] to [Overflow]." +
"This can block <quad/> height caculation error in some cases. "
);
}
}
/// <summary>
/// Add Image to <quad/> pos in text.
/// </summary>
public void AddImage(SpriteInfo spriteInfo)
{
this.spriteInfoList.Add(spriteInfo);
var spriteCount = this.spriteInfoList.Count;
Image image;
if (spriteCount <= this.transform.childCount)
{
var go = this.transform.GetChild(spriteCount - 1).gameObject;
go.SetActive(true);
image = go.GetComponent<Image>();
image.sprite = null;
}
else
{
image = this.gameObject.AddChild<Image>();
}
image.name = "Image-" + spriteInfo.sprite.name;
image.sprite = spriteInfo.sprite;
image.rectTransform.sizeDelta = spriteInfo.size;
image.rectTransform.SetScaleXY(1.0f);
if (spriteInfo.OnClick != null)
{
var btn = image.gameObject.GetComponent<Button>();
if (btn == null)
{
btn = image.gameObject.AddComponent<Button>();
btn.onClick.AddListener
(
() =>
{
this.CurrentClickImage = image;
spriteInfo.OnClick();
this.CurrentClickImage = null;
}
);
}
}
else
{
Destroy(image.gameObject.GetComponent<Button>());
}
}
/// <summary>
/// Add Images to each <quad/> pos in text.
/// </summary>
public void AddImages(IEnumerable<SpriteInfo> SpriteInfos)
{
var enumerator = SpriteInfos.GetEnumerator();
while (enumerator.MoveNext())
{
this.AddImage(enumerator.Current);
}
enumerator.Dispose();
}
/// <summary>
/// Clear all Images and SpriteInfos.
/// </summary>
public void ClearImages()
{
this.spriteInfoList.Clear();
var childCount = this.transform.childCount;
for (var i = 0; i < childCount; ++i)
{
this.transform.GetChild(i).gameObject.SetActive(false);
}
}
protected override void OnPopulateMesh(VertexHelper toFill)
{
base.OnPopulateMesh(toFill);
var count = this.spriteInfoList.Count;
// display sprites at <quad/> postion
if (count > 0)
{
var vertexLastIndex = toFill.currentVertCount - 1;
var spriteLastIndex = count - 1;
var leftBottom = new UIVertex();
// if <quad material!=0/> then the <quad/> data at the end of toFill
for (var i = spriteLastIndex; i > -1; --i)
{
// remove <quad/> display from last
var index = vertexLastIndex - (spriteLastIndex - i) * 4;
toFill.PopulateUIVertex(ref leftBottom, index); // LB
toFill.SetUIVertex ( leftBottom, index - 1); // RB
toFill.SetUIVertex ( leftBottom, index - 2); // RT
toFill.SetUIVertex ( leftBottom, index - 3); // LT
// get image from last
var imageRT = this.transform.GetChild(i).GetComponent<Image>().rectTransform;
var pos = (Vector2) leftBottom.position + imageRT.sizeDelta / 2;
if (this.spriteInfoList[i].isCenter)
{
pos.x += (this.rectTransform.rect.width - imageRT.rect.width) / 2;
imageRT.SetScaleXY(SpriteCenterScale);
}
// set sprite pos by <quad/> vertex pos
imageRT.SetLocalPositionXY(pos);
}
}
}
}
}