gpt4 book ai didi

c# - 如果输入命令,如何在保留旧文本的同时添加新文本?

转载 作者:太空宇宙 更新时间:2023-11-03 15:08:36 24 4
gpt4 key购买 nike

我基本上是在尝试保留旧文本,而如果您输入帮助,它会添加一行新文本。问题是每次我输入帮助时,它都会再次(两次)打印旧文本,然后打印新文本。

     public Canvas myCanvas;
public Text myText;

private string display = "";

List<string> chatEvents;

private bool calltext;

public InputField inputfield;
private Dictionary<string, System.Action<string,string>> commands;

protected void Awake()
{
commands = new Dictionary<string, System.Action<string,string>>();
// Add the commands you want to recognise along with the functions to call
commands.Add( "help", OnHelpTyped );
// Listen when the inputfield is validated
inputfield.onEndEdit.AddListener( OnEndEdit );
}

private void OnEndEdit( string input )
{
// Only consider onEndEdit if the Submit button has been pressed
if ( !Input.GetButtonDown( "Submit" ) )
return;

bool commandFound = false;

// Find the command
foreach ( var item in commands )
{
if ( item.Key.ToLower().StartsWith( input.ToLower() ) )
{
commandFound = true;
item.Value( item.Key, input );
break;
}
}

// Do something if the command has not been found
if ( !commandFound )
Debug.Log( "No word found" );

// Clear the input field (if you want)
inputfield.text = "";
}

private void OnHelpTyped( string command, string input )
{
chatEvents.Add ("The List");
calltext = true;
}

// Use this for initialization
void Start () {
chatEvents = new List<string>();

chatEvents.Add("Welcome to my simple application ");
chatEvents.Add ("Type help for list of commands");

calltext = true;

}

// Update is called once per frame
void Update () {
if(calltext)
{
AddText();
calltext = false;
}
}

void AddText()
{
foreach(string msg in chatEvents)
{
display = display.ToString () + msg.ToString() + "\n";
}
myText.text = display;
}
}

这是前后对比图

Image 1

Image 2

最佳答案

您似乎从未清除过chatEvents 列表,因此每次您调用AddText 时,您都会遍历chatEvents 中的所有字符串并添加它们再次。

关于c# - 如果输入命令,如何在保留旧文本的同时添加新文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42182897/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com