x


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);
        }
    }
}
more ▼

asked Feb 08, 2010 at 05:45 PM

Skjalg gravatar image

Skjalg
1.7k 67 63 83

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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! ;)

more ▼

answered Feb 10, 2010 at 10:04 AM

naruse gravatar image

naruse
361 10 9 19

I manage to scroll the buttons with the use of the controller by using GUI.FocusControl but how can I press the buttons?

Feb 13, 2010 at 08:34 PM Skjalg

do you mind telling me the new script you have? ive also been trying to get this done

Sep 25, 2010 at 01:51 AM name111

I would also like to know, once using GUI.FocusControl has been invoked, how do you actually selected the focused button?

Jul 05, 2011 at 02:45 AM Majmuns
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x8981
x5743
x159
x89
x74

asked: Feb 08, 2010 at 05:45 PM

Seen: 4244 times

Last Updated: Jul 05, 2011 at 02:45 AM