Unity 4.6 UI buttons navigation using ONLY buttons.

Hello! Me and 3 friends are working on a school project, we are going to give it an arcade style / look.
So obviously i had to remove the mouse input which i thought would allow me with EventSystem (First Selected on a button to select it) to make it work flawlessly but i was wrong, well sort of, the main menu works with arrow keys like a charm but when i load a new level and press (ESC) which is a pause menu with a couple of buttons on, i cant for some reason navigate using buttons.

My question is: How do i completely remove the mouse input/actions and just use arrow keys for the navigation.

Thanks in advance! <3

You can use Screen.lockCursor to hide the cursor and lock it to the center of the screen. Your bigger question here is how to handle UI buttons with keyboard. This would require some custom code. Likely you’d need some sort of pointer to handle what you are selecting and a container to store all the buttons you should be able to reach.

Without writing it for you, you’d need

  • Pointer to current button
  • Container for all objects
  • Input listener to translate key presses to on-clicks

4.6 has everything you need. You just need to check that your navigation is right, and that you have proper start buttons defined for each screen.

You may need to manually change the focus button via script each time you open a new menu.

You can add navigation using Redirect to... title of new-page. Some controls, like the Button, inherit from Selectable so they already have a “Navigation” drop down.

For it to work completely without the mouse you’ll also need to set the First Selected for the Event System.
89665-firstselected.png

If you have any questions let me know!

using UnityEngine;
using System.Collections;




public class mouse : MonoBehaviour {

	//drag and drop images for buttons and Cursor into the inspector
	public Texture2D curtex;
    public Texture2D but;
	public Texture2D butover;
	float cx;
	float cy;

	float sx;
	float sy;

	public int speed;
	int i;
	Rect r;
	Rect[] mybuttons;
	string[] labels;

	string display;

	int buttonnow;

	// Use this for initialization
	void Start () {

		display = "use arrows to move cursor and spacebar to push a button";
		display = display + "

Replace cursor and button images in the inspector";
if (butover == null) {butover = new Texture2D (1, 1);
butover.SetPixel(0,0,Color.green);
butover.Apply();
}
if (but == null) {but = new Texture2D (1, 1);
but.SetPixel(0,0,Color.red);
but.Apply();
}
if (curtex == null) {curtex = new Texture2D (1, 1);}

		mybuttons= new Rect[4];//<set number of buttons here

		//set button coordinates and size by screen percentages below;

		mybuttons [0] = new Rect (0.3f, 0.3f, 0.3f, 0.05f);
		mybuttons [1] = new Rect (0.8f, 0.8f, 0.1f, 0.05f);
		mybuttons [2] = new Rect (0.5f, 0.4f, 0.1f, 0.1f);
		mybuttons [3] = new Rect (0.2f, 0.5f, 0.2f, 0.1f);

		labels = new string[mybuttons.Length];
		// set button labels here
		labels [0] = "first";
		labels [1] = "second";
		labels [2] = "3";
		labels [3] = "but 4";

        if (speed == 0) {speed = 200;}

	}


	void OnGUI(){
		GUI.skin.label.alignment = TextAnchor.MiddleCenter;

		GUI.skin.label.fontSize = 20;
		GUI.Label(new Rect(0,0,sx,sy*0.2f),display);
		i = mybuttons.Length;
		while (i>0) {i--;
			r=new Rect(mybuttons<em>.x*sx,mybuttons<em>.y*sy,mybuttons<em>.width*sx,mybuttons_.height*sy);_</em></em></em>

* if(i==buttonnow){GUI.DrawTexture (r, butover);}else{GUI.DrawTexture (r, but);}*
_ GUI.Label(r,labels*);
}*_

_ GUI.DrawTexture (new Rect (cx, cy, sx*0.03f, sy * 0.03f), curtex);_

* }*

* void Update () {*
* sx = Screen.width;*
* sy = Screen.height;*
_ if(Input.GetKey(“up”)){ cy-=Time.deltaTimespeed;}
if(Input.GetKey(“down”)){cy+=Time.deltaTimespeed;}_

_ if(Input.GetKey(“right”)){cx+=Time.deltaTimespeed;}
if(Input.GetKey(“left”)){cx-=Time.deltaTimespeed;}_

* buttonnow = -1;*
* i = mybuttons.Length;*

* while (i>0) {i–;*
r=new Rect(mybuttons.xsx,mybuttons.ysy,mybuttons.widthsx,mybuttons_.heightsy);
* if(cx>r.x){if(cx<r.x+r.width){
if(cy>r.y){if(cy<r.y+r.height){
buttonnow=i;}}}}*_

* }*
* if(Input.GetKeyDown(“space”)){if(buttonnow>-1){*

* display="I Pushed button number: "+buttonnow;*
* }}*

* }*
}