Unity-UI-Extensions/com.unity.uiextensions

BUG: SelectItemIndexOnStart does not work

Closed this issue · 6 comments

Unity UI Extensions Bug Report

Describe the bug

I have tested SelectFirstItemOnStart while populating list in IEnumerator method and it does not work, I have tested same using example scene too. I guess the properties initialize before we fetch data from API

To Reproduce

Get data from API using Json Utility create a list and set into SetAvailableOptions to populate data in inspector. By doing this SelectItemIndexOnStart does not work even SelectFirstItemOnStart does not work.

My code

[System.Serializable]
public class AllUsersClass
{
public int user_id;
public string employeeName;
}

[System.Serializable]
public class AllUserList
{
    public AllUsersClass[] allUsers;
}
public AllUserList allUsersList = new AllUserList();


IEnumerator GetAllUsers()
{
    UnityWebRequest webRequest = UnityWebRequest.Get(baseUrl + "Users");
    yield return webRequest.SendWebRequest();

    if (webRequest.result == UnityWebRequest.Result.ConnectionError)
    {
        Debug.Log("Error While Sending: " + webRequest.error);
    }
    else
    {
        allUsersList = JsonUtility.FromJson<AllUserList>("{\"allUsers\":" + webRequest.downloadHandler.text + "}");
        List<string> employeeName = new List<string>();
        for (int i = 0; i < allUsersList.allUsers.Length; i++)
        {
            employeeName.Add(allUsersList.allUsers[i].employeeName);
        }
        acb.SetAvailableOptions(employeeName);

    }
}

Thanks for the report, will test asap

OK, tested and here's a sample to hopefully help you along

Tested:

  • Setting a default option
  • toggling disabled on/off
2023-02-09_14-17-59.mp4

ComboBoxSampleScene.zip

Thank but before I test your implementation, I encountered new issue while instantiating AutocompleteCombobox. I have to generate these dinamically at runtime but when it instantiate it throws null exceptions. I have tried initialized() after instantiate but errors are not resolving. Here is my code and errors.

Code

teamList = JsonUtility.FromJson<TeamList>(webRequest.downloadHandler.text);
            for (int i = 0; i < teamList.teams.Count; i++)
            {
                GameObject tableRow = Instantiate(teamRowPrefab);
                
                tableRow.transform.SetParent(teamTableContainer);
                tableRow.transform.Find("AutoCompleteComboBox_User").GetComponent<AutoCompleteComboBox>().Awake();
                tableRow.transform.Find("AutoCompleteComboBox_Role").GetComponent<AutoCompleteComboBox>().Awake();

            }

Image

error

I was about to dig in but I see an issue on line 4, you are not setting a parent for the Prefab when instantiating it, All UI Objects need to be instantiated as a child of a canvas.

Change the following line:

    GameObject tableRow = Instantiate(teamRowPrefab);

To something like:

    Canvas myCanvas; // <- Get or assign the canvas in the scene.
    GameObject tableRow = Instantiate(teamRowPrefab, myCanvas);

This will instantiate the control on a canvas.

Hope that helps.

Thanks that worked...!

Glad you go it working, closing issue.