Unity-UI-Extensions / com.unity.uiextensions

https://unity-ui-extensions.github.io/
1.2k stars 129 forks source link

BUG: SelectItemIndexOnStart does not work #431

Closed DJoshi009 closed 1 year ago

DJoshi009 commented 1 year ago

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);

    }
}
SimonDarksideJ commented 1 year ago

Thanks for the report, will test asap

SimonDarksideJ commented 1 year ago

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

Tested:

https://user-images.githubusercontent.com/1793042/217838171-b8f3d380-d5ad-4cab-a0db-e5aae8caf56b.mp4

ComboBoxSampleScene.zip

DJoshi009 commented 1 year ago

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

SimonDarksideJ commented 1 year ago

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.

DJoshi009 commented 1 year ago

Thanks that worked...!

SimonDarksideJ commented 1 year ago

Glad you go it working, closing issue.