Advertisement
EmmyDev

Read Input Section

Dec 29th, 2022 (edited)
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.71 KB | Source Code | 0 0
  1. public void SubmitString()
  2.     {
  3.        
  4.         //saves string for processing
  5.         _currentLine = console.text;
  6.        
  7.         //stops if string is empty
  8.         if(_currentLine == "")
  9.         {
  10.             return;
  11.         }
  12.  
  13.  
  14.  
  15.         //logs previous input
  16.         Color consoleColor = consoleActiveText.color;
  17.  
  18.         GameObject tempText = Instantiate(previousInputText, consoleContainer.transform);
  19.         tempText.transform.position = console.transform.position;
  20.         tempText.GetComponent<TextMeshProUGUI>().text = _currentLine;
  21.         tempText.GetComponent<TextMeshProUGUI>().color = consoleColor;
  22.        
  23.         //Response...
  24.         GameObject tempResponse = Instantiate(previousInputText, consoleContainer.transform);
  25.         tempResponse.transform.position = console.transform.position;
  26.         tempResponse.transform.position -= new Vector3(0, 7f, 0);
  27.         StringProcessor(_currentLine, tempResponse);
  28.  
  29.  
  30.         console.transform.position -= new Vector3(0, 14f, 0);
  31.  
  32.         //resets console
  33.         console.text = null;  
  34.        
  35.         //Check if console is out of bounds
  36.         // while(console.transform.position.y < (-50f))
  37.         // {
  38.         //     consoleContainer.transform.position += new Vector3(0, 14f, 0);
  39.         // }
  40.        
  41.         ConsoleFocus();
  42.     }
  43.  
  44.     private void StringProcessor(string line, GameObject output)
  45.     {
  46.        
  47.         var command = Regex.Match(line, @"^([\w\-]+)");
  48.        
  49.         foreach (string commands in knownCommands)
  50.         {
  51.             if (string.Equals(commands, command.ToString().ToUpper()))
  52.             {
  53.                 //command exists call with 1 parameter
  54.                 try
  55.                 {
  56.                     StartCoroutine(COMMAND(command.ToString().ToUpper(), line.ToString().Split(' ')[1], output.GetComponent<TextMeshProUGUI>()));
  57.                     return;
  58.                 }
  59.                 catch
  60.                 {
  61.                     output.GetComponent<TextMeshProUGUI>().text = "Missing second parameter";
  62.                     return;
  63.                 }
  64.                
  65.             }      
  66.         }
  67.        
  68.         //unrecognized
  69.         string response = ("Unrecognized command: '" + command + "'");
  70.         output.GetComponent<TextMeshProUGUI>().text = response;
  71.        
  72.         return;
  73.          
  74.     }
  75.  
  76.  
  77.     private void ConsoleFocus()
  78.     {
  79.         console.enabled = true;
  80.         EventSystem.current.SetSelectedGameObject(console.gameObject, null);
  81.         console.OnPointerClick(new PointerEventData(EventSystem.current));
  82.     }
  83.     private void ConsoleUnfocus()
  84.     {  
  85.         EventSystem.current.SetSelectedGameObject(null);
  86.         console.enabled = false;
  87.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement