public TMProData(TextMeshProUGUI text) { this.fontData.font = text.font; this.fontData.material = text.fontSharedMaterial; this.fontStyle = text.fontStyle; this.fontSize = text.fontSize; this.autoSize = text.enableAutoSizing; this.autoSizeOptions.min = text.fontSizeMin; this.autoSizeOptions.max = text.fontSizeMax; this.autoSizeOptions.wd = text.characterWidthAdjustment; this.autoSizeOptions.line = text.lineSpacingAdjustment; this.color = text.color; this.overrideTags = text.overrideColorTags; this.spacings.character = text.characterSpacing; this.spacings.word = text.wordSpacing; this.spacings.line = text.lineSpacing; this.spacings.paragraph = text.paragraphSpacing; this.alignment = text.alignment; this.wrapping = text.enableWordWrapping; this.overflow = text.overflowMode; this.horizontalMapping = text.horizontalMapping; this.verticalMapping = text.verticalMapping; this.margins.marginsV4 = text.margin; this.geometrySorting = text.geometrySortingOrder; this.richText = text.richText; this.raycastTarget = text.raycastTarget; this.parseEscapeCharaters = text.parseCtrlCharacters; this.visibleDescender = text.useMaxVisibleDescender; this.spriteAsset = text.spriteAsset; this.kerning = text.enableKerning; this.extraPadding = text.extraPadding; }
public KsmGuiText( KsmGuiBase parent, string text, string tooltipText = null, TextAlignmentOptions alignement = TextAlignmentOptions.TopLeft, bool wordWrap = true, TextOverflowModes overflowMode = TextOverflowModes.Overflow ) : base(parent) { savedAlignement = alignement; TextComponent = TopObject.AddComponent <TextMeshProUGUI>(); TextComponent.color = KsmGuiStyle.textColor; TextComponent.font = KsmGuiStyle.textFont; TextComponent.fontSize = KsmGuiStyle.textSize; TextComponent.alignment = alignement; TextComponent.enableWordWrapping = wordWrap; TextComponent.overflowMode = overflowMode; TextComponent.text = text; SetLayoutElement(true); //TextComponent.raycastTarget = false; if (tooltipText != null) { SetTooltipText(tooltipText); } }
private TextMeshProUGUI UpgradeText(Text text) { if (!text) { return(null); } GameObject go = text.gameObject; stringBuilder.AppendLine("Upgrading Text: " + GetPathOfObject(go.transform)); // Copy fields Vector2 sizeDelta = text.rectTransform.sizeDelta; TextAlignmentOptions alignment = GetTMPAlignment(text.alignment, text.alignByGeometry); bool bestFit = text.resizeTextForBestFit; int bestFitMaxSize = text.resizeTextMaxSize; int bestFitMinSize = text.resizeTextMinSize; Color color = text.color; bool enabled = text.enabled; Material fontMaterial; TMP_FontAsset font = GetCorrespondingTMPFontAsset(text.font, text, out fontMaterial); int fontSize = text.fontSize; FontStyles fontStyle = GetTMPFontStyle(text.fontStyle); bool horizontalWrapMode = text.horizontalOverflow == HorizontalWrapMode.Wrap; float lineSpacing = (text.lineSpacing - 1) * 100f; bool raycastTarget = text.raycastTarget; bool supportRichText = text.supportRichText; string _text = text.text; TextOverflowModes verticalOverflow = GetTMPVerticalOverflow(text.verticalOverflow, text.horizontalOverflow); // Replace Text with TextMeshProUGUI DestroyImmediate(text, true); TextMeshProUGUI tmp = go.AddComponent <TextMeshProUGUI>(); // Paste fields tmp.alignment = alignment; tmp.enableAutoSizing = bestFit; tmp.fontSizeMax = bestFitMaxSize; tmp.fontSizeMin = bestFitMinSize; tmp.color = color; tmp.enabled = enabled; tmp.font = font; tmp.fontMaterial = fontMaterial; tmp.fontSize = fontSize; tmp.fontStyle = fontStyle; tmp.enableWordWrapping = horizontalWrapMode; tmp.lineSpacing = lineSpacing; tmp.raycastTarget = raycastTarget; tmp.richText = supportRichText; tmp.text = _text; tmp.overflowMode = verticalOverflow; tmp.rectTransform.sizeDelta = sizeDelta; return(tmp); }
private float GetPreferredHeight() { TextOverflowModes currentOverflowMode = m_overflowMode; m_overflowMode = TextOverflowModes.Overflow; m_renderMode = TextRenderFlags.GetPreferredSizes; GenerateTextMesh(); m_renderMode = TextRenderFlags.Render; m_overflowMode = currentOverflowMode; Debug.Log("GetPreferredHeight() Called. Returning height of " + m_preferredHeight); return(m_preferredHeight); }
public static Modifier <DialogGUILabel> TextLabelOptions(Boolean enableWordWrapping = true, Boolean resizeBestFit = false, Int32 resizeMaxFontSize = 0, Int32 resizeMinFontSize = 0, TextOverflowModes overflowMode = default(TextOverflowModes)) { return(label => { label.textLabelOptions = new DialogGUILabel.TextLabelOptions() { enableWordWrapping = enableWordWrapping, resizeBestFit = resizeBestFit, resizeMaxFontSize = resizeMaxFontSize, resizeMinFontSize = resizeMinFontSize, OverflowMode = overflowMode }; return label; }); }
private void ReplaceOldTextWithNew(Text source) { // Grab all the old information first GameObject parentObject = source.gameObject; string text = source.text; FontStyle fontStyle = source.fontStyle; float fontSize = source.fontSize; float lineSpacing = source.lineSpacing; bool richText = source.supportRichText; TextAnchor alignment = source.alignment; HorizontalWrapMode horizontalMode = source.horizontalOverflow; VerticalWrapMode verticalMode = source.verticalOverflow; bool isAutoSizing = source.resizeTextForBestFit; float minFontSize = source.resizeTextMinSize; float maxFontSize = source.resizeTextMaxSize; Color fontColor = source.color; bool rayCastTarget = source.raycastTarget; // Grab transform information RectTransform transform = source.GetComponent <RectTransform>(); Vector3 anchorPosition = transform.anchoredPosition3D; Vector2 anchorMax = transform.anchorMax; Vector2 anchorMin = transform.anchorMin; Vector2 offsetMax = transform.offsetMax; Vector2 offsetMin = transform.offsetMin; // Destroy the old component DestroyImmediate(source); // Add the new text component TextMeshProUGUI newText = parentObject.AddComponent <TextMeshProUGUI>(); // Copy the old text properties into the new text newText.text = text; newText.font = newFont; newText.fontStyle = ConvertFontStyle(fontStyle); newText.fontSize = fontSize; newText.lineSpacing = lineSpacing; newText.richText = richText; newText.alignment = ConvertAlignment(alignment); // Setup word wrapping newText.enableWordWrapping = (horizontalMode == HorizontalWrapMode.Wrap); // Setup overflow TextOverflowModes overflowMode = TextOverflowModes.Overflow; if (verticalMode == VerticalWrapMode.Truncate) { overflowMode = TextOverflowModes.Truncate; } newText.overflowMode = overflowMode; // Setup the rest of the properties newText.enableAutoSizing = isAutoSizing; newText.fontSizeMin = minFontSize; newText.fontSizeMax = maxFontSize; newText.color = fontColor; newText.raycastTarget = rayCastTarget; // Revert transform information transform.anchoredPosition3D = anchorPosition; transform.anchorMax = anchorMax; transform.anchorMin = anchorMin; transform.offsetMax = offsetMax; transform.offsetMin = offsetMin; }
public override void gaxb_final(TB.TBXMLElement element, object _parent, Hashtable args) { base.gaxb_final(element, _parent, args); string attrib; if (font == null) { font = DefaultFont; } if(element != null){ attrib = element.GetAttribute ("font"); if (attrib != null) { font = attrib; } attrib = element.GetAttribute ("onLinkClick"); if (attrib != null) { onLinkClick = PlanetUnityOverride.processString (_parent, attrib); } attrib = element.GetAttribute ("fontSize"); if (attrib != null) { fontSize = (int)(float.Parse (PlanetUnityOverride.processString (_parent, attrib))); } attrib = element.GetAttribute ("fontStyle"); if (attrib != null) { fontStyle = attrib; } attrib = element.GetAttribute ("sizeToFit"); if (attrib != null) { sizeToFit = bool.Parse (PlanetUnityOverride.processString(_parent, attrib)); } attrib = element.GetAttribute ("maxSize"); if (attrib != null) { maxSize = (int)(float.Parse (PlanetUnityOverride.processString(_parent, attrib))); } attrib = element.GetAttribute ("enableWordWrapping"); if (attrib != null) { enableWordWrapping = bool.Parse (attrib); } attrib = element.GetAttribute ("maxVisibleLines"); if (attrib != null) { maxVisibleLines = int.Parse (attrib); } attrib = element.GetAttribute ("minSize"); if (attrib != null) { minSize = (int)(float.Parse (PlanetUnityOverride.processString(_parent, attrib))); } attrib = element.GetAttribute ("alignment"); if (attrib != null) { alignment = (TextAlignmentOptions)Enum.Parse (typeof(TextAlignmentOptions), attrib); } attrib = element.GetAttribute ("fontColor"); if (attrib != null) { fontColor = fontColor.PUParse (attrib); } attrib = element.GetAttribute ("overflowMode"); if (attrib != null) { overflowMode = (TextOverflowModes)Enum.Parse(typeof(TextOverflowModes), attrib); } attrib = element.GetAttribute ("lineSpacing"); if (attrib != null) { lineSpacing = (int)(float.Parse (PlanetUnityOverride.processString(_parent, attrib))); } value = element.GetAttribute ("value"); value = PlanetUnityOverride.processString (_parent, value); } }
void AssignTextStyle(int fontSize, FontStyles fontStyles, TextAlignmentOptions alignment, TextOverflowModes overflow) { text.fontSize = fontSize; text.fontStyle = fontStyles; text.alignment = alignment; text.overflowMode = overflow; }
/// <summary> /// This assumes a monospaced font, and will tell you how much will fit in /// each row and column for the TMPUGUI. /// /// The TextMesh Pro settings should be as follows: /// Wrapping & overflow: (doesn't really matter?) Disabled & truncate /// </summary> /// <returns>(x, y) as (cols, rows) or (width, height)</returns> public Vector2Int GetDialogTextSize() { // dialogText.firstOverflowCharacterIndex; string originalText = dialogText.text; int lineHeight; bool oldDBOpen = dialogBox.active; dialogBox.SetActive(true); dialogText.text = HEIGHT_TEST_STRING; // Reformat the display - see https://forum.unity.com/threads/linked-text-in-ugui-layout-groups.471477/ // Both these calls seem to work - which is more efficient? // dialogText.Rebuild(CanvasUpdate.PreRender); Canvas.ForceUpdateCanvases(); dialogText.ForceMeshUpdate(); /* * Debug.Log("Overflowing? " + dialogText.isTextOverflowing + * ", at: " + dialogText.firstOverflowCharacterIndex); * Debug.Log("Truncated? " + dialogText.isTextTruncated); * // characterCount seems to count characters even if they have been * // truncated from a long line. Disappointing. * Debug.Log("Char count: " + dialogText.textInfo.characterCount + * ", line count: " + dialogText.textInfo.lineCount + * ", page count: " + dialogText.textInfo.pageCount); */ /* * Debug.Log("Max vis chars: " + dialogText.maxVisibleCharacters + * ", max vis lines: " + dialogText.maxVisibleLines + * ", max vis words: " + dialogText.maxVisibleWords); */ // This works to get the height, but not the width. lineHeight = dialogText.textInfo.lineCount; // TRY to find the line length bool oldWW = dialogText.enableWordWrapping; TextOverflowModes oldTOM = dialogText.overflowMode; dialogText.text = "X"; dialogText.enableWordWrapping = true; dialogText.overflowMode = TextOverflowModes.Overflow; int lineWidth = 300; for (int l = 1; l < lineWidth; l++) { dialogText.text = string.Concat(Enumerable.Repeat("X", l)); // Mesh update is necessary dialogText.ForceMeshUpdate(); /* * Debug.Log("Length: " + l + * ", char count: " + dialogText.textInfo.characterCount + * ", line count: " + dialogText.textInfo.lineCount); */ if (dialogText.textInfo.lineCount > 1) { lineWidth = l - 1; break; } } if (!oldDBOpen) { dialogBox.SetActive(false); } // Restore everything to the original settings dialogText.text = originalText; dialogText.enableWordWrapping = oldWW; dialogText.overflowMode = oldTOM; // dialogText.Rebuild(CanvasUpdate.PreRender); dialogText.ForceMeshUpdate(); Debug.Log("Final text size: width: " + lineWidth + ", height: " + lineHeight); return(new Vector2Int(lineWidth, lineHeight)); } // GetDialogTextRows()
public override void gaxb_final(TB.TBXMLElement element, object _parent, Hashtable args) { string attrib; if (font == null) { font = DefaultFont; } if (element != null) { attrib = element.GetAttribute("font"); if (attrib != null) { font = attrib; } attrib = element.GetAttribute("onLinkClick"); if (attrib != null) { onLinkClick = PlanetUnityOverride.processString(_parent, attrib); } attrib = element.GetAttribute("fontSize"); if (attrib != null) { fontSize = (int)(float.Parse(PlanetUnityOverride.processString(_parent, attrib))); } attrib = element.GetAttribute("fontStyle"); if (attrib != null) { fontStyle = attrib; } attrib = element.GetAttribute("sizeToFit"); if (attrib != null) { sizeToFit = bool.Parse(PlanetUnityOverride.processString(_parent, attrib)); } attrib = element.GetAttribute("maxSize"); if (attrib != null) { maxSize = (int)(float.Parse(PlanetUnityOverride.processString(_parent, attrib))); } attrib = element.GetAttribute("enableWordWrapping"); if (attrib != null) { enableWordWrapping = bool.Parse(attrib); } attrib = element.GetAttribute("maxVisibleLines"); if (attrib != null) { maxVisibleLines = int.Parse(attrib); } attrib = element.GetAttribute("minSize"); if (attrib != null) { minSize = (int)(float.Parse(PlanetUnityOverride.processString(_parent, attrib))); } attrib = element.GetAttribute("alignment"); if (attrib != null) { alignment = (TextAlignmentOptions)Enum.Parse(typeof(TextAlignmentOptions), attrib); } attrib = element.GetAttribute("fontColor"); if (attrib != null) { fontColor = fontColor.PUParse(attrib); } attrib = element.GetAttribute("overflowMode"); if (attrib != null) { overflowMode = (TextOverflowModes)Enum.Parse(typeof(TextOverflowModes), attrib); } attrib = element.GetAttribute("lineSpacing"); if (attrib != null) { lineSpacing = (int)(float.Parse(PlanetUnityOverride.processString(_parent, attrib))); } value = element.GetAttribute("value"); value = PlanetUnityOverride.processString(_parent, value); } base.gaxb_final(element, _parent, args); }
private void UpdateToTextMeshPro(Text component) { bool enabled = component.enabled; string text = component.text; TMP_FontAsset font = this.m_FontMap[component.font]; TMPro.FontStyles fontStyles = FontStyles.Normal; switch (component.fontStyle) { case FontStyle.Bold: fontStyles = FontStyles.Bold; break; case FontStyle.BoldAndItalic: fontStyles = FontStyles.Bold | FontStyles.Italic; break; case FontStyle.Italic: fontStyles = FontStyles.Italic; break; } int fontSize = component.fontSize; Color color = component.color; bool richText = component.supportRichText; TextAlignmentOptions alignment = TextAlignmentOptions.TopLeft; switch (component.alignment) { case TextAnchor.LowerCenter: alignment = TextAlignmentOptions.Bottom; break; case TextAnchor.LowerLeft: alignment = TextAlignmentOptions.BottomLeft; break; case TextAnchor.LowerRight: alignment = TextAlignmentOptions.BottomRight; break; case TextAnchor.MiddleCenter: alignment = TextAlignmentOptions.Center; break; case TextAnchor.MiddleLeft: alignment = TextAlignmentOptions.Left; break; case TextAnchor.MiddleRight: alignment = TextAlignmentOptions.Right; break; case TextAnchor.UpperCenter: alignment = TextAlignmentOptions.Top; break; case TextAnchor.UpperLeft: alignment = TextAlignmentOptions.TopLeft; break; case TextAnchor.UpperRight: alignment = TextAlignmentOptions.TopRight; break; } bool wrap = component.horizontalOverflow == HorizontalWrapMode.Wrap ? true : false; TextOverflowModes overflowModes = component.verticalOverflow == VerticalWrapMode.Overflow? TextOverflowModes.Overflow:TextOverflowModes.Truncate; bool autoSize = component.resizeTextForBestFit; float minFontSize = component.resizeTextMinSize; float maxFontSize = component.resizeTextMaxSize; bool raycastTarget = component.raycastTarget; bool maskable = component.maskable; Outline outline = component.GetComponent <Outline>(); bool hasOutline = outline != null; float outlineThickness = 0f; Color outlineColor = Color.black; if (hasOutline) { outlineThickness = ((Mathf.Abs(outline.effectDistance.x) + Mathf.Abs(outline.effectDistance.y)) * 0.5f) * outlineRatio; outlineColor = outline.effectColor; GameObject.DestroyImmediate(outline); } Shadow shadow = component.GetComponent <Shadow>(); bool hasShadow = shadow != null; Color shadowColor = Color.black; float offsetX = 0f; float offsetY = 0f; if (hasShadow) { offsetX = shadow.effectDistance.x * shadowRatio; offsetY = shadow.effectDistance.y * shadowRatio; GameObject.DestroyImmediate(shadow); } GameObject go = component.gameObject; DestroyImmediate(component); TextMeshProUGUI textMeshPro = go.AddComponent <TextMeshProUGUI>(); textMeshPro.SetText(text); textMeshPro.font = font; textMeshPro.fontStyle = fontStyles; textMeshPro.fontSize = fontSize; textMeshPro.color = color; textMeshPro.richText = richText; textMeshPro.alignment = alignment; textMeshPro.enableWordWrapping = wrap; textMeshPro.overflowMode = overflowModes; textMeshPro.enableAutoSizing = autoSize; textMeshPro.fontSizeMin = minFontSize; textMeshPro.fontSizeMax = maxFontSize; textMeshPro.raycastTarget = raycastTarget; textMeshPro.maskable = maskable; Material material = textMeshPro.fontMaterial; if (hasOutline) { material.EnableKeyword(ShaderUtilities.Keyword_Outline); material.SetColor("_OutlineColor", outlineColor); material.SetFloat("_OutlineWidth", outlineThickness); } else { material.DisableKeyword(ShaderUtilities.Keyword_Outline); } if (hasShadow) { material.EnableKeyword(ShaderUtilities.Keyword_Underlay); material.SetColor("_UnderlayColor", shadowColor); material.SetFloat("_UnderlayOffsetX", offsetX); material.SetFloat("_UnderlayOffsetY", offsetY); material.SetFloat("_UnderlayDilate", 0f); material.SetFloat("_UnderlaySoftness", 0.2f); } else { material.DisableKeyword(ShaderUtilities.Keyword_Underlay); } textMeshPro.enabled = enabled; }
public UIText Overflow(TextOverflowModes mode) { tmpText.overflowMode = mode; return(this); }