Version: 2017.3

Text.alignment

切换到手册
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; } } }
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961