I have a game that can handle controller inputs but I have no Idea how to making interact with the UI buttons. I also have a container that switches pages on button click but I would like it to switch pages on the controller’s RB and LB buttons too. Buttons are not all in the same container. Everything is kind of messy. How can I make the UI responds to controller inputs ?
An easy way would be to create a singleton to access your UI elements.
Put the following script on a gameObject e.g. Canvas and drag and drop the UI elements into the variables in the inspector.
using UnityEngine;
using UnityEngine.UI;
public class UIManager : MonoBehaviour {
public static UIManager Instance; //A public static reference to itself (make's it visible to other objects without a reference)
public Button myButton;
public Text myText;
void Awake()
{
//Ensure that there is only one object
if (Instance == null)
{
Instance = this;
}
else
{
Destroy(gameObject);
}
}
}
Than you can access it from other compenents via:
UIManager.Instance.myText.text = "Whatever";