如何保留旧行文字?

时间:2017-02-08 11:42:46

标签: c# unity3d unity5

我一直试图弄清楚这一点,但没有成功。我想要做的是在脚本在旧文本下添加新文本时保留旧文本。

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

2 个答案:

答案 0 :(得分:1)

欢迎来到SO。你的问题对我来说似乎有点不清楚,但如果我说得对,你想要这样的事情:

   <Page>
        <Page.Resources>
                <Style TargetType="{x:Type Button}" x:Key="NonFocusButton">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type Button}">
                            <Border Name="Border" 
                        Background="Blue"
                        BorderBrush="Blue">
                            </Border>
                            <ControlTemplate.Triggers>
                                <Trigger Property="Button.IsPressed" Value="True">
                                    <Setter TargetName="Border" Property="BorderBrush" Value="Blue" />
                                </Trigger>
                                <Trigger Property="IsMouseOver" Value="true">
                                    <Setter TargetName="Border" 
                              Property="Background" Value="Blue" />
                                </Trigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>

        <Button x:Name"myButton" Style="{StaticResource NonFocusButton}" /> //Assign style here
        </Page.Resources>
    </Page>

希望有所帮助

答案 1 :(得分:1)

您可以创建方法AppendText,然后格式化输入参数并将其附加到当前文本:

void AppendText(string text)
{
    string current = textComponent.text;
    if(string.IsNullOrWhiteSpace(current))
    {
        current = text;
    }
    else
    {
        current += string.Format("\r\n{1}", text);
    }
    textComponent.text = current;
}

现在你所要做的就是调用AppendText方法:

AppendText("hello");
AppendText("world");
AppendText("!");

结果应如下所示:

hello
world
!

编辑:

(如果出现错误消息System.string does not contain a definition for IsNullOrWhiteSpace

而不是:

if (string.IsNullOrWhiteSpace(current))

使用:

if(string.IsNullOrEmpty(current) || current.Trim().Length == 0)

(清除文字)

您可以创建一个方法,然后将其固定到UnityEvent

public void ClearText()
{
    textComponent.text = string.Empty;
}