Class Text
Inherited Members
UnityEngine.Component.GetComponentInParent<T>(System.Boolean)
UnityEngine.Object.FindObjectsByType<T>(UnityEngine.FindObjectsSortMode)
UnityEngine.Object.FindObjectsOfType<T>(System.Boolean)
UnityEngine.Object.FindObjectsByType<T>(UnityEngine.FindObjectsInactive, UnityEngine.FindObjectsSortMode)
UnityEngine.Object.FindObjectOfType<T>(System.Boolean)
UnityEngine.Object.FindFirstObjectByType<T>()
UnityEngine.Object.FindAnyObjectByType<T>()
UnityEngine.Object.FindFirstObjectByType<T>(UnityEngine.FindObjectsInactive)
UnityEngine.Object.FindAnyObjectByType<T>(UnityEngine.FindObjectsInactive)
Syntax
[RequireComponent(typeof(CanvasRenderer))]
[AddComponentMenu("UI/Legacy/Text", 100)]
public class Text : MaskableGraphic, ICanvasElement, IClippable, IMaskable, IMaterialModifier, ILayoutElement
Constructors
Text()
Declaration
Fields
m_DisableFontTextureRebuiltCallback
Declaration
[NonSerialized]
protected bool m_DisableFontTextureRebuiltCallback
Field Value
m_Text
Declaration
[TextArea(3, 10)]
[SerializeField]
protected string m_Text
Field Value
s_DefaultText
Declaration
protected static Material s_DefaultText
Field Value
Properties
alignByGeometry
Use the extents of glyph geometry to perform horizontal alignment rather than glyph metrics.
Declaration
public bool alignByGeometry { get; set; }
Property Value
alignment
The positioning of the text reliative to its [[RectTransform]].
Declaration
public TextAnchor alignment { get; set; }
Property Value
Examples
//Create a Text GameObject by going to __Create__>__UI__>__Text__. Attach this script to the GameObject to see it working.
using UnityEngine;
using UnityEngine.UI;
public class UITextAlignment : MonoBehaviour
{
Text m_Text;
void Start()
{
//Fetch the Text Component
m_Text = GetComponent<Text>();
//Switch the Text alignment to the middle
m_Text.alignment = TextAnchor.MiddleCenter;
}
//This is a legacy function used for an instant demonstration. See the <a href="https://learn.u3d.cn/s/user-interface-ui">UI Tutorials pages </a> and [[wiki:UISystem|UI Section]] of the manual for more information on creating your own buttons etc.
void OnGUI()
{
//Press this Button to change the Text alignment to the lower right
if (GUI.Button(new Rect(0, 0, 100, 40), "Lower Right"))
{
m_Text.alignment = TextAnchor.LowerRight;
}
//Press this Button to change the Text alignment to the upper left
if (GUI.Button(new Rect(150, 0, 100, 40), "Upper Left"))
{
m_Text.alignment = TextAnchor.UpperLeft;
}
}
}
cachedTextGenerator
The cached TextGenerator used when generating visible Text.
Declaration
public TextGenerator cachedTextGenerator { get; }
Property Value
cachedTextGeneratorForLayout
The cached TextGenerator used when determine Layout
Declaration
public TextGenerator cachedTextGeneratorForLayout { get; }
Property Value
flexibleHeight
Declaration
public virtual float flexibleHeight { get; }
Property Value
Implements
flexibleWidth
Declaration
public virtual float flexibleWidth { get; }
Property Value
Implements
font
The Font used by the text.
Declaration
public Font font { get; set; }
Property Value
Examples
//Create a new Text GameObject by going to Create>UI>Text in the Editor. Attach this script to the Text GameObject. Then, choose or click and drag your own font into the Font section in the Inspector window.
using UnityEngine;
using UnityEngine.UI;
public class TextFontExample : MonoBehaviour
{
Text m_Text;
//Attach your own Font in the Inspector
public Font m_Font;
void Start()
{
//Fetch the Text component from the GameObject
m_Text = GetComponent<Text>();
}
void Update()
{
if (Input.GetKey(KeyCode.Space))
{
//Change the Text Font to the Font attached in the Inspector
m_Text.font = m_Font;
//Change the Text to the message below
m_Text.text = "My Font Changed!";
}
}
}
fontSize
The size that the Font should render at. Unit of measure is Points.
Declaration
public int fontSize { get; set; }
Property Value
Examples
//For this script to work, create a new Text GameObject by going to Create>U>Text. Attach the script to the Text GameObject. Make sure the GameObject has a RectTransform component.
using UnityEngine;
using UnityEngine.UI;
public class Example : MonoBehaviour
{
Text m_Text;
RectTransform m_RectTransform;
void Start()
{
//Fetch the Text and RectTransform components from the GameObject
m_Text = GetComponent<Text>();
m_RectTransform = GetComponent<RectTransform>();
}
void Update()
{
//Press the space key to change the Font size
if (Input.GetKey(KeyCode.Space))
{
changeFontSize();
}
}
void changeFontSize()
{
//Change the Font Size to 16
m_Text.fontSize = 30;
//Change the RectTransform size to allow larger fonts and sentences
m_RectTransform.sizeDelta = new Vector2(m_Text.fontSize * 10, 100);
//Change the m_Text text to the message below
m_Text.text = "I changed my Font size!";
}
}
fontStyle
Font style used by the Text's text.
Declaration
public FontStyle fontStyle { get; set; }
Property Value
horizontalOverflow
Horizontal overflow mode.
Declaration
public HorizontalWrapMode horizontalOverflow { get; set; }
Property Value
layoutPriority
Declaration
public virtual int layoutPriority { get; }
Property Value
Implements
lineSpacing
Line spacing, specified as a factor of font line height. A value of 1 will produce normal line spacing.
Declaration
public float lineSpacing { get; set; }
Property Value
mainTexture
Text's texture comes from the font.
Declaration
public override Texture mainTexture { get; }
Property Value
Overrides
minHeight
Declaration
public virtual float minHeight { get; }
Property Value
Implements
minWidth
Declaration
public virtual float minWidth { get; }
Property Value
Implements
pixelsPerUnit
Provides information about how fonts are scale to the screen.
Declaration
public float pixelsPerUnit { get; }
Property Value
preferredHeight
Declaration
public virtual float preferredHeight { get; }
Property Value
Implements
preferredWidth
Declaration
public virtual float preferredWidth { get; }
Property Value
Implements
resizeTextForBestFit
Should the text be allowed to auto resized.
Declaration
public bool resizeTextForBestFit { get; set; }
Property Value
resizeTextMaxSize
The maximum size the text is allowed to be. 1 = infinitely large.
Declaration
public int resizeTextMaxSize { get; set; }
Property Value
resizeTextMinSize
The minimum size the text is allowed to be.
Declaration
public int resizeTextMinSize { get; set; }
Property Value
supportRichText
Whether this Text will support rich text.
Declaration
public bool supportRichText { get; set; }
Property Value
text
Text that's being displayed by the Text.
Declaration
public virtual string text { get; set; }
Property Value
Examples
using UnityEngine;
using UnityEngine.UI;
public class Example : MonoBehaviour
{
public Text m_MyText;
void Start()
{
//Text sets your text to say this message
m_MyText.text = "This is my text";
}
void Update()
{
//Press the space key to change the Text message
if (Input.GetKey(KeyCode.Space))
{
m_MyText.text = "My text has now changed.";
}
}
}
verticalOverflow
Declaration
public VerticalWrapMode verticalOverflow { get; set; }
Property Value
Methods
CalculateLayoutInputHorizontal()
Declaration
public virtual void CalculateLayoutInputHorizontal()
Implements
CalculateLayoutInputVertical()
Declaration
public virtual void CalculateLayoutInputVertical()
Implements
FontTextureChanged()
Called by the FontUpdateTracker when the texture associated with a font is modified.
Declaration
public void FontTextureChanged()
GetGenerationSettings(Vector2)
Convenience function to populate the generation setting for the text.
Declaration
public TextGenerationSettings GetGenerationSettings(Vector2 extents)
Parameters
Type |
Name |
Description |
Vector2 |
extents |
The extents the text can draw in.
|
Returns
GetTextAnchorPivot(TextAnchor)
Convenience function to determine the vector offset of the anchor.
Declaration
public static Vector2 GetTextAnchorPivot(TextAnchor anchor)
Parameters
Returns
OnDisable()
Declaration
protected override void OnDisable()
Overrides
OnEnable()
Declaration
protected override void OnEnable()
Overrides
OnPopulateMesh(VertexHelper)
Declaration
protected override void OnPopulateMesh(VertexHelper toFill)
Parameters
Overrides
UpdateGeometry()
Declaration
protected override void UpdateGeometry()
Overrides
Extension Methods