0
\$\begingroup\$

I've recently come across a problem with the UI DropDown component. I would like to resize other UI components depending on whether the dropdown is opened or closed. Unfortunately, the dropdown dynamically creates and destroys its own "Dropdown List" component, making it difficult to check whether it is open or not, without having to check with Update or FixedUpdate (both being expensive on the performance).

I have tried to implement a solution with event handlers, but it seems as if the even handler triggers before the "Dropdown List" is created. Thus this does not work.

Am I simply missing an easy way to check whether the dropdown is open or closed?

\$\endgroup\$
0
\$\begingroup\$

I have managed to find an surprisingly easy way to solve this issue. The "Dropdown List" that is dynamically created, is created from the "Template" object in the dropdown object's hierarchy. I added a small script to that template object, with a public GameObject variable pointing to my main script component as well as a a public GameObject pointing to itself. When the script is started, it checks its own object name, and if that is "Dropdown List", it sets a bool flag in my main script. When it is destroyed, it does the same. See the code in the script below:

using UnityEngine;

public class DropdownOpenScript : Monobehaviour
{
    // Public variables
    public GameObject self;
    public GameObject controller;      //This is the object containing my controller script

    // Start is called before the first frame update
    void Start()
    {
        if (self.name == "Dropdown List")
        {
            //ControllScript is my main script controlling my program opperation.
            //It contains a public boolean bDropdownOpen to indicate whether the
            //dropdown list is open or not
            controller.GetComponent<ControllScript>().bDropdownOpen = true;
        }
    }

    private void OnDestroy()
    {
        if (self.name == "Dropdown List")
        {
            controller.GetComponent<ControllScript>().bDropdownOpen = false;
        }
    }
}
```
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.