How to access dropdown objects childrens?
Hi! I'm currently working on project, where we want to navigate menus with controller. Problem now is that how I can access and select dropdown menus options? So in scene I have dropdown object with 4 options. Current task is to be able to navigate in those options only with controller.
This script is attached to the Canvas. Canvas has canvas reycaster,canvas scaler,canvas and rect transform components.
Problem lies on a line 106.
Any help is appreciated!
using UnityEngine
using System.Collections
using UnityEngine.EventSystems
using System.Collections.Generic
using UnityEngine.SceneManagement
using UnityEngine.UI
using System.Linq
public class uiNavigation : MonoBehaviour {
//List for menu options
public List<GameObject> menuOptions = new List<GameObject> ();
//Private variable for list
private int menuIndex;
private bool canClick;
private bool canSlide;
//Just some basic formating for variables
void Start () {
canClick = true;
canSlide = true;
//Cursor.visible = false;
//Cursor.lockState = CursorLockMode.Locked;
if (EventSystem.current.currentSelectedGameObject == null) {
EventSystem.current.SetSelectedGameObject (menuOptions [menuIndex]);
}
}
void Update () {
int listSize = menuOptions.Count;
//For Vertical navigation in menus
if (Input.GetAxisRaw ("p1 controller dpad Y") == 1) {
//(Input.GetButtonDown("Menu up")
if (canClick == false) {
return;
}
canClick = false;
menuIndex--;
print (menuIndex);
if (menuIndex < 0) {
print ("smaller than 0");
menuIndex = 0;
}
EventSystem.current.SetSelectedGameObject (menuOptions [menuIndex]);
}
if (Input.GetAxisRaw ("p1 controller dpad Y") == -1) {
//(Input.GetButtonDown("Menu down")
if (canClick == false) {
return;
}
canClick = false;
menuIndex++;
if (menuIndex > (listSize - 1)) {
print ("greater than " + (listSize - 1));
menuIndex = listSize - 1;
print ("MenuIndex " + menuIndex);
}
EventSystem.current.SetSelectedGameObject (menuOptions [menuIndex]);
}
if (Input.GetAxisRaw ("p1 controller dpad Y") == 0) {
//(!Input.GetButtonDown("Menu down" || !Input.GetButtonDown("Menu up"))
canClick = true;
}
//What happens on submit button press.
if (Input.GetButtonDown ("Submit")) {
if (menuOptions [menuIndex].GetComponent<Button> ()) {
//Activates button
menuOptions [menuIndex].GetComponent<Button> ().onClick.Invoke ();
} else if (menuOptions [menuIndex].GetComponent<Dropdown> ()) {
//TODO Make this shit work like it shoud be.
menuOptions [menuIndex].GetComponent<Dropdown> ().Show ();
//menuOptions [menuIndex].GetComponent<Dropdown> ().GetComponentInChildren<Dropdown.OptionDataList> ();
//EventSystem.current.SetSelectedGameObject (menuOptions [menuIndex].GetComponent<Dropdown> ().gameObject);
//menuOptions [menuIndex].GetComponent<GameObject> ().SetActive(true);
} else if (menuOptions [menuIndex].GetComponent<Toggle> ()) {
if (menuOptions [menuIndex].GetComponent<Toggle> ().isOn) {
print ("toggle is on");
menuOptions [menuIndex].GetComponent<Toggle> ().isOn = false;
} else {
print ("toggle is off");
menuOptions [menuIndex].GetComponent<Toggle> ().isOn = true;
}
}
}
//For Slider adjustment
if (menuOptions [menuIndex].GetComponent<Slider> ()) {
if (Input.GetAxisRaw ("p1 controller dpad X") == 1) {
if (canSlide == false) {
return;
}
canSlide = false;
menuOptions [menuIndex].GetComponent<Slider> ().value++;
}
if (Input.GetAxisRaw ("p1 controller dpad X") == -1) {
if (canSlide == false) {
return;
}
canSlide = false;
menuOptions [menuIndex].GetComponent<Slider> ().value--;
}
if (Input.GetAxisRaw ("p1 controller dpad X") == 0) {
canSlide = true;
}
}
}
}
Your answer
Follow this Question
Related Questions
Find child coordinates and pass them further? 0 Answers
Writing dropdown selections to file 1 Answer
How to add more UI elements to Dropdown OptionData except default string and sprite values in Unity? 0 Answers
How delete options from the dropdown gui withscript? 0 Answers
Get Collider of Child Object? 1 Answer