Я пытался выяснить это, но безуспешно. То, что я пытаюсь сделать, - сохранить старый текст, в то время как скрипт добавляет новый текст под старым текстом.Как сохранить текст старой строки?
public GameObject Text_Object;
private Text textComponent;
void Start()
{
//gets the text UI Object in canvas component
textComponent = Text_Object.GetComponent<Text>();
textComponent.text = "Hello";
Text2();
}
void Text2()
{
textComponent.text = "Hey";
}
Как видно, первый текст заменяет второй текст. То, что я пытаюсь сохранить, сохраняет старый текст, а если я нажимаю T, он печатает новый текст под старым текстом, а также добавляет команду, когда они нажимают D, он очищает текст от нового и старого текста. Image of what I am talking about
ОБНОВЛЕНО
public GameObject Text_Object;
private Text textComponent;
void AppendText(string text)
{
string current = textComponent.text;
if(string.IsNullOrEmpty(current) || current.Trim().Length == 0)
{
current = text;
}
else
{
current += string.Format("\r\n{1}", text);
}
textComponent.text = current;
}
// Use this for initialization
void Start() {
textComponent = Text_Object.GetComponent<Text>();
textComponent.text ="Hello1";
Welcome();
}
// Update is called once per frame
void Update() {
if (Input.GetKey (KeyCode.D)) {
textComponent.text = string.Empty;
}
}
void Welcome(){
textComponent.text = "Hello";
textComponent.text = "Hello2";
}
Обновлено снова
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Test : MonoBehaviour {
public GameObject Text_Object;
private Text textComponent;
void AppendText(string text)
{
textComponent = Text_Object.GetComponent<Text>();
string current = textComponent.text;
if(string.IsNullOrEmpty(current) || current.Trim().Length == 0)
{
current = text;
}
else
{
current += string.Format("\r\n{1}", text);
}
textComponent.text = current;
}
// Use this for initialization
void Start() {
AppendText ("Hmm");
AppendText ("Hi");
AppendText ("Hello");
}
// Update is called once per frame
void Update() {
if (Input.GetKey (KeyCode.D)) {
textComponent.text = string.Empty;
}
}
}
Используйте переменную строку для сохранения старого текста перед заменой с новым. – master2080