Text.alignment

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

public TextAnchor alignment;

Description

The positioning of the text reliative to its RectTransform.

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.

//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 UI Tutorials pages and 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; } } }
对文档有任何疑问,请移步至开发者社区提问,我们将尽快为您解答