public TextGenerationSettings GetGenerationSettings(Vector2 extents) { var settings = new TextGenerationSettings(); // Settings affected by pixels density var pixelsPerUnitCached = pixelsPerUnit; settings.generationExtents = extents * pixelsPerUnitCached + Vector2.one * kEpsilon; if (font != null && font.dynamic) { settings.fontSize = Mathf.Min(Mathf.FloorToInt(m_FontData.fontSize * pixelsPerUnitCached), 1000); settings.resizeTextMinSize = Mathf.Min(Mathf.FloorToInt(m_FontData.minSize * pixelsPerUnitCached), 1000); settings.resizeTextMaxSize = Mathf.Min(Mathf.FloorToInt(m_FontData.maxSize * pixelsPerUnitCached), 1000); } // Other settings settings.textAnchor = m_FontData.alignment; settings.color = color; settings.font = font; settings.pivot = rectTransform.pivot; settings.richText = m_FontData.richText; settings.lineSpacing = m_FontData.lineSpacing; settings.fontStyle = m_FontData.fontStyle; settings.resizeTextForBestFit = m_FontData.bestFit; settings.updateBounds = false; settings.horizontalOverflow = m_FontData.horizontalOverflow; settings.verticalOverflow = m_FontData.verticalOverflow; return(settings); }
public void Init(Text TextTemplate) { mTextGenerator = TextTemplate.cachedTextGeneratorForLayout; mTgSettings = TextTemplate.GetGenerationSettings(Vector2.zero); mPixelsPerUnit = TextTemplate.pixelsPerUnit; HrefPrefab.GetComponent <Text>().fontSize = TextTemplate.fontSize; LineHeight = mTextGenerator.GetPreferredHeight("好", mTgSettings) / mPixelsPerUnit; SpaceWidth = GetStrWidth(" "); EmojiWidthSpaceNum = (EmojiSize.x / SpaceWidth) % 1 < 0.05f ? (int)(EmojiSize.x / SpaceWidth) : (int)(EmojiSize.x / SpaceWidth) + 1; EmojiHeightSpaceNum = (EmojiSize.y / LineHeight) % 1 < 0.05f ? (int)(EmojiSize.y / LineHeight) - 1 : (int)(EmojiSize.y / LineHeight); EmojiWidthStr = ""; EmojiHeightStr = ""; for (int i = 0; i < EmojiWidthSpaceNum; i++) { EmojiWidthStr += " "; } for (int i = 0; i < EmojiHeightSpaceNum; i++) { EmojiHeightStr += '\n'; } EmojiSpWidth = GetStrWidth(EmojiWidthStr); }
public int getOverflowPoint( string message, Text textBox = null, int lines = -1) { if (textBox == null) { textBox = dialogBoxText; } if (lines <= 0) { lines = defaultDialogLines; } TextGenerator textGen = new TextGenerator(); TextGenerationSettings generationSettings = textBox.GetGenerationSettings(textBox.rectTransform.rect.size); for (int i = 0; i < message.Length; i++) // Got overflow point { float height = textGen.GetPreferredHeight(message.Substring(0, i), generationSettings); if (height > generationSettings.fontSize * (lines + 2)) { for (int i2 = i; i2 > 0; i2--) { if (message[i2] == ' ' || message[i2] == '\n') // Stop counter at first space or newline { return(i2); } } return(i); } } return(-1); }
public TextGenerationSettings GetGenerationSettings(Vector2 extents) { TextGenerationSettings settings = new TextGenerationSettings { generationExtents = extents }; if ((this.font != null) && this.font.dynamic) { settings.fontSize = this.m_FontData.fontSize; settings.resizeTextMinSize = this.m_FontData.minSize; settings.resizeTextMaxSize = this.m_FontData.maxSize; } settings.textAnchor = this.m_FontData.alignment; settings.scaleFactor = this.pixelsPerUnit; settings.color = base.color; settings.font = this.font; settings.pivot = base.rectTransform.pivot; settings.richText = this.m_FontData.richText; settings.lineSpacing = this.m_FontData.lineSpacing; settings.fontStyle = this.m_FontData.fontStyle; settings.resizeTextForBestFit = this.m_FontData.bestFit; settings.updateBounds = false; settings.horizontalOverflow = this.m_FontData.horizontalOverflow; settings.verticalOverflow = this.m_FontData.verticalOverflow; return(settings); }
private void ResizeImageHolder() { Vector2 rectSize = (txtChild.transform as RectTransform).rect.size; TextGenerationSettings tempSettings = txtChild.GetGenerationSettings(rectSize); TextGenerator txtGenerator = txtChild.cachedTextGenerator; RectTransform txtRectT = txtChild.rectTransform; RectTransform imgRectT = imgTextArea.rectTransform; //float width = txtGenerator.GetPreferredWidth(txtChild.text, tempSettings) + (txtRectT.offsetMin.x - txtRectT.offsetMax.x); //float height = txtGenerator.GetPreferredHeight(txtChild.text, tempSettings) + (txtRectT.offsetMin.y - txtRectT.offsetMax.y); float width = txtChild.preferredWidth + (txtRectT.offsetMin.x - txtRectT.offsetMax.x); float height = txtChild.preferredHeight + (txtRectT.offsetMin.y - txtRectT.offsetMax.y); switch (expand) { case Expand.Horizontal: if (height > imgTextArea.rectTransform.sizeDelta.y) { Vector2 rectNew = new Vector2(width, (txtChild.transform as RectTransform).rect.y); TextGenerationSettings newSettings = txtChild.GetGenerationSettings(rectNew); height = txtGenerator.GetPreferredHeight(txtChild.text, newSettings) + (txtRectT.offsetMin.y - txtRectT.offsetMax.y); } imgRectT.sizeDelta = new Vector2(width, height); break; case Expand.Vertical: imgRectT.sizeDelta = new Vector2(imgRectT.rect.width, height); break; default: break; } }
private void _UseFitSettings() { TextGenerationSettings settings = GetGenerationSettings(rectTransform.rect.size); settings.resizeTextForBestFit = false; if (!resizeTextForBestFit) { cachedTextGenerator.Populate(text, settings); return; } int minSize = resizeTextMinSize; int txtLen = text.Length; for (int i = resizeTextMaxSize; i >= minSize; --i) { settings.fontSize = i; cachedTextGenerator.Populate(text, settings); if (cachedTextGenerator.characterCountVisible == txtLen) { break; } } }
public void Populate(string text, GameObject context) { TextGenerationSettings settings = _textComponent.GetGenerationSettings(_textComponent.rectTransform.rect.size); settings.generateOutOfBounds = true; RawTextGenerator.PopulateWithErrors(text, settings, context); }
private void FillMessages(RectTransform parent, DebugMessage[] msg) { GameObject prefab = (GameObject)Resources.Load("Prefabs/Message"); Text[] texts = parent.GetComponentsInChildren <Text>(); for (int i = 0; i < texts.Length; i++) { Destroy(texts[i].gameObject); } float endSize = 0; for (int i = msg.Length - 1; i > -1; i--) { RectTransform trans = ((GameObject)Instantiate(prefab)).GetComponent <RectTransform> (); trans.SetParent(parent); trans.anchoredPosition = -Vector2.up * (endSize + 15); trans.GetComponentInChildren <Text> ().text = msg [i].message; trans.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, parent.rect.width); Text t = trans.GetComponent <Text> (); TextGenerationSettings s = t.GetGenerationSettings(trans.rect.size); float slotSize = t.cachedTextGenerator.GetPreferredHeight(msg[i].message, s); trans.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, slotSize); t.text = msg [i].message; t.color = msg [i].color; endSize += slotSize; } parent.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, endSize + 15); }
public override void DisplayResponse(Response response, int ID, bool possible) { if (possible) { // Disable Next button NextButton.gameObject.SetActive(false); NextButton.interactable = false; GameObject buttonClone = GameObject.Instantiate(ButtonPrototype, responsePanel); RectTransform buttonTransform = buttonClone.GetComponent <RectTransform>(); Button buttonScript = buttonClone.GetComponent <Button>(); Text buttonText = buttonClone.GetComponentInChildren <Text>(); Vector2 buttonPos = buttonTransform.anchoredPosition; // Place button at correct position buttonPos.y = numResponses * -(buttonTransform.rect.height + buttonSpacing); buttonTransform.anchoredPosition = buttonPos; // TODO parse response text and evaluate result buttonText.text = response.Text; buttonScript.onClick.AddListener(() => uiManager.manager.ResponseSelected(ID)); // Calculate width to determine widest button needed TextGenerator textGen = new TextGenerator(); TextGenerationSettings genSettings = buttonText.GetGenerationSettings(buttonTransform.rect.size); float buttonWidth = textGen.GetPreferredWidth(buttonText.text, genSettings) + buttonPadding; maxResponseWidth = Mathf.Max(maxResponseWidth, buttonWidth); numResponses++; } }
private static string WrapLine(this TextGenerator self, string line, TextGenerationSettings settings) { string str = string.Empty; float lineWidth = 0; float speceWidth = self.GetPreferredWidth(" ", settings); float maxWidth = (settings.generationExtents.x > 20 ? settings.generationExtents.x : 99999) * settings.scaleFactor; List <string> lines = new List <string>(50); var words = line.Split(' '); for (int i = words.Length - 1; i >= 0; --i) { lineWidth += self.GetPreferredWidth(words[i], settings) + speceWidth; if (lineWidth > maxWidth && str.Length > 0) { lineWidth = 0; lines.Add(str); str = string.Empty; i++; } else if (str.Length > 0) { str = words[i] + " " + str; } else { str = words[i]; } } if (str.Length > 0) { lines.Add(str); } return(string.Join("\n", lines.ToArray())); }
/// <summary> /// Get the height of the UI Text string. /// </summary> /// <param name="uiText">Unity text component</param> /// <param name="text">Text to be calculated</param> /// <returns></returns> public static float GetUiTextHeight(Text uiText, string text) { TextGenerator textGen = new TextGenerator(); TextGenerationSettings generationSettings = uiText.GetGenerationSettings(uiText.rectTransform.rect.size); return(textGen.GetPreferredHeight(text, generationSettings)); }
private float GetTextHeight() { string layoutText = inputField.text; Vector2 generationSize = new Vector2(); if (inputField.lineType == InputField.LineType.SingleLine) { generationSize = new Vector2(float.MaxValue, float.MaxValue); layoutText = layoutText.Replace("\n", ""); } else { generationSize = new Vector2(inputText.rectTransform.rect.width, float.MaxValue); } TextGenerator textGenerator = inputText.cachedTextGeneratorForLayout; TextGenerationSettings textGenerationSettings = inputText.GetGenerationSettings(generationSize); if (materialUiScaler == null) { Debug.LogError("Must have a MaterialUIScaler atached to root canvas"); return(0); } return(textGenerator.GetPreferredHeight(layoutText, textGenerationSettings) / materialUiScaler.scaleFactor); }
void SetText(string t) { mainText.alignment = TextAnchor.UpperCenter; mainText.text = t; dummy.text = t; int hmax = 525; if (statTestInteraction.isCumulative && !statTestInteraction.passFail) { hmax = 410; } TextGenerator textGen = new TextGenerator(); TextGenerationSettings generationSettings = mainText.GetGenerationSettings(mainText.rectTransform.rect.size); float height = textGen.GetPreferredHeight(t, generationSettings); //Debug.Log( height );//lineheight=35 //Regex rx = new Regex( @"\r\n" ); //MatchCollection matches = rx.Matches( t ); //height -= matches.Count * 35; var windowH = Math.Min(hmax, height + 80 + 80); if (height + 80 + 80 > hmax) { mainText.alignment = TextAnchor.UpperCenter; } else { mainText.alignment = TextAnchor.MiddleCenter; } rect.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, windowH); /*height + 80 + 80*/ content.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, height + 80); }
public TextInfo Update(TextGenerationSettings tgs) { textInfo.isDirty = true; TextGenerator.GenerateText(tgs, textInfo); textGenerationSettings = tgs; return(textInfo); }
public Vector2 TextBBox(Vector2 restrain) { if (Text == null) { return(new Vector2(-1, -1)); } TextGenerationSettings settings = new TextGenerationSettings(); settings.textAnchor = Text.alignment; settings.color = Text.color; settings.generationExtents = restrain; settings.pivot = Vector2.zero; settings.richText = Text.supportRichText; settings.font = Text.font; settings.fontSize = fontSize_px; settings.verticalOverflow = VerticalWrapMode.Overflow; settings.horizontalOverflow = HorizontalWrapMode.Wrap; settings.lineSpacing = Text.lineSpacing; settings.generateOutOfBounds = true; settings.resizeTextForBestFit = false; settings.scaleFactor = 1f; TextGenerator generator = new TextGenerator(); return(new Vector2(generator.GetPreferredWidth(Text.text, settings), generator.GetPreferredHeight(Text.text, settings))); }
public TextGenerationSettings GetGenerationSettings(Vector2 extents) { var settings = new TextGenerationSettings(); settings.generationExtents = extents; if (font != null && font.dynamic) { settings.fontSize = m_FontData.fontSize; settings.resizeTextMinSize = m_FontData.minSize; settings.resizeTextMaxSize = m_FontData.maxSize; } // Other settings settings.textAnchor = m_FontData.alignment; settings.scaleFactor = pixelsPerUnit; settings.color = color; settings.font = font; settings.pivot = rectTransform.pivot; settings.richText = m_FontData.richText; settings.lineSpacing = m_FontData.lineSpacing; settings.fontStyle = m_FontData.fontStyle; settings.resizeTextForBestFit = m_FontData.bestFit; settings.updateBounds = false; settings.horizontalOverflow = m_FontData.horizontalOverflow; settings.verticalOverflow = m_FontData.verticalOverflow; return(settings); }
public Vector2 GetViewSize() { var scale = Vector3.one; if (canvas == null) { canvas = transform.GetComponentInParent(typeof(Canvas)) as Canvas; } if (canvas) { scale = canvas.transform.localScale; } var strContent = this.text; var strOutput = GetOutputText(strContent); TextGenerator tg = new TextGenerator(); var size = new Vector2(this.rectTransform.rect.size.x, 0.0f); TextGenerationSettings settingsHeight = this.GetGenerationSettings(size); //txt是用来显示Text组件 float height = tg.GetPreferredHeight(strOutput, settingsHeight); var size2 = new Vector2(0, height); TextGenerationSettings settingsWidth = this.GetGenerationSettings(size2); //txt是用来显示Text组件 float width = tg.GetPreferredWidth(strOutput, settingsWidth); height = Mathf.Round(height / scale.y); width = Mathf.Round(width / scale.x); // Debug.LogFormat("width={0},height={1}, text={2}", width, height, strContent); // Debug.LogFormat("{0}", strOutput); return(new Vector2(width, height)); }
private void _HandleLinkTag(VertexHelper toFill, IList <UIVertex> verts, TextGenerationSettings setting) { this.verts = verts; GetLines(m_Lines); GetBounds(toFill, mUnderlineTagInfos); //绘制underLine 实现有点bug,先不开启 //TextGenerator textGenerator = new TextGenerator(); //textGenerator.Populate("_",setting); //IList<UIVertex> underlineVerts = textGenerator.verts; //for(int m=0;m<mUnderlineTagInfos.Count;++m) //{ // var underLineInfo = mUnderlineTagInfos[m]; // if(!underLineInfo.IsValid()) // { // continue; // } // if(underLineInfo.GetStartIndex()>=mVertexHelperRef.currentVertCount) // { // continue; // } // for(int i=0;i<underLineInfo.Boxes.Count;i++) // { // Vector3 startBoxPos = new Vector3(underLineInfo.Boxes[i].x,underLineInfo.Boxes[i].y-1,0.0f); // Vector3 endBoxPos = startBoxPos + new Vector3(underLineInfo.Boxes[i].width,0.0f,0.0f); // AddUnderlineQuad(underlineVerts,startBoxPos,endBoxPos); // } //} }
// Start is called before the first frame update void Start() { textGen = new TextGenerator(); settings = new TextGenerationSettings(); settings.font = text.font; settings.fontSize = text.fontSize; if (SceneManager.GetActiveScene().buildIndex == 1) { words = new List <string>((Resources.Load <TextAsset>("Story/prologue").text).Split(new[] { ' ' })); StartCoroutine(presentNextLine()); } if (SceneManager.GetActiveScene().buildIndex == 2) { words = new List <string>((Resources.Load <TextAsset>("Story/tutorial").text).Split(new[] { ' ' })); StartCoroutine(presentNextLine()); } if (SceneManager.GetActiveScene().buildIndex == 3) { words = new List <string>((Resources.Load <TextAsset>("Story/fellowship").text).Split(new[] { ' ' })); StartCoroutine(presentNextLine()); } if (SceneManager.GetActiveScene().buildIndex == 4) { words = new List <string>((Resources.Load <TextAsset>("Story/alone").text).Split(new[] { ' ' })); StartCoroutine(presentNextLine()); } }
protected override void OnShow() { base.OnShow(); this.OnClickLimb(Limb.LArm); this.m_LimbCurrentSelections[0].enabled = true; this.m_HintBG.gameObject.SetActive(true); this.m_HintText.gameObject.SetActive(true); PlayerArmorModule.Get().SetMeshesVisible(this.m_ArmorEnabled); if (this.m_ArmorEnabled) { BodyInspectionController.Get().OnArmorMeshesEnabled(); } else { BodyInspectionController.Get().OnArmorMeshesDisabled(); } this.m_ArmorBG.gameObject.SetActive(true); this.m_BackpackHint.SetActive(GreenHellGame.IsPadControllerActive() && !Inventory3DManager.Get().IsActive()); this.m_SortBackpackHint.SetActive(GreenHellGame.IsPadControllerActive() && Inventory3DManager.Get().IsActive()); TextGenerationSettings generationSettings = this.m_SelectLimbText.GetGenerationSettings(this.m_SelectLimbText.rectTransform.rect.size); generationSettings.scaleFactor = 1f; float preferredWidth = this.m_TextGen.GetPreferredWidth(this.m_SelectLimbText.text, generationSettings); this.m_SelectLimgBGSize.Set(Mathf.Max(99.75f, preferredWidth + this.m_SelectLimbW), this.m_SelectLimbBG.rectTransform.rect.size.y); this.m_SelectLimbBG.rectTransform.sizeDelta = this.m_SelectLimgBGSize; preferredWidth = this.m_TextGen.GetPreferredWidth(this.m_RotateLimbText.text, generationSettings); this.m_RotateLimgBGSize.Set(Mathf.Max(80f, preferredWidth + this.m_SelectLimbW), this.m_RotateLimbBG.rectTransform.rect.size.y); this.m_RotateLimbBG.rectTransform.sizeDelta = this.m_RotateLimgBGSize; }
/// <summary> /// /// <para> /// Convenience function to populate the generation setting for the text. /// </para> /// /// </summary> /// <param name="extents">The extents the text can draw in.</param> /// <returns> /// /// <para> /// Generated settings. /// </para> /// /// </returns> public TextGenerationSettings GetGenerationSettings(Vector2 extents) { TextGenerationSettings generationSettings = new TextGenerationSettings(); generationSettings.generationExtents = extents; if ((UnityEngine.Object) this.font != (UnityEngine.Object)null && this.font.dynamic) { generationSettings.fontSize = this.m_FontData.fontSize; generationSettings.resizeTextMinSize = this.m_FontData.minSize; generationSettings.resizeTextMaxSize = this.m_FontData.maxSize; } generationSettings.textAnchor = this.m_FontData.alignment; generationSettings.alignByGeometry = this.m_FontData.alignByGeometry; generationSettings.scaleFactor = this.pixelsPerUnit; generationSettings.color = this.color; generationSettings.font = this.font; generationSettings.pivot = this.rectTransform.pivot; generationSettings.richText = this.m_FontData.richText; generationSettings.lineSpacing = this.m_FontData.lineSpacing; generationSettings.fontStyle = this.m_FontData.fontStyle; generationSettings.resizeTextForBestFit = this.m_FontData.bestFit; generationSettings.updateBounds = false; generationSettings.horizontalOverflow = this.m_FontData.horizontalOverflow; generationSettings.verticalOverflow = this.m_FontData.verticalOverflow; return(generationSettings); }
public TextGenerationSettings GetGenerationSettings(Vector2 extents) { var settings = new TextGenerationSettings(); var data = FontData.defaultFontData; data.font = Font.GetDefault(); settings.generationExtents = extents; // if (data.font != null && data.font.dynamic) // { // settings.fontSize = data.fontSize; // settings.resizeTextMinSize = data.minSize; // settings.resizeTextMaxSize = data.maxSize; // } // Other settings settings.textAnchor = data.alignment; settings.alignByGeometry = data.alignByGeometry; settings.scaleFactor = 1; settings.color = Color.white; settings.font = data.font; settings.pivot = Vector2.zero; settings.richText = data.richText; settings.lineSpacing = data.lineSpacing; settings.fontStyle = data.fontStyle; settings.resizeTextForBestFit = data.bestFit; settings.updateBounds = false; settings.horizontalOverflow = data.horizontalOverflow; settings.verticalOverflow = data.verticalOverflow; return(settings); }
void SetText(string t) { mainText.alignment = TextAnchor.UpperCenter; mainText.text = t; dummy.text = t; TextGenerator textGen = new TextGenerator(); TextGenerationSettings generationSettings = dummy.GetGenerationSettings(dummy.rectTransform.rect.size); float height = textGen.GetPreferredHeight(t, generationSettings); //Debug.Log( height );//lineheight=35 //Regex rx = new Regex( @"\r\n" ); //MatchCollection matches = rx.Matches( t ); //height -= matches.Count * 35; var windowH = Math.Min(525, height + 80); if (height + 80 > 525) { mainText.alignment = TextAnchor.UpperCenter; } else { mainText.alignment = TextAnchor.MiddleCenter; } rect.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, windowH); content.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, height + 20); }
public TextGenerationSettings GetGenerationSettings(Vector2 extents) { TextGenerationSettings result = default(TextGenerationSettings); result.generationExtents = extents; if (this.font != null && this.font.dynamic) { result.fontSize = this.m_FontData.fontSize; result.resizeTextMinSize = this.m_FontData.minSize; result.resizeTextMaxSize = this.m_FontData.maxSize; } result.textAnchor = this.m_FontData.alignment; result.alignByGeometry = this.m_FontData.alignByGeometry; result.scaleFactor = this.pixelsPerUnit; result.color = this.color; result.font = this.font; result.pivot = base.rectTransform.pivot; result.richText = this.m_FontData.richText; result.lineSpacing = this.m_FontData.lineSpacing; result.fontStyle = this.m_FontData.fontStyle; result.resizeTextForBestFit = this.m_FontData.bestFit; result.updateBounds = false; result.horizontalOverflow = this.m_FontData.horizontalOverflow; result.verticalOverflow = this.m_FontData.verticalOverflow; return(result); }
// Start is called before the first frame update void Start() { isInputActive = false; isChangeAlpha = false; inputField.characterLimit = maxInputLength; contentText = contentTransform.GetComponent <Text>(); contentTextGenerator = new TextGenerator(); contentTextGeneratorSettings = contentText.GetGenerationSettings(contentText.rectTransform.rect.size); oneLineHeigth = contentTextGenerator.GetPreferredHeight(" ", contentTextGeneratorSettings); chatView.verticalNormalizedPosition = 0.0f; contentWidth = contentTransform.sizeDelta.x; contentTransform.sizeDelta = new Vector2(contentWidth, GetComponent <RectTransform>().sizeDelta.y - inputField.GetComponent <RectTransform>().sizeDelta.y); emptyContentSize = contentTransform.sizeDelta.y; chatView.verticalScrollbar.size = 1.0f; chanContentLinesCount = (int)(emptyContentSize / oneLineHeigth); chatItems = new List <string>(); GameObject go = GameObject.Find("SocketIO"); socket = go.GetComponent <SocketIOComponent>(); setupEvents(); }
public void SetText(string msg) { // trim whitespace at the end while (msg.EndsWith("\n") || msg.EndsWith(" ")) { msg = msg.Remove(msg.Length - 1); } // If the text we're changing to isn't the same if (text.text != msg) { text.text = msg; // Size the Tooltip to hold the text we've given it TextGenerator textGen = new TextGenerator(); TextGenerationSettings generationSettings = text.GetGenerationSettings(background.rectTransform.sizeDelta); float width = textGen.GetPreferredWidth(msg, generationSettings); float height = textGen.GetPreferredHeight(msg, generationSettings); background.rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, width + 20); background.rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, height + 20); text.rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, width); text.rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, height); } }
protected override float ImplCalcMinSizeWidth(Dictionary <Ele, float> cache) { if (this.text.font == null) { return(0.0f); } float minWrapWidth = 50.0f; TextGenerationSettings tgs = (this.wrap == true) ? this.text.GetGenerationSettings(new Vector2(minWrapWidth, float.PositiveInfinity)) : this.text.GetGenerationSettings(new Vector2(float.PositiveInfinity, float.PositiveInfinity)); //tgs.scaleFactor = 1.0f; TextGenerator tg = this.text.cachedTextGenerator; float ret = tg.GetPreferredWidth(this.text.text, tgs) / this.text.rectTransform.lossyScale.x; // There's an odd issue with wrapping not being honored. if (this.wrap == false) { return(Mathf.Ceil(ret) + 1.0f); } else { return(Mathf.Min(minWrapWidth, ret)); } }
private IEnumerator SetupText(string text, float delay, bool wt_active) { yield return(new WaitForSeconds(delay)); if (base.enabled) { if (!this.m_Text.gameObject.activeSelf) { this.m_Text.gameObject.SetActive(true); } this.m_Text.text = GreenHellGame.Instance.GetLocalization().Get(text, true); this.m_Text.fontSize = ((GreenHellGame.Instance.m_Settings.m_Subtitles == SubtitlesSetting.Large) ? HUDDialog.FONT_SIZE_LARGE : HUDDialog.FONT_SIZE); TextGenerationSettings generationSettings = this.m_Text.GetGenerationSettings(this.m_Text.rectTransform.rect.size); generationSettings.scaleFactor = 1f; float num = Mathf.Min(this.m_TextGen.GetPreferredWidth(this.m_Text.text, generationSettings), generationSettings.generationExtents.x); this.m_WTIconActive = wt_active; if (this.m_WTIcon.gameObject.activeSelf != this.m_WTIconActive) { this.m_WTIcon.gameObject.SetActive(this.m_WTIconActive); } if (this.m_WTIcon.gameObject.activeSelf) { Vector3 localPosition = this.m_Text.rectTransform.localPosition; localPosition.x -= num * 0.5f + 25f; this.m_WTIcon.transform.localPosition = localPosition; } this.UpdateRadius(); } yield break; yield break; }
// public Material mMaterial; void Start() { TextGenerationSettings settings = new TextGenerationSettings(); settings.color = Color.red; settings.generationExtents = new Vector2(100, 100); settings.pivot = new Vector2(0.5f, 0.5f);; settings.richText = true; settings.font = font; settings.fontSize = 14; settings.fontStyle = FontStyle.Normal; settings.verticalOverflow = VerticalWrapMode.Overflow; settings.horizontalOverflow = HorizontalWrapMode.Wrap; settings.lineSpacing = 1; settings.generateOutOfBounds = true; settings.resizeTextForBestFit = false; settings.scaleFactor = 1f; mGenerator = new TextGenerator(); mGenerator.Populate("xxxxxxxxxx", settings); // GetComponent<MeshFilter>().mesh = TextGenToMesh(mGenerator); // GetComponent<MeshRenderer>().sharedMaterial = settings.font.material; GetComponent <CanvasRenderer>().SetMaterial(settings.font.material, null); GetComponent <CanvasRenderer>().SetMesh(TextGenToMesh(mGenerator)); Debug.Log("I generated: " + mGenerator.vertexCount + " verts!"); }
static TextStyle GetStyle(TextGenerationSettings generationSetting, int hashCode) { TextStyle style = null; TextStyleSheet styleSheet = generationSetting.styleSheet; // Get Style from Style Sheet potentially assigned to text object. if (styleSheet != null) { style = styleSheet.GetStyle(hashCode); if (style != null) { return(style); } } // Use Global Style Sheet (if exists) styleSheet = generationSetting.textSettings.defaultStyleSheet; if (styleSheet != null) { style = styleSheet.GetStyle(hashCode); } return(style); }
public TextGenerationSettings GetGenerationSettings(Vector2 extents) { var settings = new TextGenerationSettings(); // Settings affected by pixels density var pixelsPerUnitCached = pixelsPerUnit; settings.generationExtents = extents * pixelsPerUnitCached + Vector2.one * kEpsilon; if (font != null && font.dynamic) { settings.fontSize = Mathf.Min(Mathf.FloorToInt(m_FontData.fontSize * pixelsPerUnitCached), 1000); settings.resizeTextMinSize = Mathf.Min(Mathf.FloorToInt(m_FontData.minSize * pixelsPerUnitCached), 1000); settings.resizeTextMaxSize = Mathf.Min(Mathf.FloorToInt(m_FontData.maxSize * pixelsPerUnitCached), 1000); } // Other settings settings.textAnchor = m_FontData.alignment; settings.color = color; settings.font = font; settings.pivot = rectTransform.pivot; settings.richText = m_FontData.richText; settings.lineSpacing = m_FontData.lineSpacing; settings.fontStyle = m_FontData.fontStyle; settings.resizeTextForBestFit = m_FontData.bestFit; settings.updateBounds = false; settings.horizontalOverflow = m_FontData.horizontalOverflow; settings.verticalOverflow = m_FontData.verticalOverflow; return settings; }
public bool Equals(TextGenerationSettings other) { if (this.CompareColors(this.color, other.color) && this.fontSize == other.fontSize && (Mathf.Approximately(this.scaleFactor, other.scaleFactor) && this.resizeTextMinSize == other.resizeTextMinSize) && (this.resizeTextMaxSize == other.resizeTextMaxSize && Mathf.Approximately(this.lineSpacing, other.lineSpacing) && (this.fontStyle == other.fontStyle && this.richText == other.richText)) && (this.textAnchor == other.textAnchor && this.alignByGeometry == other.alignByGeometry && (this.resizeTextForBestFit == other.resizeTextForBestFit && this.resizeTextMinSize == other.resizeTextMinSize) && (this.resizeTextMaxSize == other.resizeTextMaxSize && this.resizeTextForBestFit == other.resizeTextForBestFit && (this.updateBounds == other.updateBounds && this.horizontalOverflow == other.horizontalOverflow))) && (this.verticalOverflow == other.verticalOverflow && this.CompareVector2(this.generationExtents, other.generationExtents) && this.CompareVector2(this.pivot, other.pivot))) return (Object) this.font == (Object) other.font; return false; }
public TextGenerationSettings GetGenerationSettings(Vector2 extents) { var settings = new TextGenerationSettings(); settings.generationExtents = extents; if (font != null && font.dynamic) { settings.fontSize = m_FontData.fontSize; settings.resizeTextMinSize = m_FontData.minSize; settings.resizeTextMaxSize = m_FontData.maxSize; } // Other settings settings.textAnchor = m_FontData.alignment; settings.alignByGeometry = m_FontData.alignByGeometry; settings.scaleFactor = pixelsPerUnit; settings.color = color; settings.font = font; settings.pivot = rectTransform.pivot; settings.richText = m_FontData.richText; settings.lineSpacing = m_FontData.lineSpacing; settings.fontStyle = m_FontData.fontStyle; settings.resizeTextForBestFit = m_FontData.bestFit; settings.updateBounds = false; settings.horizontalOverflow = m_FontData.horizontalOverflow; settings.verticalOverflow = m_FontData.verticalOverflow; return settings; }
/// <summary> /// <para>Convenience function to populate the generation setting for the text.</para> /// </summary> /// <param name="extents">The extents the text can draw in.</param> /// <returns> /// <para>Generated settings.</para> /// </returns> public TextGenerationSettings GetGenerationSettings(Vector2 extents) { TextGenerationSettings generationSettings = new TextGenerationSettings(); generationSettings.generationExtents = extents; if ((UnityEngine.Object) this.font != (UnityEngine.Object) null && this.font.dynamic) { generationSettings.fontSize = this.m_FontData.fontSize; generationSettings.resizeTextMinSize = this.m_FontData.minSize; generationSettings.resizeTextMaxSize = this.m_FontData.maxSize; } generationSettings.textAnchor = this.m_FontData.alignment; generationSettings.alignByGeometry = this.m_FontData.alignByGeometry; generationSettings.scaleFactor = this.pixelsPerUnit; generationSettings.color = this.color; generationSettings.font = this.font; generationSettings.pivot = this.rectTransform.pivot; generationSettings.richText = this.m_FontData.richText; generationSettings.lineSpacing = this.m_FontData.lineSpacing; generationSettings.fontStyle = this.m_FontData.fontStyle; generationSettings.resizeTextForBestFit = this.m_FontData.bestFit; generationSettings.updateBounds = false; generationSettings.horizontalOverflow = this.m_FontData.horizontalOverflow; generationSettings.verticalOverflow = this.m_FontData.verticalOverflow; return generationSettings; }
public TextContext(Factory f, Data data, Format.Text text) { m_factory = f; Format.TextProperty textProperty = data.textProperties[text.textPropertyId]; Format.Font fontProperty = data.fonts[textProperty.fontId]; string fontName = data.strings[fontProperty.stringId]; float fontHeight = (float)textProperty.fontHeight; float width = (float)text.width; m_height = (float)text.height; #if !UNITY_4_5 float lineSpacing = 1.0f + (float)textProperty.leading / fontHeight; #endif //float letterSpacing = fontProperty.letterspacing; float leftMargin = textProperty.leftMargin / fontHeight; float rightMargin = textProperty.rightMargin / fontHeight; var font = Resources.Load<Font>(fontName); if (font == null) font = Resources.GetBuiltinResource<Font>("Arial.ttf"); int va = textProperty.align & (int)Align.VERTICAL_MASK; int a = textProperty.align & (int)Align.ALIGN_MASK; TextAnchor textAnchor = TextAnchor.UpperLeft; switch (va) { default: switch (a) { default: case (int)Align.LEFT: textAnchor = TextAnchor.UpperLeft; break; case (int)Align.RIGHT: textAnchor = TextAnchor.UpperRight; break; case (int)Align.CENTER: textAnchor = TextAnchor.UpperCenter; break; } break; case (int)Align.VERTICAL_BOTTOM: switch (a) { default: case (int)Align.LEFT: textAnchor = TextAnchor.LowerLeft; break; case (int)Align.RIGHT: textAnchor = TextAnchor.LowerRight; break; case (int)Align.CENTER: textAnchor = TextAnchor.LowerCenter; break; } break; case (int)Align.VERTICAL_MIDDLE: switch (a) { default: case (int)Align.LEFT: textAnchor = TextAnchor.MiddleLeft; break; case (int)Align.RIGHT: textAnchor = TextAnchor.MiddleRight; break; case (int)Align.CENTER: textAnchor = TextAnchor.MiddleCenter; break; } break; } var s = new TextGenerationSettings(); #if UNITY_4_5 s.anchor = textAnchor; s.extents = new Vector2(width - leftMargin - rightMargin, m_height); s.style = FontStyle.Normal; s.size = (int)fontHeight; s.wrapMode = TextWrapMode.Wrap; #else s.textAnchor = textAnchor; s.generationExtents = new Vector2(width - leftMargin - rightMargin, m_height); s.fontStyle = FontStyle.Normal; s.fontSize = (int)fontHeight; s.lineSpacing = lineSpacing; s.horizontalOverflow = HorizontalWrapMode.Wrap; s.verticalOverflow = VerticalWrapMode.Overflow; #endif s.color = factory.ConvertColor(data.colors[text.colorId]); s.pivot = new Vector2(-leftMargin, 0); s.richText = true; s.font = font; m_textGenerationSettings = s; }