If you're not seeing the list of commands when you type "list" in the console input field, it's possible that the Label component used to display the list is not anchored correctly to the console window.
Here's how you can modify the ListCommands() method to ensure that the list is displayed correctly:
- Calculate the position of the first label in the list based on the position of the console window:
private const int commandListYOffset = 100;
private void ListCommands()
{
int y = consoleRect.y + commandListYOffset;
GUI.Label(new Rect(consoleRect.x + 12, y, consoleWidth - 24, 20), "Available commands:");
y += 20;
GUI.Label(new Rect(consoleRect.x + 12, y, consoleWidth - 24, 20), "- heal: Restores 100 health points");
y += 20;
GUI.Label(new Rect(consoleRect.x + 12, y, consoleWidth - 24, 20), "- list: Displays a list of available commands");
}
This code calculates the position of the first label in the list by adding a vertical offset (commandListYOffset) to the y-coordinate of the console window. It then creates a new Rect object for each label in the list, with the x-coordinate set to the left edge of the console window, and the y-coordinate set to the position of the previous label plus the label height.
- Modify the
OnGUI() method to ensure that the list is displayed only when it's supposed to be:
private void OnGUI()
{
if (showConsole)
{
Rect consoleRect = new Rect(
(Screen.width - consoleWidth) / 2,
(Screen.height - consoleHeight) / 2,
consoleWidth,
consoleHeight
);
GUI.Box(consoleRect, "Console");
inputText = GUI.TextField(new Rect(consoleRect.x + 12, consoleRect.y + 32, consoleWidth - 24, 20), inputText);
if (GUI.Button(new Rect(consoleRect.x + 12, consoleRect.y + 62, 80, 20), "Heal"))
{
Heal();
}
if (GUI.Button(new Rect(consoleRect.x + 12, consoleRect.y + 92, 80, 20), "List"))
{
ListCommands();
}
if (GUI.Button(new Rect(consoleRect.x + 12, consoleRect.y + 122, 80, 20), "Close"))
{
showConsole = false;
}
if (Event.current.isKey && Event.current.keyCode == KeyCode.Return)
{
ExecuteCommand(inputText);
inputText = "";
}
ListCommands();
}
else
{
inputText = GUI.TextField(new Rect(10, Screen.height - 30, 200, 30), inputText);
if (Event.current.isKey && Event.current.keyCode == KeyCode.Return)
{
ExecuteCommand(inputText);
inputText = "";
}
}
}
This code calls the ListCommands() method outside the if statement that checks for the "List" button press, so that the list is displayed every time the console window is open.