- Home /
XBox 360 controller for the GUI
I have followed the Lerpz tutorial on how to make a Start Menu for my game, but when I load up the game with my xbox 360 controller there is now way for me to select the buttons that I have made.
How can I make this work?
I have attached the code I've been using. Just attach it to your camera if you want to see how it behaves.
using UnityEngine; using System.Collections;
[AddComponentMenu("ESE Interface/Manu Menu GUI")] public class MainMenuGUI : MonoBehaviour { public GUISkin GuiSkin; private bool isLoading = false; public string nameOfFirstLevel = "level_01"; public string nameOfContinueLevel = "continueLevel"; public string nameOfCreditsLevel = "credits"; public bool showContinueButton = true;
public float nativeVerticalResolution = 1900.0f;
public float nativeHorizontalResolution = 1200.0f;
public float buttonWidth = 512.0f;
public float buttonHeight = 170.0f;
public float buttonGap = 48.0f;
public float buttonPaddingX = 128.0f;
public float buttonPaddingY = 512.0f;
public float exitbuttonSpaceGab = 512.0f;
public Texture2D loadingScreen;
public void OnGUI()
{
if (GuiSkin)
{
GUI.skin = GuiSkin;
}
else
{
Debug.LogError("MainMenuGUI : GUI Skin object missing!");
}
if (!isLoading)
{
Vector3 pos = new Vector3(0.0f, 0.0f, 0.0f);
Vector3 scale = new Vector3(Screen.height / nativeVerticalResolution, Screen.height / nativeVerticalResolution, 1);
GUI.matrix = Matrix4x4.TRS(pos, Quaternion.identity, scale);
float scaledResolutionWidth = nativeVerticalResolution / Screen.height * Screen.width;
float scaledResolutionHeight = nativeHorizontalResolution / Screen.height * Screen.width;
GUI.BeginGroup(new Rect(0, 0, scaledResolutionHeight, nativeVerticalResolution));
{
float left = buttonPaddingX;
float top = buttonPaddingY;
if (showContinueButton)
{
MakeButton(left, top, "Continue", nameOfContinueLevel);
}
top = top + buttonHeight + buttonGap;
MakeButton(left, top, "New Game", nameOfFirstLevel);
top = top + buttonHeight + buttonGap;
MakeButton(left, top, "Load Game", "Level_01");
top = top + buttonHeight + buttonGap;
MakeButton(left, top, "Credits", nameOfCreditsLevel);
top = top + buttonHeight + exitbuttonSpaceGab;
MakeExitButton(left, top);
}
GUI.EndGroup();
}
if (isLoading)
{
GUIStyle backgroundStyle = new GUIStyle();
backgroundStyle.normal.background = loadingScreen;
GUI.Label (new Rect( ( Screen.width - (Screen.height * 2)) * 0.75f, 0, Screen.height * 2, Screen.height), "", backgroundStyle);
}
}
private void MakeExitButton(float left, float top)
{
bool isWebPlayer = (Application.platform == RuntimePlatform.OSXWebPlayer || Application.platform == RuntimePlatform.WindowsWebPlayer);
if (!isWebPlayer)
{
if (GUI.Button(new Rect(left, top, buttonWidth, buttonHeight), "Exit")) Application.Quit();
}
}
private void MakeButton(float left, float top, string name, string levelToLoad)
{
if (GUI.Button(new Rect(left, top, buttonWidth, buttonHeight), name))
{
isLoading = true;
Application.LoadLevel(levelToLoad);
}
}
}
Answer by naruse · Feb 10, 2010 at 10:04 AM
Hi,
To make a control work with a GUI interface is a little more difficult.
What I would do is detect the key presses from your xbox controller and then with GUI.FocusControl focus the GUI controls you have selected.
Hope it helps! ;)
I manage to scroll the buttons with the use of the controller by using GUI.FocusControl but how can I press the buttons?
do you $$anonymous$$d telling me the new script you have? ive also been trying to get this done
I would also like to know, once using GUI.FocusControl has been invoked, how do you actually selected the focused button?