-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSpriteText.cs
231 lines (207 loc) · 5.95 KB
/
SpriteText.cs
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using UnityEngine;
using UnityEngine.U2D;
using UnityEngine.UI;
#if UNITY_EDITOR
using UnityEditor;
using UnityEditor.U2D;
#endif
[AddComponentMenu("UI/SpriteText")]
public class SpriteText : Text
{
[SerializeField]
SpriteAtlas spriteAtlas;
static readonly Regex spriteRegex = new Regex(@"(?i)<(?:sprite=)(.*?)(?: x=(.*?))?(?: y=(.*?))?(?: size=(.*?))?\/>", RegexOptions.Multiline);
string OutputText { get; set; }
Action DelayAction { get; set; }
public override string text
{
get => base.text;
set
{
base.text = value;
Changed();
DelayAction?.Invoke();
}
}
protected override void OnPopulateMesh(VertexHelper toFill)
{
if (spriteAtlas == null)
{
base.OnPopulateMesh(toFill);
return;
}
string orignText = m_Text;
m_Text = OutputText;
base.OnPopulateMesh(toFill);
m_Text = orignText;
}
private void Update()
{
DelayAction?.Invoke();
}
public override void SetVerticesDirty()
{
base.SetVerticesDirty();
if (spriteAtlas == null)
return;
Changed();
}
public void Changed()
{
if (gameObject.activeInHierarchy == false)
return;
OutputText = m_Text;
MatchCollection matchCollection = spriteRegex.Matches(m_Text);
Sprite[] sprites = new Sprite[matchCollection.Count];
int[] matchIndexs = new int[matchCollection.Count];
int matchOffset = 0;
for (int i = 0; i < matchCollection.Count; i++)
{
Match match = matchCollection[i];
Sprite sprite = GetSprite(match.Groups[1].Value, spriteAtlas);
if (sprite == null)
continue;
OutputText = OutputText.Remove(match.Index - matchOffset, match.Length);
matchIndexs[i] = match.Index - matchOffset;
sprites[i] = sprite;
matchOffset += match.Length;
}
DelayAction = delegate
{
List<Image> imageList = new List<Image>();
GetComponentsInChildren(imageList);
for (int i = 0; i < matchCollection.Count; i++)
{
if (i < imageList.Count)
continue;
GameObject spriteObject = new GameObject("RichSprite");
spriteObject.transform.SetParent(rectTransform);
spriteObject.transform.localScale = Vector3.one;
Image image = spriteObject.AddComponent<Image>();
image.raycastTarget = false;
imageList.Add(image);
}
TextGenerationSettings settings = GetGenerationSettings(rectTransform.rect.size);
cachedTextGenerator.Populate(OutputText, settings);
int removeLength = 0;
for (int i = 0; i < matchCollection.Count; i++)
{
Sprite sprite = sprites[i];
Image image = imageList[i];
if (sprite == null)
{
image.enabled = false;
continue;
}
Match match = matchCollection[i];
int matchIndex = matchIndexs[i];
if (cachedTextGenerator.characters.Count <= matchIndex)
{
image.enabled = false;
continue;
}
UICharInfo uiCharInfo = cachedTextGenerator.characters[matchIndex];
UILineInfo uiLineInfo = cachedTextGenerator.lines[0];
Vector2 position = uiCharInfo.cursorPos;
float.TryParse(match.Groups[2].Value, out float x);
float.TryParse(match.Groups[3].Value, out float y);
float size = 1;
if (match.Groups[4].Success)
float.TryParse(match.Groups[4].Value, out size);
position.x += x;
position.y += y;
position.y -= uiLineInfo.height * 0.5f;
position *= (1 / pixelsPerUnit);
removeLength += match.Length;
//sprite
image.enabled = true;
image.sprite = sprite;
image.SetNativeSize();
image.rectTransform.localScale = Vector3.one * size;
image.rectTransform.localPosition = position;
}
for (int i = transform.childCount - 1; i >= matchCollection.Count; i--)
DestroyImmediate(transform.GetChild(i).gameObject);
DelayAction = null;
};
}
#if UNITY_EDITOR
[MenuItem("GameObject/UI/SpriteText")]
static void CreateSpriteText()
{
GameObject go = new GameObject("SpriteText");
go.AddComponent<SpriteText>();
if (Selection.activeGameObject == null)
{
Canvas canvas = FindObjectOfType<Canvas>();
if (canvas == null)
{
GameObject canvasGo = new GameObject("Canvas");
canvas = canvasGo.AddComponent<Canvas>();
}
canvas.renderMode = RenderMode.ScreenSpaceOverlay;
go.transform.SetParent(canvas.transform);
}
else
go.transform.SetParent(Selection.activeGameObject.transform);
go.transform.localPosition = Vector3.zero;
Undo.RegisterCreatedObjectUndo(go, "CreateSpriteText");
Selection.activeGameObject = go;
}
protected override void OnEnable()
{
if (spriteAtlas == null)
{
Debug.LogError("SpriteAtlas is null", gameObject);
return;
}
base.OnEnable();
}
public Sprite GetSprite(string name, SpriteAtlas spriteAtlas)
{
foreach (var packable in spriteAtlas.GetPackables())
{
var dirPath = AssetDatabase.GetAssetPath(packable);
var spriteGUIDs = AssetDatabase.FindAssets($"{name} t:sprite", new string[] { dirPath });
if (spriteGUIDs.Length == 0)
continue;
for (int i = 0; i < spriteGUIDs.Length; i++)
{
var spritePath = AssetDatabase.GUIDToAssetPath(spriteGUIDs[i]);
var sprite = AssetDatabase.LoadAssetAtPath<Sprite>(spritePath);
if (sprite.name == name)
return sprite;
}
}
return null;
}
#else
public Sprite GetSprite(string name, SpriteAtlas spriteAtlas)
{
return spriteAtlas.GetSprite(name);
}
#endif
}
#if UNITY_EDITOR
[CanEditMultipleObjects]
[CustomEditor(typeof(SpriteText))]
public class SpriteTextEditor : UnityEditor.UI.TextEditor
{
SerializedProperty spriteAtlas;
protected override void OnEnable()
{
base.OnEnable();
spriteAtlas = serializedObject.FindProperty("spriteAtlas");
}
public override void OnInspectorGUI()
{
serializedObject.Update();
base.OnInspectorGUI();
EditorGUILayout.PropertyField(spriteAtlas);
serializedObject.ApplyModifiedProperties();
}
}
#endif