Scripting for smooth spread/fold dropdown onGUI

Hello,

I have been experimenting with different codes and I wanted to make an onGUI dropdown menu that can spread/fold smoothly based on deltaTime.

But the code doesn’t seem to work. It did occur to me that onGUI is updated every frame so the idea of using deltaTime may not be valid. I am very new to scripting. How can I make this work? I would like to keep using onGUI if possible.

if (GUI.Button(new Rect(Screen.width - (Screen.width / 8), Screen.height / 40, Screen.width / 10, Screen.height / 10), "Menu"))
            {
                if (show == false)
                    show = true;

                else
                    show = false;  
            }

            if (show)
            {
                    Vector2 scale = scrollViewVector;
                    scale.y = Mathf.Lerp(scale.y, show ? 1 : 0, Time.deltaTime * 12);
                    scrollViewVector = scale;
                scrollViewVector = GUI.BeginScrollView(new Rect(Screen.width - (Screen.width / 8), Screen.height / 8, Screen.width / 10, (Screen.height / 10 + 25) * list.Length), scrollViewVector, new Rect(Screen.width - (Screen.width / 8), Screen.height / 40, Screen.width / 10, Mathf.Max(Screen.height / 7, (Screen.height / 10 + 25) * list.Length)));
                GUI.Box(new Rect(Screen.width - (Screen.width / 8), Screen.height / 40, Screen.width / 10, Mathf.Max((Screen.height / 10 + 25) * list.Length)), "");
                for (int index = 0; index < list.Length; index++)
                {
                    if (GUI.Button(new Rect(Screen.width - (Screen.width / 8), (Screen.height / 40 + (Screen.height / 9 * index + 15)), Screen.width / 10, Screen.height / 10), ""))
                    {
                        show = false;
                        indexNumber = index;
                    }
                    GUI.Label(new Rect(Screen.width - (Screen.width / 8) + 10, (Screen.height / 40 + (Screen.height / 9 * index + 15)), Screen.width / 10, Screen.height / 10), list[index], DropDownMenuFont);
                }
                GUI.EndScrollView();
            }
        }

Again for record keeping,
I’ve solved the problem.
At the end, the solution is always pretty obvious.
I have had some helps from the internet but basically you are animating the onGUI.

if (GUI.Button(new Rect(Screen.width - (Screen.width / 8), Screen.height / 40, Screen.width / 10, Screen.height / 10), "Menu"))
            {
                if (show == false)
                    show = true;

                else
                    show = false; 
            }

            scale.y = Mathf.Lerp(scale.y, show ? 1 : 0, Time.deltaTime * 12);

            if (show)
            {           
                 scrollViewVector = GUI.BeginScrollView(new Rect(Screen.width - (Screen.width / 8), Screen.height / 8, Screen.width / 10, (Screen.height / 10 + 25) * list.Length), scrollViewVector, new Rect(Screen.width - (Screen.width / 8), Screen.height / 40, Screen.width / 10, Mathf.Max(Screen.height / 7, (Screen.height / 10 + 25) * list.Length)));
                GUI.Box(new Rect(Screen.width - (Screen.width / 8), Screen.height / 40, Screen.width / 10, Mathf.Max((Screen.height / 10 + 25) * list.Length)), "");
                for (int index = 0; index < list.Length; index++)
                {
                    if (GUI.Button(new Rect(Screen.width - (Screen.width / 8), (Screen.height / 40 + (Screen.height / 9 * index + 15)), Screen.width / 10, Screen.height / 10 * scale.y), ""))
                    {
                        show = false;
                        indexNumber = index;
                    }
                    GUI.Label(new Rect(Screen.width - (Screen.width / 8) + 10, (Screen.height / 40 + (Screen.height / 9 * index + 15)), Screen.width / 10, Screen.height/10*scale.y), list[index], DropDownMenuFont);

}


GUI.EndScrollView();

}

So instead of tempering with the ScrollView, we can directly change how the boxes that are made inside behave.
Time.deltaTime*12 seemed like a reasonable spread/fold speed.

I would say that one downside to this method is that when you fold (when menu button is pressed), the scroll view collapses right away so you don’t actually see the smooth fold motion of the boxes inside. But it definitely works for opening.