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