public TextAnchor alignment ;

描述

文本相对其 RectTransform 的定位。

这是 Text 相对其 RectTransform 的定位。可以通过脚本修改此设置,或在 Text 组件的 Inspector 中使用 Alignment 部分中的按钮修改此设置。

//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; } } }