Command prompt like thing

Hello,

I wanna make a command prompt like thing. I want this for debugging so i can send commands like teleportMeTo(x,y,z) and so on

I wanna know these things:

command prompt - Here can i type the commands, press enter and receive the result
Checking which command is filled in - So i can excecute the command

I would pay for a command prompt in Unity. There have been a number of occasions where I wished I could just enter a command instead of doing something repetitive during setup in the editor (not game scripts, just standard 3D workflow command-line entry.) Every 3D software worth using has one, I’m surprised Unity doesn’t, considering how code-oriented it is.

I just wanna make something but i dont know how i can do that, it can’t be that hard to make a little input-box and check what’s in there and execute a function.

I’ve tried to use a GUI.TextField(), i can click it and a | (i dont know how i can call that thing) appears but i can’t type something in there, i could make some script to check which button is going down, but that will be a lot of code.

I’ve found the problem, here is my script so far and i think i can do it alone now, but if i have more questions i will use this topic.

Code so far:

var _enabled : boolean = true;
var CommandString : String;

private var CommandResults : String;
private var version : float = 0.1;
private var __Enabled : boolean = false;
private var currentCommand : String = "";
private var Player : GameObject;
private var pController : PlayerController;

function Start() {
	Player = GameObject.FindWithTag("Player");
	pController = Player.GetComponent("PlayerController"); // Connection with the Playercontroller script
}

function LateUpdate() {
	
	if(__Enabled == true) { // stop the player from walking when pressing the W button
		pController.setNoUpdate(true);
	} else {
		pController.setNoUpdate(false);
	}
	
	if(Input.GetKeyUp(KeyCode.F12))
		__Enabled = true; // Opening command prompt
	
	if(Input.GetKeyUp("return")) { // Send the command
		currentCommand = CommandString;
		CommandString = ""; 
	}
	
	if(currentCommand != "") {
		 //  Executing command
		switch(currentCommand) {
			case "/help" :
				callHelp();
			break;
			case "/exit" :
				callExit();
			break;
			default :
				callDefault();
		}
		
	}
	
}

function OnGUI () {
	
	if(_enabled  __Enabled) {
	
		CommandString = GUI.TextField(Rect(25, Screen.height - 25, Screen.width / 4, 25), CommandString); // the type prompt
		GUI.Box(Rect(25, Screen.height - 205, Screen.width / 4, 175), CommandResults); // results
		
	}
	
}

private function callHelp() {
	CommandReceives += "\n/exit - Exit the command prompt";
	
}

I think alot of reasoning for a command prompt can be thrown out from just having the editor running. You can move stuff as you would in edit mode in play mode as well. Also as far as commands you can use ContextMenu attribute to run things from the editor in or out of play mode. However if the deal is trying to change things in a standalone build then yea it would help.

Take a look at ContextMenu. You just right click on the component type in the inspector (on a game object) and you should be able to execute commands directly.

ContextMenu, based on reading the docs, is nothing like a command prompt. In a command prompt you type in a line of code and hit enter and it runs. End of story. That is what it is for. For example, I could select something and type something like (I’m not sure how Unity provides the currently selected GameObject) Selected.AddComponent(“RigidBody”); and it would add a rigidBody to the selected object (or all of them if written that way, which is even better - multi-object workflows don’t really exist in unity). Then i can click my next object and, in the command prompt, hit the up arrow and hit enter and it repeats. A command prompt can be the fastest way to work in most applications, once you get used to it.

Another example… Select a bunch of objects and type: transform.localPosition = Vector3.zero . All selected objects are now “zeroed” under their parent. most of us know the code, but in a command prompt you can get work in the editor done much faster.

I get what your saying, But if you use RequireComponent attribute you can accomplish what your asking for automatically. Otherwise control + D to duplicate another object.

for stuff like transform.localPosition = Vector3.zero you can just right click the top of the transform component on the game object and hit reset.

These solutions aren’t answering the fact that you want to be able to type this in, but i think there just as fast and get the job done. C# i do not believe is parsable / suitable for command line but js might be. I’m just suggesting ways to go about accomplishing things with the current system in place.

Those were just simple examples. I’ll come back when I have a real-world one, though I’m more in code than setup at the moment.

I wasn’t aware you can select multiple objects and right click in the inspector to reset them though. I’ll have to try that later.

Yea i don’t know if you can but its pretty easy just to select each one - right click - reset.

I think it isn’t that hard to add a command like ‘currentTarget.changePosition(x,y,z)’, but i don’t know how to make it when i hit the play button again, so the game stops running, that the settings will be as i made them with that command.

Another question, i have a GUI.Box for the result lines, but when it gets more lines it doesn’t show them when they go out of the box range, how can i make this that i get a scrollbar in the Box when there are more than 7/8 lines?

You could use GUI.Box(rect, content, “textArea”) to display the box as a textArea, which will do word wrap. Though it wont do scroll bars. If you want scroll bars you’d have to do something like

Rect rect = ( enter the rect here );
GUILayout.BeginArea( rect );
scrollPosition = GUILayout.BeginScrollView( scrollPosition );
GUILayout.BeginVertical();
GUILayout.BeginHorizontal();
GUILayout.Box( content );
GUILayout.FlexibleSpace();
GUILayout.EndHorizontal();
GUILayout.FlexibleSpace();
GUILayout.EndVertical();
GUILayout.EndScrollView();
GUILayout.EndArea();

This way it will automatically size the box inside the rect containing the scroll box and keep it upper left. it should only draw a horizontal scroll bar if the rect is tall enough for the horizontal scroll bar and the box itself.

And you’ll need scrollPosition ( Vector2 ) to be added as a member or static

hey check this out Unity Lua Interface Library | Integration | Unity Asset Store
i use Lua to run scripts in game.
you will have to write a c# method for every command you want
but its really not to bad.
something like …

public class comandline
{

"set up Lua stuff"

public void runCommand(string command)
{
  Lua.Do(command);
 //ummm i think thats right but just look at the examples in the asset
}

public void teleportMeTo(int x, int y, int z)
{
  " set your players new x y and z" 
}

add that script to a GameObject then you can set up a GUI with a text field and a “run” button.
when you hit the run button call commandline.runCommand(“string from textfield”);
in this case you would want the string from the text field to be something like " teleportMeTo(7,11,90); "

hope that is clear enough kinda just ran through it because i have to go to sleep. let me know <:
good luck !