public string text ;

描述

Text 显示的/字符串/值。

使用此设置可读取或编辑 Text 中显示的消息。使用其他 Text 属性,如 sizefontalignment,可更改 text 的外观。在此示例中,这些属性将更改 text

using UnityEngine;
using UnityEngine.UI;

// UI.Text.text example // // A Space keypress changes the message shown on the screen. // Two messages are used. // // Inside Awake a Canvas and Text are created.

public class Example : MonoBehaviour { private enum UpDown { Down = -1, Start = 0, Up = 1 }; private Text text; private UpDown textChanged = UpDown.Start;

void Awake() { // Load the Arial font from the Unity Resources folder. Font arial; arial = (Font)Resources.GetBuiltinResource(typeof(Font), "Arial.ttf");

// Create Canvas GameObject. GameObject canvasGO = new GameObject(); canvasGO.name = "Canvas"; canvasGO.AddComponent<Canvas>(); canvasGO.AddComponent<CanvasScaler>(); canvasGO.AddComponent<GraphicRaycaster>();

// Get canvas from the GameObject. Canvas canvas; canvas = canvasGO.GetComponent<Canvas>(); canvas.renderMode = RenderMode.ScreenSpaceOverlay;

// Create the Text GameObject. GameObject textGO = new GameObject(); textGO.transform.parent = canvasGO.transform; textGO.AddComponent<Text>();

// Set Text component properties. text = textGO.GetComponent<Text>(); text.font = arial; text.text = "Press space key"; text.fontSize = 48; text.alignment = TextAnchor.MiddleCenter;

// Provide Text position and size using RectTransform. RectTransform rectTransform; rectTransform = text.GetComponent<RectTransform>(); rectTransform.localPosition = new Vector3(0, 0, 0); rectTransform.sizeDelta = new Vector2(600, 200); }

void Update() { // Press the space key to change the Text message. if (Input.GetKeyDown(KeyCode.Space)) { if (textChanged != UpDown.Down) { text.text = "Text changed"; textChanged = UpDown.Down; } else { text.text = "Text changed back"; textChanged = UpDown.Up; } } } }
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961