Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Yet another GetPreferredValues problem

Discussion in 'UGUI & TextMesh Pro' started by drHogan, Jun 28, 2022.

  1. drHogan

    drHogan

    Joined:
    Sep 26, 2012
    Posts:
    201
    Hi,

    I think I have been reading every single post related to preferred width and height, but despite trying every solution, I still have some issues with making a dynamically resized tooltip. (I would like to avoid using coroutines to delay the resizing of the container).

    Code (CSharp):
    1.         public void SetTextOnlyTooltip(string textToDisplay)
    2.         {
    3.             _tooltipText.text = textToDisplay;
    4.  
    5.             _tooltipText.ForceMeshUpdate();
    6.  
    7.             Vector2 preferredSize = _tooltipText.GetPreferredValues();
    8.  
    9.             //this one gets added only in case of multiline tooltip
    10.             float multiLineTooltipAddedBorder = 0f;
    11.  
    12.             Debug.Log(preferredSize.x + "/" + preferredSize.y);
    13.  
    14.             if (preferredSize.x > _maxWidth)
    15.             {            
    16.                 preferredSize.y = Mathf.FloorToInt((preferredSize.x / _maxWidth)) * _lineHeigth;
    17.                 preferredSize.x = _maxWidth;
    18.                 multiLineTooltipAddedBorder = 10f;
    19.             }
    20.  
    21.             _tooltipHeight = preferredSize.y + _verticalBorder * 2 + multiLineTooltipAddedBorder;
    22.             _tooltipWidth = preferredSize.x + _horizontalBorder * 2 + multiLineTooltipAddedBorder;
    23.  
    24.             _tooltipRectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, _tooltipWidth);
    25.             _tooltipRectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, _tooltipHeight);        
    26.         }
    This in theory should resize the tooltip container according to the text length, it's got a max width in order to create a multilines text area if too long.

    The problem is, that if the tooltip hovers on an element that generates a shorter text, and moves onto an element with a longer text generated, the second one is seen as multiline, and the tooltip is double height.

    Here's an example. I hover a short one, and the tooltip size is correct. GetPreferredValues() = 149,09;13,19

    Screenshot_40.jpg
    Then I hover a slightly longer one (still shorter than the max width that would require two lines) and I get GetPreferredValues() = 183,07;27,84
    Screenshot_42.jpg

    If I then leave and return on the last one, at that point the size is correct
    Screenshot_43.jpg

    I understand that it's still using the size of the previous text to determine the values somehow, but I expected, from a couple of posts, that this could fix it

    Code (CSharp):
    1. _tooltipText.ForceMeshUpdate();
    Is there any way to fix this (possibly without using a coroutine)?

    Cheers and thanks,
    H
     
    Last edited: Jun 28, 2022
  2. drHogan

    drHogan

    Joined:
    Sep 26, 2012
    Posts:
    201
    Following a suggestion I tried the following chance, i.e. taking the renderer bounds instead than the GetPreferredValues() but while in a different way, it still doesn't work

    Code (CSharp):
    1. var bounds = _tooltipText.bounds;
    2. Vector2 preferredSize = new Vector2(bounds.size.x, bounds.size.y);


    And now I always get it into 2 lines (even though the max width i fixed is far larger than that)
     
  3. drHogan

    drHogan

    Joined:
    Sep 26, 2012
    Posts:
    201
    Ok, looks like the fix (not sure if the ideal way but it works) was to set at the beginning the width of the text to my max width, and then it works correctly.

    Code (CSharp):
    1.         public void SetTextOnlyTooltip(string textToDisplay)
    2.         {
    3.             _tooltipText.text = textToDisplay;
    4.  
    5.             _tooltipRectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, _maxWidth);
    6.  
    7.             Vector2 preferredSize = _tooltipText.GetPreferredValues();
    8.  
    9.             //this one gets added only in case of multiline tooltip
    10.             float multiLineTooltipAddedBorder = 0f;
    11.  
    12.             if (preferredSize.x > _maxWidth)
    13.             {
    14.                 preferredSize.y = Mathf.FloorToInt((preferredSize.x / _maxWidth)) * _lineHeigth;
    15.                 preferredSize.x = _maxWidth;
    16.                 multiLineTooltipAddedBorder = 10f;
    17.             }
    18.  
    19.             _tooltipHeight = preferredSize.y + _verticalBorder * 2 + multiLineTooltipAddedBorder;
    20.             _tooltipWidth = preferredSize.x + _horizontalBorder * 2 + multiLineTooltipAddedBorder;
    21.  
    22.             _tooltipRectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, _tooltipWidth);
    23.             _tooltipRectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, _tooltipHeight);
    24.         }