public GetGenerationSettings ( Vector2 extents ) : |
||
extents | Vector2 | The extents the text can draw in. |
return |
public static float GetPreferredTextHeight(this RectTransform trans, Text text) { if (text == null) return 0; var settings = text.GetGenerationSettings(new Vector2(trans.rect.size.x, 0.0f)); return text.cachedTextGeneratorForLayout.GetPreferredHeight(text.text, settings) / text.pixelsPerUnit; }
static int GetGenerationSettings(IntPtr L) { L.ChkArgsCount(2); UnityEngine.UI.Text obj = (UnityEngine.UI.Text)L.ChkUnityObjectSelf(1, "UnityEngine.UI.Text"); var arg0 = L.ToVector2(2); TextGenerationSettings o = obj.GetGenerationSettings(arg0); L.PushLightUserData(o); return(1); }
static int GetGenerationSettings(IntPtr L) { LuaScriptMgr.CheckArgsCount(L, 2); UnityEngine.UI.Text obj = (UnityEngine.UI.Text)LuaScriptMgr.GetUnityObjectSelf(L, 1, "UnityEngine.UI.Text"); Vector2 arg0 = LuaScriptMgr.GetVector2(L, 2); TextGenerationSettings o = obj.GetGenerationSettings(arg0); LuaScriptMgr.PushValue(L, o); return(1); }
// ================================================================ // public static Vector2 createTextRect(UnityEngine.UI.Text ui_text) { Rect canvas_rect = ui_text.canvas.pixelRect; TextGenerationSettings settings = ui_text.GetGenerationSettings(new Vector2(canvas_rect.width, canvas_rect.height)); TextGenerator generator = ui_text.cachedTextGenerator; float text_width = generator.GetPreferredWidth(ui_text.text, settings); float text_height = generator.GetPreferredHeight(ui_text.text, settings); return(new Vector2(text_width, text_height)); }
public static void StretchText(UnityEngine.UI.Text _text) { TextGenerator generator = new TextGenerator(); var settings = _text.GetGenerationSettings(Vector2.zero); settings.generateOutOfBounds = true; float offsetWidth = _text.fontSize / 2; float width = generator.GetPreferredWidth(_text.text, settings) / _text.pixelsPerUnit + offsetWidth; RectTransform rt = _text.GetComponent <RectTransform>(); rt.sizeDelta = new Vector2(width, rt.sizeDelta.y); }
public static void StretchDropdown(Dropdown _dropdown) { TextGenerator generator = new TextGenerator(); UnityEngine.UI.Text textComponent = _dropdown.captionText; var settings = textComponent.GetGenerationSettings(Vector2.zero); settings.generateOutOfBounds = true; float offsetWidth = textComponent.fontSize / 2 + Mathf.Abs(textComponent.GetComponent <RectTransform>().sizeDelta.x); float width = generator.GetPreferredWidth(textComponent.text, settings) / textComponent.pixelsPerUnit + offsetWidth; RectTransform rt = _dropdown.GetComponent <RectTransform>(); rt.sizeDelta = new Vector2(width, rt.sizeDelta.y); }
static public int GetGenerationSettings(IntPtr l) { try { UnityEngine.UI.Text self = (UnityEngine.UI.Text)checkSelf(l); UnityEngine.Vector2 a1; checkType(l, 2, out a1); var ret = self.GetGenerationSettings(a1); pushValue(l, ret); return(1); } catch (Exception e) { return(error(l, e)); } }
static public GameObject AddUnderlineByText(GameObject parent, GameObject prefab, UIEventTriggerListener.VoidDelegate _Click) { GameObject res = Resources.Load("Prefabs/MouseBlock") as GameObject; GameObject go = AddChild(parent, prefab); GameObject mouseBlock = AddChild(go, res); //刷入的是换鼠标的碰撞 UnityEngine.UI.Text _underline = go.GetComponent <UnityEngine.UI.Text>(); UnityEngine.UI.Text parenttext = parent.GetComponent <UnityEngine.UI.Text>(); int length = parenttext.text.Length; _underline.text = ""; for (int i = 0; i < length * 2; i++) { _underline.text += "_"; } //Debug.Log(" _underline.flexibleHeight" + _underline.flexibleHeight); TextGenerator m_TextGenerator = _underline.cachedTextGeneratorForLayout; TextGenerationSettings m_TextGenerationSettings = _underline.GetGenerationSettings(Vector2.zero); float fWidth = m_TextGenerator.GetPreferredWidth(_underline.text, m_TextGenerationSettings); m_TextGenerationSettings = _underline.GetGenerationSettings(new Vector2(_underline.rectTransform.rect.x, 0.0f)); float fHeight = m_TextGenerator.GetPreferredHeight(_underline.text, m_TextGenerationSettings); // mouseBlock.GetComponent<MouseImageControl>()._callback = _Click; SetTransformSize(mouseBlock, new Vector2(fWidth, fHeight)); //mouseBlock.GetComponent<Rect>() _underline.rectTransform.anchorMin = Vector2.zero; _underline.rectTransform.anchorMax = Vector2.one; _underline.rectTransform.offsetMax = Vector2.zero; _underline.rectTransform.offsetMin = Vector2.zero; return(go); }
static int QPYX_GetGenerationSettings_YXQP(IntPtr L_YXQP) { try { ToLua.CheckArgsCount(L_YXQP, 2); UnityEngine.UI.Text QPYX_obj_YXQP = (UnityEngine.UI.Text)ToLua.CheckObject <UnityEngine.UI.Text>(L_YXQP, 1); UnityEngine.Vector2 QPYX_arg0_YXQP = ToLua.ToVector2(L_YXQP, 2); UnityEngine.TextGenerationSettings QPYX_o_YXQP = QPYX_obj_YXQP.GetGenerationSettings(QPYX_arg0_YXQP); ToLua.PushValue(L_YXQP, QPYX_o_YXQP); return(1); } catch (Exception e_YXQP) { return(LuaDLL.toluaL_exception(L_YXQP, e_YXQP)); } }
static int GetGenerationSettings(IntPtr L) { try { ToLua.CheckArgsCount(L, 2); UnityEngine.UI.Text obj = (UnityEngine.UI.Text)ToLua.CheckObject(L, 1, typeof(UnityEngine.UI.Text)); UnityEngine.Vector2 arg0 = ToLua.ToVector2(L, 2); UnityEngine.TextGenerationSettings o = obj.GetGenerationSettings(arg0); ToLua.PushValue(L, o); return(1); } catch (Exception e) { return(LuaDLL.toluaL_exception(L, e)); } }
public static int GetGenerationSettings_wrap(long L) { try { long nThisPtr = FCLibHelper.fc_get_inport_obj_ptr(L); UnityEngine.UI.Text obj = get_obj(nThisPtr); Vector2 arg0 = new Vector2(); FCLibHelper.fc_get_vector2(L, 0, ref arg0); UnityEngine.TextGenerationSettings ret = obj.GetGenerationSettings(arg0); long ret_ptr = FCLibHelper.fc_get_return_ptr(L); long v = FCGetObj.PushObj(ret); FCLibHelper.fc_set_value_wrap_objptr(ret_ptr, v); } catch (Exception e) { Debug.LogException(e); } return(0); }
static int GetGenerationSettings(IntPtr L) { #if UNITY_EDITOR ToluaProfiler.AddCallRecord("UnityEngine.UI.Text.GetGenerationSettings"); #endif try { ToLua.CheckArgsCount(L, 2); UnityEngine.UI.Text obj = (UnityEngine.UI.Text)ToLua.CheckObject <UnityEngine.UI.Text>(L, 1); UnityEngine.Vector2 arg0 = ToLua.ToVector2(L, 2); UnityEngine.TextGenerationSettings o = obj.GetGenerationSettings(arg0); ToLua.PushValue(L, o); return(1); } catch (Exception e) { return(LuaDLL.toluaL_exception(L, e)); } }
void paintChangeComponent() { #if TMP GUILayout.Box("", GUILayout.Width(this.position.width - offsetRihgt), GUILayout.Height(3)); GUILayout.Label("ChangeCompont", EditorStyles.boldLabel); if (GUILayout.Button("UGUI TO TMP", EditorStyles.miniButtonMid, GUILayout.Width(80f)) && 0 < Selection.gameObjects.Length) { foreach (var tar in Selection.gameObjects) { UnityEngine.UI.Text sText = tar.GetComponent <UnityEngine.UI.Text>(); if (null != sText) { Undo.DestroyObjectImmediate(sText); Debug.Log("sText text is : " + sText.text); TextMeshProUGUI tmp_text = tar.AddComponent <TextMeshProUGUI>(); tmp_text.text = sText.text; tmp_text.color = sText.color; tmp_text.fontSize = sText.fontSize; // if( sText.fontStyle == FontStyle.Bold ){ // tmp_text.fontStyle = FontStyles.Bold; // } tmp_text.font = Resources.Load("Font/MSYH SDF 1") as TextMeshProFont; switch (sText.alignment) { case TextAnchor.LowerCenter: tmp_text.alignment = TMPro.TextAlignmentOptions.Bottom; break; case TextAnchor.LowerLeft: tmp_text.alignment = TMPro.TextAlignmentOptions.BottomLeft; break; case TextAnchor.LowerRight: tmp_text.alignment = TMPro.TextAlignmentOptions.BottomRight; break; case TextAnchor.MiddleCenter: tmp_text.alignment = TMPro.TextAlignmentOptions.Center; break; case TextAnchor.MiddleRight: tmp_text.alignment = TMPro.TextAlignmentOptions.MidlineRight; break; case TextAnchor.MiddleLeft: tmp_text.alignment = TMPro.TextAlignmentOptions.MidlineLeft; break; case TextAnchor.UpperLeft: tmp_text.alignment = TMPro.TextAlignmentOptions.TopLeft; break; case TextAnchor.UpperCenter: tmp_text.alignment = TMPro.TextAlignmentOptions.Top; break; case TextAnchor.UpperRight: tmp_text.alignment = TMPro.TextAlignmentOptions.TopRight; break; default: Debug.LogError("instead the instance alignment has error!" + targetObject.name); break; } // tmp_text.alignment = TMPro.TextAlignmentOptions.Center; switch (sText.GetGenerationSettings(new Vector2(0.5f, 0.5f)).verticalOverflow) { case VerticalWrapMode.Overflow: tmp_text.OverflowMode = TMPro.TextOverflowModes.Overflow; break; case VerticalWrapMode.Truncate: tmp_text.OverflowMode = TMPro.TextOverflowModes.Truncate; break; } // tmp_text.OverflowMode = TMPro.TextOverflowModes.ScrollRect; tmp_text.enableWordWrapping = true; GameObject.DestroyImmediate(sText); } } } if (GUILayout.Button("TMP TO UGUI", EditorStyles.miniButtonMid, GUILayout.Width(80f)) && 0 < Selection.gameObjects.Length) { foreach (var tar in Selection.gameObjects) { TextMeshProUGUI sText = tar.GetComponent <TextMeshProUGUI>(); if (null != sText) { Undo.DestroyObjectImmediate(sText); Debug.Log("sText text is : " + sText.text); UnityEngine.UI.Text tmp_text = tar.AddComponent <UnityEngine.UI.Text>(); tmp_text.text = sText.text; tmp_text.color = sText.color; tmp_text.fontSize = (int)sText.fontSize; // if( sText.fontStyle == FontStyle.Bold ){ // tmp_text.fontStyle = FontStyles.Bold; // } tmp_text.font = Resources.Load("Font/fzcyjt") as Font; switch (sText.alignment) { case TMPro.TextAlignmentOptions.Bottom: tmp_text.alignment = TextAnchor.LowerCenter; break; case TMPro.TextAlignmentOptions.BottomLeft: tmp_text.alignment = TextAnchor.LowerLeft; break; case TMPro.TextAlignmentOptions.BottomRight: tmp_text.alignment = TextAnchor.LowerRight; break; case TMPro.TextAlignmentOptions.Center: tmp_text.alignment = TextAnchor.MiddleCenter; break; case TMPro.TextAlignmentOptions.MidlineRight: tmp_text.alignment = TextAnchor.MiddleRight; break; case TMPro.TextAlignmentOptions.MidlineLeft: tmp_text.alignment = TextAnchor.MiddleLeft; break; case TMPro.TextAlignmentOptions.TopLeft: tmp_text.alignment = TextAnchor.UpperLeft; break; case TMPro.TextAlignmentOptions.Top: tmp_text.alignment = TextAnchor.UpperCenter; break; case TMPro.TextAlignmentOptions.TopRight: tmp_text.alignment = TextAnchor.UpperRight; break; default: Debug.LogError("instead the instance alignment has error!" + targetObject.name); break; } // tmp_text.alignment = TMPro.TextAlignmentOptions.Center; switch (sText.OverflowMode) { case TMPro.TextOverflowModes.Overflow: // tmp_text.GetGenerationSettings().verticalOverflow = VerticalWrapMode.Overflow; break; case TMPro.TextOverflowModes.Truncate: // tmp_text.GetGenerationSettings().verticalOverflow = VerticalWrapMode.Truncate; break; } // tmp_text.OverflowMode = TMPro.TextOverflowModes.ScrollRect; // tmp_text.enableWordWrapping = true; GameObject.DestroyImmediate(sText); } } } _component = EditorGUILayout.ObjectField(_component, typeof(Object), true) as Object; if (GUILayout.Button("Store Text Reference", EditorStyles.miniButtonMid, GUILayout.Width(120f)) && null != _component) { System.Type comT = _component.GetType(); // CharacteristicCtr myinstance = _component as CharacteristicCtr; object instance = _component; Debug.Log(comT.ToString()); PropertyInfo[] myFields = _component.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance); // Display the values of the fields. List <GameObject> gos = new List <GameObject>(); DChangeArray.Clear(); for (int i = 0; i < myFields.Length; i++) { if (myFields[i].PropertyType.Equals(typeof(UnityEngine.UI.Text))) { if (myFields[i].GetValue(instance, null) != null) { Debug.Log("The value of " + myFields[i].Name + " is: " + myFields[i].GetValue(instance, null)); GameObject go = ((Component)myFields[i].GetValue(instance, null)).gameObject; DChangeArray.Add(myFields[i].Name + "_tmp", go); gos.Add(go); } } if (myFields[i].PropertyType.Equals(typeof(System.Array))) { Debug.Log("has a array"); } } Selection.objects = gos.ToArray(); } if (GUILayout.Button("Find Text Reference", EditorStyles.miniButtonMid, GUILayout.Width(120f)) && null != _component) { System.Type comT = _component.GetType(); object myinstance = _component; Debug.Log(comT.ToString()); FieldInfo[] myFields = _component.GetType().GetFields(BindingFlags.Public | BindingFlags.Instance); // Display the values of the fields. List <GameObject> gos = new List <GameObject>(); for (int i = 0; i < myFields.Length; i++) { if (myFields[i].FieldType.Equals(typeof(TextMeshProUGUI))) { // if(myFields[i].GetValue(myinstance) != null) // { // Debug.Log("The value of " + myFields[i].Name + " is: " + myFields[i].GetValue(myinstance)); // GameObject go = ((Component)myFields[i].GetValue(myinstance)).gameObject; // DChangeArray.Add(myFields[i].Name, go); // gos.Add(go); // } if (DChangeArray.ContainsKey(myFields[i].Name)) { TextMeshProUGUI component = DChangeArray[myFields[i].Name].GetComponent <TextMeshProUGUI>(); myFields[i].SetValue(myinstance, component); } } } Selection.objects = gos.ToArray(); } GUILayout.Box("", GUILayout.Width(this.position.width - offsetRihgt), GUILayout.Height(3)); #endif }
private static int GetBestFitSize(UnityEngine.UI.Text text, List <string> newStrings) { var smallestFontSize = 0; var currentText = text.text; if (newStrings == null) { newStrings = new List <string> { currentText }; } if (!newStrings.Contains(currentText)) { newStrings.Add(currentText); } var bestFitBehaviour = text.GetComponentInParent <BestFit>(); foreach (var s in newStrings) { text.text = s; text.resizeTextForBestFit = true; text.resizeTextMinSize = bestFitBehaviour ? bestFitBehaviour.MinFontSize : 1; text.resizeTextMaxSize = (bestFitBehaviour ? bestFitBehaviour.MaxFontSize : 300) + 1; text.fontSize = text.resizeTextMaxSize; text.cachedTextGenerator.Invalidate(); text.cachedTextGenerator.Populate(text.text, text.GetGenerationSettings(text.rectTransform.rect.size)); text.resizeTextForBestFit = false; var newSize = text.cachedTextGenerator.fontSizeUsedForBestFit; var newSizeRescale = text.rectTransform.rect.size.x / text.cachedTextGenerator.rectExtents.size.x; if (text.rectTransform.rect.size.y / text.cachedTextGenerator.rectExtents.size.y < newSizeRescale) { newSizeRescale = text.rectTransform.rect.size.y / text.cachedTextGenerator.rectExtents.size.y; } //log logic copied from https://bitbucket.org/Unity-Technologies/ui/src/a3f89d5f7d145e4b6fa11cf9f2de768fea2c500f/UnityEngine.UI/UI/Core/Layout/CanvasScaler.cs?at=2017.3&fileviewer=file-view-default //allows calculation to be accurate from the first frame var canvasScaler = text.GetComponentInParent <CanvasScaler>(); while (canvasScaler != null && canvasScaler.transform.parent != null && canvasScaler.transform.parent.GetComponentInParent <CanvasScaler>()) { canvasScaler = canvasScaler.transform.parent.GetComponentInParent <CanvasScaler>(); } var canvas = text.GetComponentInParent <Canvas>(); while (canvas != null && canvas.transform.parent != null && canvas.transform.parent.GetComponentInParent <Canvas>()) { canvas = canvas.transform.parent.GetComponentInParent <Canvas>(); } if (canvas && canvasScaler) { var logWidth = Mathf.Log(Screen.width / canvasScaler.referenceResolution.x, 2); var logHeight = Mathf.Log(Screen.height / canvasScaler.referenceResolution.y, 2); var logWeightedAverage = Mathf.Lerp(logWidth, logHeight, canvasScaler.matchWidthOrHeight); var logScaleFactor = Mathf.Pow(2, logWeightedAverage); if (canvas.renderMode == RenderMode.ScreenSpaceCamera && canvas.worldCamera) { logScaleFactor *= canvas.worldCamera.orthographicSize / (Screen.height * 0.5f); } if (canvas.renderMode == RenderMode.ScreenSpaceOverlay || (canvas.renderMode == RenderMode.ScreenSpaceCamera && !canvas.worldCamera) || (canvas.renderMode == RenderMode.ScreenSpaceCamera && canvas.worldCamera && canvas.worldCamera.orthographic)) { newSizeRescale *= canvas.transform.localScale.x / logScaleFactor; } } newSize = Mathf.FloorToInt(newSize * newSizeRescale); if (newSize < smallestFontSize || smallestFontSize == 0) { smallestFontSize = newSize; } } text.text = currentText; return(Mathf.Min(smallestFontSize, bestFitBehaviour ? bestFitBehaviour.MaxFontSize : 300)); }
public void Start() { m_outputTextComponent = OutputText.GetComponent<Text>(); m_outputScrollBar = OutputScrollBar.GetComponent<Scrollbar>(); m_outputViewExtents = OutputScrollView.GetComponent<RectTransform>().rect.size; m_outputTextExtents = OutputText.GetComponent<RectTransform>().rect.size; m_outputTextSettings = m_outputTextComponent.GetGenerationSettings(m_outputTextExtents); m_outputScratchBuffer = new StringBuilder(TEXT_BUFFER_SIZE); m_outputLines = new LinkedList<string>(); m_outputTextComponent.text = ""; }
/// <summary> /// Updates the text generator. /// </summary> protected virtual void UpdateTextgen() { _textGenerator.Populate(_consoleText.text, _consoleText.GetGenerationSettings(new Vector2(_consoleText.GetComponent <RectTransform>().rect.size.x, float.MaxValue))); }