Sure, here's how you could add a "List" command to display all available commands in the console:
using UnityEngine;
public class ConsoleCommand : MonoBehaviour
{
private string consoleText = "";
private bool showConsole = false;
private PlayerVitals playerVitals;
private string command;
private string[] commands = { "heal", "list" }; // Add more commands to this list
private void OnGUI()
{
if(showConsole)
{
float y = 60f;
GUI.Box(new Rect(0, y, Screen.width, 30), "");
GUI.backgroundColor = Color.black;
consoleText = GUI.TextField(new Rect(10f, y + 5f, Screen.width - 20f, 20f), consoleText);
if (Event.current.isKey && Event.current.keyCode == KeyCode.Return)
{
if(consoleText == "heal")
{
Heal();
}
else if (consoleText == "list")
{
ListCommands();
}
consoleText = "";
}
}
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.BackQuote))
{
showConsole = !showConsole;
consoleText = "";
}
}
private void Heal()
{
playerVitals.currentHealth += 100;
}
private void ListCommands()
{
foreach(string command in commands)
{
Debug.Log(command);
}
}
}
In the above code, a new commands array was added that contains all available commands. The ListCommands method was also added, which iterates through the commands array and logs each command to the console.
The OnGUI method was updated to check if the entered command is "list", and if so, call the ListCommands method.
Please note that the output of the "list" command will be displayed in Unity's console, not in the game's console. If you want to display it in the game's console, you would need to add more code to handle this.