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
|
/*
------------------- Code Monkey -------------------
Thank you for downloading the Code Monkey Utilities
I hope you find them useful in your projects
If you have any questions use the contact form
Cheers!
unitycodemonkey.com
--------------------------------------------------
*/
using UnityEngine;
using UnityEngine.UI;
namespace CodeMonkey.Utils {
/*
* Displays text with icons in between the text
* */
public class UI_TextComplex {
private static Transform GetCanvasTransform() {
return UtilsClass.GetCanvasTransform();
}
public struct Icon {
public Sprite sprite;
public Vector2 size;
public Color color;
public Icon(Sprite sprite, Vector2 size, Color? color = null) {
this.sprite = sprite;
this.size = size;
if (color == null) {
this.color = Color.white;
} else {
this.color = (Color) color;
}
}
}
public GameObject gameObject;
private Transform transform;
private RectTransform rectTransform;
// iconChar prepends the iconArr index;
// Example using iconChar '#':
// test #0 asdf
// Displays "test [iconArr[0]] asdf"
public UI_TextComplex(Transform parent, Vector2 anchoredPosition, int fontSize, char iconChar, string text, Icon[] iconArr, Font font) {
SetupParent(parent, anchoredPosition);
string tmp = text;
float textPosition = 0f;
while (tmp.IndexOf(iconChar) != -1) {
string untilTmp = tmp.Substring(0, tmp.IndexOf(iconChar));
string iconNumber = tmp.Substring(tmp.IndexOf(iconChar)+1);
int indexOfSpaceAfterIconNumber = iconNumber.IndexOf(" ");
if (indexOfSpaceAfterIconNumber != -1) {
// Still has more space after iconNumber
iconNumber = iconNumber.Substring(0, indexOfSpaceAfterIconNumber);
} else {
// No more space after iconNumber
}
tmp = tmp.Substring(tmp.IndexOf(iconChar+iconNumber) + (iconChar+iconNumber).Length);
if (untilTmp.Trim() != "") {
Text uiText = UtilsClass.DrawTextUI(untilTmp, transform, new Vector2(textPosition,0), fontSize, font);
textPosition += uiText.preferredWidth;
}
// Draw Icon
int iconIndex = UtilsClass.Parse_Int(iconNumber, 0);
Icon icon = iconArr[iconIndex];
UtilsClass.DrawSprite(icon.sprite, transform, new Vector2(textPosition + icon.size.x / 2f, 0), icon.size);
textPosition += icon.size.x;
}
if (tmp.Trim() != "") {
UtilsClass.DrawTextUI(tmp, transform, new Vector2(textPosition,0), fontSize, font);
}
}
private void SetupParent(Transform parent, Vector2 anchoredPosition) {
gameObject = new GameObject("UI_TextComplex", typeof(RectTransform));
transform = gameObject.transform;
rectTransform = gameObject.GetComponent<RectTransform>();
rectTransform.SetParent(parent, false);
rectTransform.sizeDelta = new Vector2(0, 0);
rectTransform.anchorMin = new Vector2(0, .5f);
rectTransform.anchorMax = new Vector2(0, .5f);
rectTransform.pivot = new Vector2(0, .5f);
rectTransform.anchoredPosition = anchoredPosition;
}
public void SetTextColor(Color color) {
foreach (Transform trans in transform) {
Text text = trans.GetComponent<Text>();
if (text != null) {
text.color = color;
}
}
}
public float GetTotalWidth() {
float textPosition = 0f;
foreach (Transform trans in transform) {
Text text = trans.GetComponent<Text>();
if (text != null) {
textPosition += text.preferredWidth;
}
Image image = trans.GetComponent<Image>();
if (image != null) {
textPosition += image.GetComponent<RectTransform>().sizeDelta.x;
}
}
return textPosition;
}
public float GetTotalHeight() {
foreach (Transform trans in transform) {
Text text = trans.GetComponent<Text>();
if (text != null) {
return text.preferredHeight;
}
}
return 0f;
}
public void AddTextOutline(Color color, float size) {
foreach (Transform textComplexTrans in transform) {
if (textComplexTrans.GetComponent<Text>() != null) {
Outline outline = textComplexTrans.gameObject.AddComponent<Outline>();
outline.effectColor = color;
outline.effectDistance = new Vector2(size, size);
}
}
}
public void SetAnchorMiddle() {
rectTransform.anchorMin = new Vector2(.5f, .5f);
rectTransform.anchorMax = new Vector2(.5f, .5f);
}
public void CenterOnPosition(Vector2 position) {
rectTransform.anchoredPosition = position + new Vector2(-GetTotalWidth() / 2f, 0);
}
public void DestroySelf() {
Object.Destroy(gameObject);
}
}
}
|