Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

[C#] Xbox D'Pad to cycle through GUI.Buttons?

Discussion in 'Scripting' started by jeThomas, Sep 1, 2014.

  1. jeThomas

    jeThomas

    Joined:
    Nov 13, 2013
    Posts:
    6
    I'm trying to create a script where you can use the XBox Controller D'Pad (Up and Down I would suppose>7th Axis) to cycle through my OnGUI Buttons that I have, basically (Single Player, Multi-Player, Options, etc.)

    I have my input manager set up in unity where as it's "DPadVertical" if this helps you to use in the example code.
     
  2. Polymorphik

    Polymorphik

    Joined:
    Jul 25, 2014
    Posts:
    599
    You need to keep an array of your buttons and as you cycle through move the index over.

    Button buttons = new Buttons[size]
    Button currentButton;
    int currentIndex;

    void Start() {
    this.curretIndex = 0;
    this.currentButton = this.buttons[this.currentIndex]
    }

    void MoveUp() {
    if((this.currentIndex - 1) > -1) {
    this.currentIndex--;
    } else {
    this.currentIndex = size - 1;
    }
    this.currentButton = this.buttons[this.currentIndex];
    }

    Do the reverse for going down. This is assuming the first button in your array is the first option in your menu list. I wrote this up on my phone sorry for the S*** format.
     
  3. jeThomas

    jeThomas

    Joined:
    Nov 13, 2013
    Posts:
    6
    Thanks for your reply! I'm a bit confused at the starting point of your structure.

    Button buttons = new Buttons[size]
    Button currentButton;
    int currentIndex;

    Does that go in the array?
     
  4. Polymorphik

    Polymorphik

    Joined:
    Jul 25, 2014
    Posts:
    599
    Its abstract, essentially "Buttons" is whatever your Menu Grid is made up of. If you are using Unity's OnGUI then you need to make an array of GUI.Button. If you are using NGUI, then UIButton is what you are looking for. Size is the amount of buttons you have.
     
  5. jeThomas

    jeThomas

    Joined:
    Nov 13, 2013
    Posts:
    6
    Alright! Thanks, I'll try that. I really appreciate your time!