How do you READ or GET info from an input from a joystick/controller?

I’m looking for a read-only input check. I’m trying to create a script that let’s people configure their controls via controller input. I need a way to READ what button was just pressed, that I can assign controller buttons and axis’ to character movement. The problem I’m running into, is that many controllers are different and I want to have a function that sends out what button was pressed, then I can assign that button. I could probably cycle through every button mapped on a controller with “for” and “if” statements, but I would assume that Unity would have something built in to return a read-only value or string of what button was just pressed.

Does this exist or am I stuck “for-looping” through every button?

(This is NOT actual code, but a form of what I’m looking for)

OnGUI()
{
  GUI.Box(new Rect(10,10,100,40),"Press a button for 'JUMP'...");
  //wait for input from user
  jump = Input.GetWhateverButtonWasJustPressed;

}

Since you asked me in my old question what I ended up doing

here is the answer, but it is not pretty:

Since you can request whether a button was pressed during Update() with compound strings like “joystick 1 button 0”, it is in my opinion easier to use these than do what rmaniotis suggested. So when my game starts up, I first make a list of every possible button string on every possible controller. I think the maximum number of controllers is 11. Then during each update, I test for all of these buttons and assign a function if needed.

It works flawlessly, it doesn’t take much time coding since the strings can be built with a for loop, it is bug-free since you do not have to mess with the editor and in-game speed is irrelevant anyway since this is only done when changing button configurations.

Still, I feel it is kind of strange there is no way to do this similar to keyboard input.

If you still need it I can post the code that builds the strings to test for, I think I should have some for axis as well, but I do not have it on me right now.

i did this, this tests all possible keycodes in the enum “KeyCode” and prints if it has been pressed. This can be used to assign inputs as Keycodes, or you can cast it to strings inputs if you know how.

System.Array values = System.Enum.GetValues(typeof(KeyCode));
foreach(KeyCode code in values){
	if(Input.GetKeyDown(code)){ print(System.Enum.GetName(typeof(KeyCode), code)); }					
}

but i am still searching how to parse the axis…

If you’re just looking to read in any button from a controller, one way would be to set up your Input Manager with an input named something like “AnyButton”.

Each input entry in the manager can support a main input button and an alternate input button. So each entry can support two of the buttons on the controller.

Add additional entries in the Input Manager using the same name of “AnyButton” (or your name of choice) to add more than two buttons per input name.

Once complete you, to test for this in your code, you can add:

jump = Input.GetButton("AnyButton");

or 

jump = Input.GetButtonDown("AnyButton");

The first statement will get the current state of the button (pressed or not pressed) as a boolean. The second will only return true right after the button is pressed down, but subsequent calls to the function will return false until the button is released and pressed again.