Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question Hello! I'm new. How to change the Font Asset from a Dropdown List item?

Discussion in 'UGUI & TextMesh Pro' started by Mushedar, May 8, 2023.

  1. Mushedar


    May 8, 2023
    So.. I started coding for a week now, learning little by little.
    I don't know if this is possible, but I need to change the fonts individually, from the dropdown list. I understand that you can assign the same font to all items, but it is not possible to assign a specific font to each item.

    The only way I found to be able to change the font is in play mode.
    When I click the dropdown, a dropdown list object appears, and inside has all the items (fonts), then when I click out or select a font the dropdown list object in the hierarchy dissapears.
    In simple words, I want to be able to show the font style when you open the dropdown, so the user doesn't have to click one by one.

    Please see attached file! Sorry for my english!

    Thank you!

    PS: YES I know! I'm making a text editor on a game engine :)

    Attached Files:

  2. karliss_coldwild


    Oct 1, 2020
    I don't think there is a clean way for doing this using unity builtin dropdown but something like this should work:

    1) inherit from the Dropdown (or TMP_Dropdown whichever you use) component,
    2) hook in your code which replaces fonts after dropdown.Shown has created all the items
    3) iterate over all the child elements of Dropdown Content

    To do this you will require at least following programming skills:
    * knowing how to work with loops and various list types
    * basic understanding of inheritance
    * navigating and manipulating Unity object hierachy using code

    I am slightly worried about "started coding for a week now". If you meant learning to code in general only for a week, this might be quite challenging. If you were referring to Unity or your current project and you have other programming experience before, then it's probably fine.

    Step 2 would be easier if show was a virtual method, but unfortunately it isn't. It's called only from two places
    OnPointerClick and OnSubmit both of which are virtual, so you can override those instead. Show could also be called from your code, but that can be easily avoided.