- Home /
[C#] Xbox D'Pad to cycle through GUI.Buttons?
I'm trying to use the XBox D'Pad Controller to cycle through GUI.Buttons that I have on my Main Menu Scene through C#.
I'm pretty sure it would be something like, if(Input.GetAxis("DPadVertical")) But then what comes next I have no idea!! Please help. Like how to make GUI.Buttons noticed.
Answer by codezeero · Sep 17, 2014 at 01:21 PM
public enum Dpad{None,Right,Left,Up,Down} private bool flag = true; private Dpad control = Dpad.None;
void Update(){
PadControl();
}
// little tricky to use Dpad Axis as a button
void PadControl(){
if(Input.GetAxis("DpadX")==0.0){
control= Dpad.None;
flag = true;
}
if(Input.GetAxis("DpadX")==1f && flag ){
StartCoroutine( "DpadControl",Dpad.Right);
}
if(Input.GetAxis("DpadX")==-1f && flag){
StartCoroutine( "DpadControl",Dpad.Left);
}
if(Input.GetAxis("DpadY")==1f&& flag ){
StartCoroutine( "DpadControl",Dpad.Up);
}
if(Input.GetAxis("DpadY")==-1f && flag ){
StartCoroutine( "DpadControl",Dpad.Down);
}
}
// your methods can go nice and easy here !
IEnumerator DpadControl(Dpad value){
flag = false;
yield return new WaitForSeconds(0.15f); // delay it as you wish
if(value==Dpad.Right) yourfunction(); //** go right
if(value==Dpad.Left) yourfunction(); //** go left
if(value==Dpad.Up) yourfunction(); //** go up
if(value == Dpad.Down) yourfunction(); //** go down
StopCoroutine ("DpadControl");
}
Answer by screenname_taken · Sep 17, 2014 at 12:57 PM
The way i'd do it, is have an array, that has a reference to the GUIButtons. Then when i'd press up or down, i'd cycle through the array, up or down. You see, you won't be pointing directly at the button, but rather a position that corresponds to that. And have a function in the button that would enlarge it if it's own position in that array is selected. (If the integer that the gamepad is changing is the same number as a position in that array.)