Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Horizontal dropdown UI menu

Discussion in 'Scripting' started by Thibault-Potier, Mar 8, 2019.

  1. Thibault-Potier

    Thibault-Potier

    Joined:
    Apr 10, 2015
    Posts:
    206
    Hi,
    I have been struggling the whole morning. Does someone know how to use the ui dropdown in a horizontal mode ?
    I played with the anchor of the template and could make the child list appears on the side of my dropdown, but it's still a vertical list.

    I want item to appears right to left , at the left of my dropdown.

    As it's very common in UI design, i assume there is a built in opion to achieve this. I don't want to code my own solution for a dropdown if there is an existing tool. But right now it's driving me mad.


    free image host


    Thanks for the help !
     
  2. Madgvox

    Madgvox

    Joined:
    Apr 13, 2014
    Posts:
    1,315
  3. Thibault-Potier

    Thibault-Potier

    Joined:
    Apr 10, 2015
    Posts:
    206
    So i started to code my own solution. Better do it myself i guess ^^

    With a simple setup and a few line of code i have this :

    https://gph.is/g/Z28k18E

    Of course i would like it to work in the "reverse" direction
    Any idea on how to work this around ?

    Code (CSharp):
    1. public class CustomDropdown : MonoBehaviour,IPointerClickHandler
    2. {
    3.     public RectTransform container;
    4.     public bool isOpen;
    5.  
    6.     public void OnPointerClick(PointerEventData eventData)
    7.     {
    8.         isOpen = !isOpen;
    9.     }
    10.  
    11.  
    12.     // Start is called before the first frame update
    13.     void Start()
    14.     {
    15.         isOpen = false;
    16.     }
    17.  
    18.     // Update is called once per frame
    19.     void Update()
    20.     {
    21.         if (isOpen)
    22.         {
    23.             Vector3 scale = container.localScale;
    24.             scale.x = Mathf.Lerp(scale.x, 1, Time.deltaTime * 12);
    25.             container.localScale = scale;
    26.         }
    27.         else
    28.         {
    29.             Vector3 scale = container.localScale;
    30.             scale.x = Mathf.Lerp(scale.x, 0, Time.deltaTime * 12);
    31.             container.localScale = scale;
    32.         }
    33.     }
    34. }
     
  4. Thibault-Potier

    Thibault-Potier

    Joined:
    Apr 10, 2015
    Posts:
    206
    So i would like to lerp the position of my container at the same time that i lerp it scale. (Maybe this will look good enough).

    But when i try to access the rect transform value i got some strange value

    Code (CSharp):
    1. Debug.Log(container.rect.width+"   "+container.rect);
    The Rect transform of my container :


    The printed value :


    Witchcraft ? dark magic ? :(
     
  5. patSilva

    patSilva

    Joined:
    Mar 25, 2019
    Posts:
    27
    I know this is somewhat old but this printed log doesn't look wrong?

    On the inspector your seeing it's anchored values and in the log you are seeing the rectangle that would box your RectTransform, essentially. Try setting you anchor to the center and see if the rect better matches the values in the log. X and Y might be off if it is a child of something but I could be wrong.

    just curious, what were you expecting it to be?