如果键入命令,如何在保留旧文本的同时添加新文本



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

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

这是之前和之后的图像

图片 1

图片 2

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

最新更新