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

Please make TMP_DropdownItem public

Discussion in 'UGUI & TextMesh Pro' started by pushxtonotdie, Jul 31, 2018.

  1. pushxtonotdie

    pushxtonotdie

    Joined:
    Oct 21, 2010
    Posts:
    111
    My feedback:

    https://feedback.unity3d.com/suggestions/make-tmp-dropdownitem-public

    In TextMesh Pro the dropdown is pretty good, but sometimes I need to pass more information to it. Before Unity purchased TMP and put it in PackMan I made the dropdown item public instead of protected. I could GetComponent() it and get the OptionData to further customize the dropdown. I would also extend OptionData and decorate it with more information. When migrating to the latest version of unity I've had to resort to a gross hack to get this information.

    It would be nice if TMP_DropdownItem was public so other scripts could gain access to it to further customize the UI.
     
  2. Stephan_B

    Stephan_B

    Unity Technologies

    Joined:
    Feb 26, 2017
    Posts:
    6,588
    One of the nice things about TMP being available via the package manager is that it once again is provided with source code. As such, you can go to the cache location of the TMP package and make those modifications as needed.

    In terms of the suggestion, I have take a note of it.

    If you do make those changes on your end, please post them here if you believe it will benefit other users.
     
  3. pushxtonotdie

    pushxtonotdie

    Joined:
    Oct 21, 2010
    Posts:
    111
    Thanks for your reply, Stephan.

    So is it possible to 'override' the TMP_DropdownItem in the package manager? What I mean is that making those modifications locally doesn't help anyone else on the project. I could move the code back into the project, but then I'm essentially forking the code. I'm trying to avoid this, since I'd like to use packman to manage my upgrades and dependencies. Is there a way to tell packman to essentially 'patch' this file?
     
  4. pushxtonotdie

    pushxtonotdie

    Joined:
    Oct 21, 2010
    Posts:
    111
    Here is a patch of the change for anyone who might want it. I thought it was actually just making it public but I am actually setting the OptionData on the TMPDropdownItem so that I can get access to it later via .data. Example usage:

    Code (CSharp):
    1.       TMP_Dropdown.DropdownItem dropdownItem = GetComponent<TMP_Dropdown.DropdownItem>();
    2.       data = (MyOptionData)dropdownItem.data;
    3. // do custom stuff with the data
    4.  
    Its possible I'm the only one who wants to do custom stuff in dropdowns....or maybe not?

    Code (CSharp):
    1. --- /Users/chris/Library/Unity/cache/packages/packages.unity.com/com.unity.textmeshpro@1.2.4/Scripts/Runtime/TMP_Dropdown.cs    2018-07-31 15:14:57.000000000 -0700
    2. +++ /Users/chris/tmp/TMP_Dropdown.cs    2018-07-31 15:19:23.000000000 -0700
    3. @@ -13,7 +13,7 @@
    4.      [RequireComponent(typeof(RectTransform))]
    5.      public class TMP_Dropdown : Selectable, IPointerClickHandler, ISubmitHandler, ICancelHandler
    6.      {
    7. -        public class DropdownItem : MonoBehaviour, IPointerEnterHandler, ICancelHandler
    8. +        protected internal class DropdownItem : MonoBehaviour, IPointerEnterHandler, ICancelHandler
    9.          {
    10.              [SerializeField]
    11.              private TMP_Text m_Text;
    12. @@ -23,11 +23,7 @@
    13.              private RectTransform m_RectTransform;
    14.              [SerializeField]
    15.              private Toggle m_Toggle;
    16. -            private OptionData m_data;
    17. -          
    18. -
    19. -            public OptionData data { get { return m_data; } set { m_data = value; } }
    20.              public TMP_Text text { get { return m_Text; } set { m_Text = value; } }
    21.              public Image image { get { return m_Image; } set { m_Image = value; } }
    22.              public RectTransform rectTransform { get { return m_RectTransform; } set { m_RectTransform = value; } }
    23. @@ -548,7 +544,6 @@
    24.          {
    25.              // Add a new item to the dropdown.
    26.              DropdownItem item = CreateItem(itemTemplate);
    27. -            item.data = data;
    28.              item.rectTransform.SetParent(itemTemplate.rectTransform.parent, false);
    29.              item.gameObject.SetActive(true);
    30.  
     
  5. ImperativeGames

    ImperativeGames

    Joined:
    Dec 11, 2016
    Posts:
    34
    Nope, you are not the only one. I also needed Dropdown sending OnValueChanged if the same item was chosen (as sometimes it's the only value/option on my InventoryItem, called "Use item" ^^).
    Had to write VERY strange TMP_Dropdown_Extended: TMP_Dropdown.