19 private TextGenerationSettings m_RelatedTextSettings;
22 get {
return m_Text; }
28 m_RelatedTextSettings = m_Text.GetGenerationSettings(Vector2.zero);
33 private TextMeshProUGUI m_TMPText;
34 public TextMeshProUGUI tmpText {
get {
return m_TMPText; }
set { m_TMPText = value; } }
36 public GameObject gameObject
41 if (m_TMPText !=
null)
return m_TMPText.gameObject;
43 if (m_Text !=
null)
return m_Text.gameObject;
49 public TextAnchor alignment
54 if (m_TMPText ==
null)
return TextAnchor.MiddleCenter;
55 switch (m_TMPText.alignment)
57 case TextAlignmentOptions.Bottom:
return TextAnchor.LowerCenter;
58 case TextAlignmentOptions.BottomLeft:
return TextAnchor.LowerLeft;
59 case TextAlignmentOptions.BottomRight:
return TextAnchor.LowerRight;
60 case TextAlignmentOptions.Center:
return TextAnchor.MiddleCenter;
61 case TextAlignmentOptions.Left:
return TextAnchor.MiddleLeft;
62 case TextAlignmentOptions.Right:
return TextAnchor.MiddleRight;
63 case TextAlignmentOptions.Top:
return TextAnchor.UpperCenter;
64 case TextAlignmentOptions.TopLeft:
return TextAnchor.UpperLeft;
65 case TextAlignmentOptions.TopRight:
return TextAnchor.UpperRight;
66 default:
return TextAnchor.MiddleCenter;
69 if (m_Text !=
null)
return m_Text.alignment;
70 else return TextAnchor.MiddleCenter;
75 SetAlignment(alignment);
86 m_TMPText = textParent.GetComponentInChildren<TextMeshProUGUI>();
87 if (m_TMPText ==
null)
89 Debug.LogError(
"can't find TextMeshProUGUI component:" + textParent);
92 m_Text = textParent.GetComponentInChildren<Text>();
95 Debug.LogError(
"can't find Text component:" + textParent);
100 public void SetFontSize(
float fontSize)
103 if (m_TMPText !=
null) m_TMPText.fontSize = fontSize;
105 if (m_Text !=
null) m_Text.fontSize = (int)fontSize;
109 public void SetText(
string text)
111 if (text ==
null) text =
string.Empty;
112 else text = text.Replace(
"\\n",
"\n");
114 if(m_TMPText !=
null) m_TMPText.text = text;
116 if (m_Text !=
null) m_Text.text = text;
120 public string GetText()
123 if (m_TMPText !=
null)
return m_TMPText.text;
125 if (m_Text !=
null)
return m_Text.text;
130 public void SetColor(Color color)
133 if (m_TMPText !=
null) m_TMPText.color = color;
135 if (m_Text !=
null) m_Text.color = color;
139 public void SetLineSpacing(
float lineSpacing)
142 if (m_TMPText !=
null) m_TMPText.lineSpacing = lineSpacing;
144 if (m_Text !=
null) m_Text.lineSpacing = lineSpacing;
148 public void SetActive(
bool flag)
152 if (m_TMPText !=
null) ChartHelper.SetActive(m_TMPText.gameObject, flag);
155 if (m_Text !=
null) ChartHelper.SetActive(m_Text.gameObject, flag);
159 public void SetLocalPosition(Vector3 position)
162 if (m_TMPText !=
null) m_TMPText.transform.localPosition = position;
164 if (m_Text !=
null) m_Text.transform.localPosition = position;
167 public void SetSizeDelta(Vector2 sizeDelta)
170 if (m_TMPText !=
null) m_TMPText.GetComponent<RectTransform>().sizeDelta = sizeDelta;
172 if (m_Text !=
null) m_Text.GetComponent<RectTransform>().sizeDelta = sizeDelta;
176 public void SetLocalEulerAngles(Vector3 position)
179 if (m_TMPText !=
null) m_TMPText.transform.localEulerAngles = position;
181 if (m_Text !=
null) m_Text.transform.localEulerAngles = position;
185 public void SetAlignment(TextAnchor alignment)
188 if (m_TMPText ==
null)
return;
191 case TextAnchor.LowerCenter: m_TMPText.alignment = TextAlignmentOptions.Bottom;
break;
192 case TextAnchor.LowerLeft: m_TMPText.alignment = TextAlignmentOptions.BottomLeft;
break;
193 case TextAnchor.LowerRight: m_TMPText.alignment = TextAlignmentOptions.BottomRight;
break;
194 case TextAnchor.MiddleCenter: m_TMPText.alignment = TextAlignmentOptions.Center;
break;
195 case TextAnchor.MiddleLeft: m_TMPText.alignment = TextAlignmentOptions.Left;
break;
196 case TextAnchor.MiddleRight: m_TMPText.alignment = TextAlignmentOptions.Right;
break;
197 case TextAnchor.UpperCenter: m_TMPText.alignment = TextAlignmentOptions.Top;
break;
198 case TextAnchor.UpperLeft: m_TMPText.alignment = TextAlignmentOptions.TopLeft;
break;
199 case TextAnchor.UpperRight: m_TMPText.alignment = TextAlignmentOptions.TopRight;
break;
202 if (m_Text !=
null) m_Text.alignment = alignment;
206 public void SetFont(Font font)
208 if (m_Text) m_Text.font = font;
211 public void SetFontStyle(FontStyle fontStyle)
214 if (m_TMPText ==
null)
return;
217 case FontStyle.Normal: m_TMPText.fontStyle = FontStyles.Normal;
break;
218 case FontStyle.Bold: m_TMPText.fontStyle = FontStyles.Bold;
break;
219 case FontStyle.BoldAndItalic: m_TMPText.fontStyle = FontStyles.Bold | FontStyles.Italic;
break;
220 case FontStyle.Italic: m_TMPText.fontStyle = FontStyles.Italic;
break;
223 if (m_Text !=
null) m_Text.fontStyle = fontStyle;
230 if (m_TMPText ==
null)
return;
231 m_TMPText.font = textStyle.tmpFont ==
null ? theme.tmpFont : textStyle.tmpFont;
233 m_TMPText.fontStyle = textStyle.tmpFontStyle;
244 public float GetPreferredWidth(
string content)
247 if (m_TMPText !=
null)
return 0;
251 var tg = m_Text.cachedTextGeneratorForLayout;
252 var setting = m_Text.GetGenerationSettings(Vector2.zero);
253 return tg.GetPreferredWidth(content, setting) / m_Text.pixelsPerUnit;
259 public float GetPreferredWidth()
262 if (m_TMPText !=
null)
return m_TMPText.preferredWidth;
264 if (m_Text !=
null)
return m_Text.preferredWidth;
268 public float GetPreferredHeight()
271 if (m_TMPText !=
null)
return m_TMPText.preferredHeight;
273 if (m_Text !=
null)
return m_Text.preferredHeight;
278 public string GetPreferredText(
string content,
string suffix,
float maxWidth)
281 if (m_TMPText !=
null)
return content;
285 var sourWid = GetPreferredWidth(content);
286 if (sourWid < maxWidth)
return content;
287 var suffixWid = GetPreferredWidth(suffix);
288 var textWid = maxWidth - 1.3f * suffixWid;
289 for (
int i = content.Length; i > 0; i--)
291 var temp = content.Substring(0, i);
292 if (GetPreferredWidth(temp) < textWid)
294 return temp + suffix;
304 public void SetFont(TMP_FontAsset font)
306 if (m_TMPText !=
null) m_TMPText.font = font;