C# Unity script to change UI Document

229 Views Asked by At

I made a script but I can't get it to work, I need help please. I want to drag several empty with UI Documents and then extract from them all the buttons that match the labels I manually put in the inspector. The idea is to have 2 lists of buttons to move forward or backward between UIs. I'm in Unity 2022 and UI Toolkit.Thanks.

using UnityEngine;
using UnityEngine.UIElements;
using System.Collections.Generic;

public class CambiarPaginas : MonoBehaviour
{
    public List<GameObject> uiDocuments;
    public List<string> PaginaSiguiente;
    public List<string> PaginaAnterior;


    private int currentUIIndex = 0;
    private List<VisualElement> containers = new List<VisualElement>();
    public Dictionary<string, Button> BotonesPaginaSiguiente;
    public Dictionary<string, Button> BotonesPaginaAnterior;
    private void Start()
    {
        ShowUI(currentUIIndex);

        BotonesPaginaSiguiente = new Dictionary<string, Button>();
        BotonesPaginaAnterior = new Dictionary<string, Button>();

        foreach (var uiDocument in uiDocuments)
        {
            UIDocument uiDocComponent = uiDocument.GetComponent<UIDocument>();
            if (uiDocComponent != null)
            {
                VisualElement container = uiDocComponent.rootVisualElement;
                containers.Add(container);
            }
        }

        foreach (var container in containers)
        {
            List<Button> allButtons = container.Query<Button>().ToList();

            foreach (var button in allButtons)
            {
                button.clicked -= () => ShowNextUIClick(button);
                button.clicked += () => ShowPreviousUIClick(button); // Corrección aquí
            }

            foreach (string buttonName in PaginaSiguiente)
            {
                Button button = container.Q<Button>(buttonName);
                if (button != null)
                {
                    button.clicked += () => ShowNextUIClick(button);
                    BotonesPaginaSiguiente.Add(buttonName, button);
                }
            }

            foreach (string buttonName in PaginaAnterior)
            {
                Button button = container.Q<Button>(buttonName);
                if (button != null)
                {
                    button.clicked += () => ShowPreviousUIClick(button);
                    BotonesPaginaAnterior.Add(buttonName, button);
                }
            }
        }
    }

    public void ShowNextUI()
    {
        currentUIIndex++;
        if (currentUIIndex >= uiDocuments.Count)
        {
            currentUIIndex = 0;
        }
        ShowUI(currentUIIndex);
    }

    public void ShowPreviousUI()
    {
        currentUIIndex--;
        if (currentUIIndex < 0)
        {
            currentUIIndex = uiDocuments.Count - 1;
        }
        ShowUI(currentUIIndex);
    }

    public void ShowUI(int index)
    {
        for (int i = 0; i < uiDocuments.Count; i++)
        {
            if (i == index)
            {
                uiDocuments[i].SetActive(true);
            }
            else
            {
                uiDocuments[i].SetActive(false);
            }
        }
    }



    private void ShowNextUIClick(Button clickedButton)
    {
        string buttonName = clickedButton.name;
        int buttonIndex = PaginaSiguiente.IndexOf(buttonName);
        ShowNextUI();
        Debug.Log("Se hizo clic en el botón: " + buttonName + ", índice: " + buttonIndex);
    }

    private void ShowPreviousUIClick(Button clickedButton)
    {
        string buttonName = clickedButton.name;
        int buttonIndex = PaginaAnterior.IndexOf(buttonName);
        ShowPreviousUI();
        Debug.Log("Se hizo clic en el botón: " + buttonName + ", índice: " + buttonIndex);
    }
}

enter image description here

In the last 2 blocks of code if I withdraw: "ShowPreviousUI(); and ShowNextUI();" correctly logs the Debug.Log, but I can't get it to switch between screens as it should. Patience please, I'm still new to C#.

0

There are 0 best solutions below