private void TMProFromText(TextHandler handler) { if (handler == null) { return; } Text text = handler.GetComponent <Text>(); if (text == null) { return; } string t = text.text; Color c = text.color; int i = text.fontSize; bool r = text.raycastTarget; FontStyles sty = getStyle(text.fontStyle); TextAlignmentOptions align = getAnchor(text.alignment); float spacing = text.lineSpacing; GameObject obj = text.gameObject; MonoBehaviour.DestroyImmediate(text); SEP_TextMeshProHolder tmp = obj.AddComponent <SEP_TextMeshProHolder>(); tmp.text = t; tmp.color = c; tmp.fontSize = i; tmp.raycastTarget = r; tmp.alignment = align; tmp.fontStyle = sty; tmp.lineSpacing = spacing; tmp.font = Resources.Load("Fonts/Calibri SDF", typeof(TMP_FontAsset)) as TMP_FontAsset; tmp.fontSharedMaterial = Resources.Load("Fonts/Materials/Calibri Dropshadow", typeof(Material)) as Material; tmp.enableWordWrapping = true; tmp.isOverlay = false; tmp.richText = true; }
/// <summary> /// This method replaces standard Unity Text elements with TextMeshPro elements /// </summary> /// <param name="handler">The text element marker</param> private void TMProFromText(TextHandler handler) { if (handler == null) { return; } //The TextHandler element should be attached only to objects with a Unity Text element //Note that the "[RequireComponent(typeof(Text))]" attribute cannot be attached to TextHandler since Unity will not allow the Text element to be removed Text text = handler.GetComponent <Text>(); if (text == null) { return; } //Cached all of the relevent information from the Text element string t = text.text; Color c = text.color; int i = text.fontSize; bool r = text.raycastTarget; FontStyles sty = TMPProUtil.FontStyle(text.fontStyle); TextAlignmentOptions align = TMPProUtil.TextAlignment(text.alignment); float spacing = text.lineSpacing; GameObject obj = text.gameObject; //The existing Text element must by destroyed since Unity will not allow two UI elements to be placed on the same GameObject MonoBehaviour.DestroyImmediate(text); BasicOrbitTextMeshProHolder tmp = obj.AddComponent <BasicOrbitTextMeshProHolder>(); //Populate the TextMeshPro fields with the cached data from the old Text element tmp.text = t; if (handler.ReadoutField && BasicSettings.Instance != null) { tmp.color = BasicSettings.Instance.readoutColor; } else if (handler.ReadoutLabel && BasicSettings.Instance != null) { tmp.color = BasicSettings.Instance.labelColor; } else { tmp.color = c; } tmp.fontSize = i; tmp.raycastTarget = r; tmp.alignment = align; tmp.fontStyle = sty; tmp.lineSpacing = spacing; //Load the TMP Font from disk tmp.font = UISkinManager.TMPFont; tmp.fontSharedMaterial = Resources.Load("Fonts/Materials/Calibri Dropshadow", typeof(Material)) as Material; tmp.enableWordWrapping = true; tmp.isOverlay = false; tmp.richText = true; }