docs.unity3d.com
Search Results for

    Show / Hide Table of Contents

    Class Text

    Inheritance
    object
    Object
    Component
    Behaviour
    MonoBehaviour
    UIBehaviour
    Graphic
    MaskableGraphic
    Text
    Implements
    ICanvasElement
    IClippable
    IMaskable
    IMaterialModifier
    ILayoutElement
    Inherited Members
    MaskableGraphic.m_ShouldRecalculateStencil
    MaskableGraphic.m_MaskMaterial
    MaskableGraphic.onCullStateChanged
    MaskableGraphic.maskable
    MaskableGraphic.isMaskingGraphic
    MaskableGraphic.m_StencilValue
    MaskableGraphic.GetModifiedMaterial(Material)
    MaskableGraphic.Cull(Rect, bool)
    MaskableGraphic.SetClipRect(Rect, bool)
    MaskableGraphic.SetClipSoftness(Vector2)
    MaskableGraphic.OnTransformParentChanged()
    MaskableGraphic.OnCanvasHierarchyChanged()
    MaskableGraphic.RecalculateClipping()
    MaskableGraphic.RecalculateMasking()
    Graphic.s_DefaultUI
    Graphic.s_WhiteTexture
    Graphic.defaultGraphicMaterial
    Graphic.m_Material
    Graphic.m_SkipLayoutUpdate
    Graphic.m_SkipMaterialUpdate
    Graphic.color
    Graphic.raycastTarget
    Graphic.raycastPadding
    Graphic.m_OnDirtyLayoutCallback
    Graphic.m_OnDirtyVertsCallback
    Graphic.m_OnDirtyMaterialCallback
    Graphic.s_Mesh
    Graphic.m_CachedMesh
    Graphic.m_CachedUvs
    Graphic.useLegacyMeshGeneration
    Graphic.SetAllDirty()
    Graphic.SetLayoutDirty()
    Graphic.SetVerticesDirty()
    Graphic.SetMaterialDirty()
    Graphic.SetRaycastDirty()
    Graphic.OnRectTransformDimensionsChange()
    Graphic.OnBeforeTransformParentChanged()
    Graphic.depth
    Graphic.rectTransform
    Graphic.canvas
    Graphic.canvasRenderer
    Graphic.defaultMaterial
    Graphic.material
    Graphic.materialForRendering
    Graphic.OnDestroy()
    Graphic.OnCullingChanged()
    Graphic.Rebuild(CanvasUpdate)
    Graphic.LayoutComplete()
    Graphic.GraphicUpdateComplete()
    Graphic.UpdateMaterial()
    Graphic.workerMesh
    Graphic.OnPopulateMesh(Mesh)
    Graphic.OnDidApplyAnimationProperties()
    Graphic.SetNativeSize()
    Graphic.Raycast(Vector2, Camera)
    Graphic.PixelAdjustPoint(Vector2)
    Graphic.GetPixelAdjustedRect()
    Graphic.CrossFadeColor(Color, float, bool, bool)
    Graphic.CrossFadeColor(Color, float, bool, bool, bool)
    Graphic.CrossFadeAlpha(float, float, bool)
    Graphic.RegisterDirtyLayoutCallback(UnityAction)
    Graphic.UnregisterDirtyLayoutCallback(UnityAction)
    Graphic.RegisterDirtyVerticesCallback(UnityAction)
    Graphic.UnregisterDirtyVerticesCallback(UnityAction)
    Graphic.RegisterDirtyMaterialCallback(UnityAction)
    Graphic.UnregisterDirtyMaterialCallback(UnityAction)
    UIBehaviour.Awake()
    UIBehaviour.Start()
    UIBehaviour.IsActive()
    UIBehaviour.OnCanvasGroupChanged()
    UIBehaviour.IsDestroyed()
    MonoBehaviour.IsInvoking()
    MonoBehaviour.CancelInvoke()
    MonoBehaviour.Invoke(string, float)
    MonoBehaviour.InvokeRepeating(string, float, float)
    MonoBehaviour.CancelInvoke(string)
    MonoBehaviour.IsInvoking(string)
    MonoBehaviour.StartCoroutine(string)
    MonoBehaviour.StartCoroutine(string, object)
    MonoBehaviour.StartCoroutine(IEnumerator)
    MonoBehaviour.StartCoroutine_Auto(IEnumerator)
    MonoBehaviour.StopCoroutine(IEnumerator)
    MonoBehaviour.StopCoroutine(Coroutine)
    MonoBehaviour.StopCoroutine(string)
    MonoBehaviour.StopAllCoroutines()
    MonoBehaviour.print(object)
    MonoBehaviour.destroyCancellationToken
    MonoBehaviour.useGUILayout
    MonoBehaviour.didStart
    MonoBehaviour.didAwake
    MonoBehaviour.runInEditMode
    Behaviour.enabled
    Behaviour.isActiveAndEnabled
    Component.GetComponent(Type)
    Component.GetComponent<T>()
    Component.TryGetComponent(Type, out Component)
    Component.TryGetComponent<T>(out T)
    Component.GetComponent(string)
    Component.GetComponentInChildren(Type, bool)
    Component.GetComponentInChildren(Type)
    Component.GetComponentInChildren<T>(bool)
    Component.GetComponentInChildren<T>()
    Component.GetComponentsInChildren(Type, bool)
    Component.GetComponentsInChildren(Type)
    Component.GetComponentsInChildren<T>(bool)
    Component.GetComponentsInChildren<T>(bool, List<T>)
    Component.GetComponentsInChildren<T>()
    Component.GetComponentsInChildren<T>(List<T>)
    Component.GetComponentInParent(Type, bool)
    Component.GetComponentInParent(Type)
    Component.GetComponentInParent<T>(bool)
    Component.GetComponentInParent<T>()
    Component.GetComponentsInParent(Type, bool)
    Component.GetComponentsInParent(Type)
    Component.GetComponentsInParent<T>(bool)
    Component.GetComponentsInParent<T>(bool, List<T>)
    Component.GetComponentsInParent<T>()
    Component.GetComponents(Type)
    Component.GetComponents(Type, List<Component>)
    Component.GetComponents<T>(List<T>)
    Component.GetComponents<T>()
    Component.GetComponentIndex()
    Component.CompareTag(string)
    Component.CompareTag(TagHandle)
    Component.SendMessageUpwards(string, object, SendMessageOptions)
    Component.SendMessageUpwards(string, object)
    Component.SendMessageUpwards(string)
    Component.SendMessageUpwards(string, SendMessageOptions)
    Component.SendMessage(string, object)
    Component.SendMessage(string)
    Component.SendMessage(string, object, SendMessageOptions)
    Component.SendMessage(string, SendMessageOptions)
    Component.BroadcastMessage(string, object, SendMessageOptions)
    Component.BroadcastMessage(string, object)
    Component.BroadcastMessage(string)
    Component.BroadcastMessage(string, SendMessageOptions)
    Component.transform
    Component.gameObject
    Component.tag
    Object.GetInstanceID()
    Object.GetHashCode()
    Object.Equals(object)
    Object.InstantiateAsync<T>(T)
    Object.InstantiateAsync<T>(T, Transform)
    Object.InstantiateAsync<T>(T, Vector3, Quaternion)
    Object.InstantiateAsync<T>(T, Transform, Vector3, Quaternion)
    Object.InstantiateAsync<T>(T, int)
    Object.InstantiateAsync<T>(T, int, Transform)
    Object.InstantiateAsync<T>(T, int, Vector3, Quaternion)
    Object.InstantiateAsync<T>(T, int, ReadOnlySpan<Vector3>, ReadOnlySpan<Quaternion>)
    Object.InstantiateAsync<T>(T, int, Transform, Vector3, Quaternion)
    Object.InstantiateAsync<T>(T, int, Transform, Vector3, Quaternion, CancellationToken)
    Object.InstantiateAsync<T>(T, int, Transform, ReadOnlySpan<Vector3>, ReadOnlySpan<Quaternion>)
    Object.InstantiateAsync<T>(T, int, Transform, ReadOnlySpan<Vector3>, ReadOnlySpan<Quaternion>, CancellationToken)
    Object.Instantiate(Object, Vector3, Quaternion)
    Object.Instantiate(Object, Vector3, Quaternion, Transform)
    Object.Instantiate(Object)
    Object.Instantiate(Object, Scene)
    Object.Instantiate(Object, Transform)
    Object.Instantiate(Object, Transform, bool)
    Object.Instantiate<T>(T)
    Object.Instantiate<T>(T, Vector3, Quaternion)
    Object.Instantiate<T>(T, Vector3, Quaternion, Transform)
    Object.Instantiate<T>(T, Transform)
    Object.Instantiate<T>(T, Transform, bool)
    Object.Destroy(Object, float)
    Object.Destroy(Object)
    Object.DestroyImmediate(Object, bool)
    Object.DestroyImmediate(Object)
    Object.FindObjectsOfType(Type)
    Object.FindObjectsOfType(Type, bool)
    Object.FindObjectsByType(Type, FindObjectsSortMode)
    Object.FindObjectsByType(Type, FindObjectsInactive, FindObjectsSortMode)
    Object.DontDestroyOnLoad(Object)
    Object.DestroyObject(Object, float)
    Object.DestroyObject(Object)
    Object.FindSceneObjectsOfType(Type)
    Object.FindObjectsOfTypeIncludingAssets(Type)
    Object.FindObjectsOfType<T>()
    Object.FindObjectsByType<T>(FindObjectsSortMode)
    Object.FindObjectsOfType<T>(bool)
    Object.FindObjectsByType<T>(FindObjectsInactive, FindObjectsSortMode)
    Object.FindObjectOfType<T>()
    Object.FindObjectOfType<T>(bool)
    Object.FindFirstObjectByType<T>()
    Object.FindAnyObjectByType<T>()
    Object.FindFirstObjectByType<T>(FindObjectsInactive)
    Object.FindAnyObjectByType<T>(FindObjectsInactive)
    Object.FindObjectsOfTypeAll(Type)
    Object.FindObjectOfType(Type)
    Object.FindFirstObjectByType(Type)
    Object.FindAnyObjectByType(Type)
    Object.FindObjectOfType(Type, bool)
    Object.FindFirstObjectByType(Type, FindObjectsInactive)
    Object.FindAnyObjectByType(Type, FindObjectsInactive)
    Object.ToString()
    Object.name
    Object.hideFlags
    object.Equals(object, object)
    object.GetType()
    object.MemberwiseClone()
    object.ReferenceEquals(object, object)
    Namespace: UnityEngine.UI
    Assembly: UnityEngine.UI.dll
    Syntax
    [RequireComponent(typeof(CanvasRenderer))]
    [AddComponentMenu("UI/Legacy/Text", 100)]
    public class Text : MaskableGraphic, ICanvasElement, IClippable, IMaskable, IMaterialModifier, ILayoutElement

    Constructors

    Text()

    Declaration
    protected Text()

    Fields

    m_DisableFontTextureRebuiltCallback

    Declaration
    [NonSerialized]
    protected bool m_DisableFontTextureRebuiltCallback
    Field Value
    Type Description
    bool

    m_Text

    Declaration
    [TextArea(3, 10)]
    [SerializeField]
    protected string m_Text
    Field Value
    Type Description
    string

    s_DefaultText

    Declaration
    protected static Material s_DefaultText
    Field Value
    Type Description
    Material

    Properties

    alignByGeometry

    Use the extents of glyph geometry to perform horizontal alignment rather than glyph metrics.

    Declaration
    public bool alignByGeometry { get; set; }
    Property Value
    Type Description
    bool
    Remarks

    This can result in better fitting left and right alignment, but may result in incorrect positioning when attempting to overlay multiple fonts (such as a specialized outline font) on top of each other.

    alignment

    The positioning of the text reliative to its [[RectTransform]].

    Declaration
    public TextAnchor alignment { get; set; }
    Property Value
    Type Description
    TextAnchor
    Remarks

    This is the positioning of the Text relative to its RectTransform. You can alter this via script or in the Inspector of a Text component using the buttons in the Alignment section.

    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://unity3d.com/learn/tutorials/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
    Type Description
    TextGenerator

    cachedTextGeneratorForLayout

    The cached TextGenerator used when determine Layout

    Declaration
    public TextGenerator cachedTextGeneratorForLayout { get; }
    Property Value
    Type Description
    TextGenerator

    flexibleHeight

    The extra relative height this layout element should be allocated if there is additional available space.

    Declaration
    public virtual float flexibleHeight { get; }
    Property Value
    Type Description
    float
    Examples
      using UnityEngine;
                            using System.Collections;
                            using UnityEngine.UI; // Required when using UI elements.
    
                            public class ExampleClass : MonoBehaviour
                            {
                                public Transform MyContentPanel;
    
                                //Sets the flexible height on on all children in the content panel.
                                public void Start()
                                {
                                    //Assign all the children of the content panel to an array.
                                    LayoutElement[] myLayoutElements = MyContentPanel.GetComponentsInChildren<LayoutElement>();
    
                                    //For each child in the array change its LayoutElement's flexible height to 100.
                                    foreach (LayoutElement element in myLayoutElements)
                                    {
                                        element.flexibleHeight = 100f;
                                    }
                                }
                            }
    

    flexibleWidth

    The extra relative width this layout element should be allocated if there is additional available space.

    Declaration
    public virtual float flexibleWidth { get; }
    Property Value
    Type Description
    float
    Remarks

    Setting preferredWidth to -1 removed the preferredWidth.

    Examples
      using UnityEngine;
                            using System.Collections;
                            using UnityEngine.UI; // Required when using UI elements.
    
                            public class ExampleClass : MonoBehaviour
                            {
                                public Transform MyContentPanel;
    
                                //Sets the flexible height on on all children in the content panel.
                                public void Start()
                                {
                                    //Assign all the children of the content panel to an array.
                                    LayoutElement[] myLayoutElements = MyContentPanel.GetComponentsInChildren<LayoutElement>();
    
                                    //For each child in the array change its LayoutElement's flexible width to 200.
                                    foreach (LayoutElement element in myLayoutElements)
                                    {
                                        element.flexibleWidth = 200f;
                                    }
                                }
                            }
    

    font

    The Font used by the text.

    Declaration
    public Font font { get; set; }
    Property Value
    Type Description
    Font
    Remarks

    This is the font used by the Text component. Use it to alter or return the font from the Text. There are many free fonts available online.

    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
    Type Description
    int
    Remarks

    This is the size of the Font of the Text. Use this to fetch or change the size of the Font. When changing the Font size, remember to take into account the RectTransform of the Text. Larger Font sizes or messages may not fit in certain rectangle sizes and do not show in the Scene. Note: Point size is not consistent from one font to another.

    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
    Type Description
    FontStyle

    horizontalOverflow

    Horizontal overflow mode.

    Declaration
    public HorizontalWrapMode horizontalOverflow { get; set; }
    Property Value
    Type Description
    HorizontalWrapMode
    Remarks

    When set to HorizontalWrapMode.Overflow, text can exceed the horizontal boundaries of the Text graphic. When set to HorizontalWrapMode.Wrap, text will be word-wrapped to fit within the boundaries.

    layoutPriority

    The layout priority of this component.

    Declaration
    public virtual int layoutPriority { get; }
    Property Value
    Type Description
    int
    Remarks

    If multiple components on the same GameObject implement the ILayoutElement interface, the values provided by components that return a higher priority value are given priority. However, values less than zero are ignored. This way a component can override only select properties by leaving the remaning values to be -1 or other values less than zero.

    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
    Type Description
    float

    mainTexture

    Text's texture comes from the font.

    Declaration
    public override Texture mainTexture { get; }
    Property Value
    Type Description
    Texture
    Overrides
    Graphic.mainTexture

    minHeight

    The minimum height this layout element may be allocated.

    Declaration
    public virtual float minHeight { get; }
    Property Value
    Type Description
    float

    minWidth

    The minimum width this layout element may be allocated.

    Declaration
    public virtual float minWidth { get; }
    Property Value
    Type Description
    float

    pixelsPerUnit

    Provides information about how fonts are scale to the screen.

    Declaration
    public float pixelsPerUnit { get; }
    Property Value
    Type Description
    float
    Remarks

    For dynamic fonts, the value is equivalent to the scale factor of the canvas. For non-dynamic fonts, the value is calculated from the requested text size and the size from the font.

    preferredHeight

    The preferred height this layout element should be allocated if there is sufficient space.

    Declaration
    public virtual float preferredHeight { get; }
    Property Value
    Type Description
    float
    Remarks

    PreferredHeight can be set to -1 to remove the size.

    preferredWidth

    The preferred width this layout element should be allocated if there is sufficient space.

    Declaration
    public virtual float preferredWidth { get; }
    Property Value
    Type Description
    float
    Remarks

    PreferredWidth can be set to -1 to remove the size.

    resizeTextForBestFit

    Should the text be allowed to auto resized.

    Declaration
    public bool resizeTextForBestFit { get; set; }
    Property Value
    Type Description
    bool

    resizeTextMaxSize

    The maximum size the text is allowed to be. 1 = infinitely large.

    Declaration
    public int resizeTextMaxSize { get; set; }
    Property Value
    Type Description
    int

    resizeTextMinSize

    The minimum size the text is allowed to be.

    Declaration
    public int resizeTextMinSize { get; set; }
    Property Value
    Type Description
    int

    supportRichText

    Whether this Text will support rich text.

    Declaration
    public bool supportRichText { get; set; }
    Property Value
    Type Description
    bool

    text

    Text that's being displayed by the Text.

    Declaration
    public virtual string text { get; set; }
    Property Value
    Type Description
    string
    Remarks

    This is the string value of a Text component. Use this to read or edit the message displayed in Text.

    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

    Vertical overflow mode.

    Declaration
    public VerticalWrapMode verticalOverflow { get; set; }
    Property Value
    Type Description
    VerticalWrapMode

    Methods

    CalculateLayoutInputHorizontal()

    After this method is invoked, layout horizontal input properties should return up-to-date values. Children will already have up-to-date layout horizontal inputs when this methods is called.

    Declaration
    public virtual void CalculateLayoutInputHorizontal()

    CalculateLayoutInputVertical()

    After this method is invoked, layout vertical input properties should return up-to-date values. Children will already have up-to-date layout vertical inputs when this methods is called.

    Declaration
    public virtual void CalculateLayoutInputVertical()

    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
    Type Description
    TextGenerationSettings

    Generated settings.

    GetTextAnchorPivot(TextAnchor)

    Convenience function to determine the vector offset of the anchor.

    Declaration
    public static Vector2 GetTextAnchorPivot(TextAnchor anchor)
    Parameters
    Type Name Description
    TextAnchor anchor
    Returns
    Type Description
    Vector2

    OnDisable()

    Clear references.

    Declaration
    protected override void OnDisable()
    Overrides
    MaskableGraphic.OnDisable()

    OnEnable()

    Mark the Graphic and the canvas as having been changed.

    Declaration
    protected override void OnEnable()
    Overrides
    MaskableGraphic.OnEnable()

    OnPopulateMesh(VertexHelper)

    Callback function when a UI element needs to generate vertices. Fills the vertex buffer data.

    Declaration
    protected override void OnPopulateMesh(VertexHelper toFill)
    Parameters
    Type Name Description
    VertexHelper toFill
    Overrides
    Graphic.OnPopulateMesh(VertexHelper)
    Remarks

    Used by Text, UI.Image, and RawImage for example to generate vertices specific to their use case.

    OnRebuildRequested()

    Editor-only callback that is issued by Unity if a rebuild of the Graphic is required. Currently sent when an asset is reimported.

    Declaration
    public override void OnRebuildRequested()
    Overrides
    Graphic.OnRebuildRequested()

    OnValidate()

    Declaration
    protected override void OnValidate()
    Overrides
    MaskableGraphic.OnValidate()

    Reset()

    Declaration
    protected override void Reset()
    Overrides
    Graphic.Reset()

    UpdateGeometry()

    Call to update the geometry of the Graphic onto the CanvasRenderer.

    Declaration
    protected override void UpdateGeometry()
    Overrides
    Graphic.UpdateGeometry()

    Implements

    ICanvasElement
    IClippable
    IMaskable
    IMaterialModifier
    ILayoutElement
    In This Article
    Back to top
    Copyright © 2024 Unity Technologies — Trademarks and terms of use
    • Legal
    • Privacy Policy
    • Cookie Policy
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)