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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

UI Resize or Position Change Issues

Discussion in 'Scripting' started by kensarto17, Feb 19, 2020.

  1. kensarto17

    kensarto17

    Joined:
    Nov 11, 2017
    Posts:
    27
    I am using a method that is called when entering certain ui elements with event triggers. The issue i was having was that when the ui elements were near the border, they would flow over. I fixed that issue in a sense that it does work. The problem is that it will only correctly reposition the tooltipPanel every second time the same method with the same arguments are provided i.e. if i hover over the health bar, then the stamina bar and the stamina bar is wider than the health bar, the tooltip for the stamina bar will still overflow, but if i do the stamina bar a second time, it will position itself correct. Upon going back to the health bar it is now offset by a large margin like the health bar was.

    Below is the code used for determining position of the panel, i am unsure of why it is doing this.

    Code (CSharp):
    1. Vector3 pos = Input.mousePosition;
    2.        
    3.         corners = new Vector3[4];
    4.         tooltipPanel.GetComponent<RectTransform>().GetWorldCorners(corners);
    5.         float width = corners[2].x - corners[0].x;
    6.         float height = corners[1].y - corners[0].y;
    7.  
    8.         float distPastX = pos.x + width - Screen.width;
    9.         if (distPastX > 0)
    10.             pos = new Vector3(pos.x - distPastX, pos.y, pos.z);
    11.         float distPastY = pos.y - height;
    12.         if (distPastY < 0)
    13.             pos = new Vector3(pos.x, pos.y - distPastY, pos.z);
    14.  
    15.         tooltipPanel.transform.position = pos;
     
  2. unit_dev123

    unit_dev123

    Joined:
    Feb 10, 2020
    Posts:
    989
    looks like delay trigger issue if only work on twice so check if func is working on first
     
  3. kensarto17

    kensarto17

    Joined:
    Nov 11, 2017
    Posts:
    27
    I know the function is definitely working on first hover-over the ui element because the tooltip correctly changes the text of the tooltip to the related UI element (part of the same method, just wasnt included because its not relevant to the part presenting an issue)
    I will attach the full method for clarity and can confirm the text works just fine.

    Code (CSharp):
    1. public void DisplayTooltipEnergy(string type)
    2.     {
    3.         tooltipPanel.SetActive(true);
    4.  
    5.         switch (type)
    6.         {
    7.             case "Stamina":
    8.                 tooltipText.text = "Stamina\nA Measure of your physical endurance.\n\nStamina Regen: " + CharacterManager.Instance.staminaRegen + "\nStamina Degen: " + CharacterManager.Instance.staminaDegen + "\nTotal: " + (CharacterManager.Instance.staminaRegen - CharacterManager.Instance.staminaDegen);
    9.                 break;
    10.             case "Chakra":
    11.                 tooltipText.text = "Chakra\nA Measure of your spiritual power.\nDue to your current level of control, your current chakra drains at a rate of: " + 10 + "%\n\nChakra Regen: " + CharacterManager.Instance.chakraRegen + "\nChakra Degen: " + CharacterManager.Instance.chakraDegen + "\nTotal: " + (CharacterManager.Instance.chakraRegen - CharacterManager.Instance.chakraDegen);
    12.                 break;
    13.             case "Health":
    14.                 tooltipText.text = "Stamina\nA Measure of your vital essence.\n\nHealth Regen: " + CharacterManager.Instance.healthRegen + "\nHealth Degen: " + CharacterManager.Instance.healthDegen + "\nTotal: " + (CharacterManager.Instance.healthRegen - CharacterManager.Instance.healthDegen);
    15.                 break;
    16.         }
    17.  
    18.         Vector3 pos = Input.mousePosition;
    19.        
    20.         corners = new Vector3[4];
    21.         tooltipPanel.GetComponent<RectTransform>().GetWorldCorners(corners);
    22.         float width = corners[2].x - corners[0].x;
    23.         float height = corners[1].y - corners[0].y;
    24.  
    25.         float distPastX = pos.x + width - Screen.width;
    26.         if (distPastX > 0)
    27.             pos = new Vector3(pos.x - distPastX, pos.y, pos.z);
    28.         float distPastY = pos.y - height;
    29.         if (distPastY < 0)
    30.             pos = new Vector3(pos.x, pos.y - distPastY, pos.z);
    31.  
    32.         tooltipPanel.transform.position = pos;
    33.     }
     
  4. unit_dev123

    unit_dev123

    Joined:
    Feb 10, 2020
    Posts:
    989
    not sure but your description of problem tells it work second time, all the time, just not first.

    So something must be wrong with first?

    try to print out of values in func to check maybe.
     
  5. kensarto17

    kensarto17

    Joined:
    Nov 11, 2017
    Posts:
    27
    In theory yes, but that's what I can't work out. You trigger the pointerEntry, it calls this method. then you exit and it calls the method that hides the tooltip (sets the panel.active to false and nothing else), then you enter again and it calls the exact same method.

    I'm thinking its a logic error or potentially a editor error, but i really dont know.
    Edit: by editor error i mean a user error within the editor
     
  6. unit_dev123

    unit_dev123

    Joined:
    Feb 10, 2020
    Posts:
    989
    try to print out numerical value using println() it is other way than using debugger tool to trouble shoot, sorry i at school now.

    so print(width), print(height), print(distPastX) ect.
     
  7. kensarto17

    kensarto17

    Joined:
    Nov 11, 2017
    Posts:
    27
    As per your suggestion I performed a test of values by using Debug.Log for the height of the panel, the width of the panel, and the position of the panel (x and y).
    The test involved hovering over my health bar twice, followed by my chakra bar twice.

    upload_2020-2-19_18-11-31.png

    Based on the above values, I believe the issue is that it is not correctly reading the width and height of the panel at the time of calculation, and these wrong values lead to it being placed wrong. However, due to the text being changed first, the width and height of the panel should have already changed. The change in panel size is correctly shown in the game itself.

    Note that the width and height is the same on the second and third experiments, this means that despite hovering over the health bar at first and the chakra bar second, it returns the width/height of the tooltipPanel as if it were the health bar tooltip twice.

    What can i do to ensure that it reads the correct width and height of the tooltipPanel for the calculation
     

    Attached Files:

  8. unit_dev123

    unit_dev123

    Joined:
    Feb 10, 2020
    Posts:
    989
    are u sure changing text first auto changes width and height of panel?
     
  9. kensarto17

    kensarto17

    Joined:
    Nov 11, 2017
    Posts:
    27
    im not sure the order in which everything occurs within unity. but i know for a fact that changing the text will change the height and width of the panel.

    I was using the text changing along with content size fitters / layout groups for about a week before tackling the screen border overflow issue, and the panel was resizing just fine that entire time.

    Additionally the panel does definitely resize in the editor to the correct fit.
     
  10. unit_dev123

    unit_dev123

    Joined:
    Feb 10, 2020
    Posts:
    989
    so what you think the problem is?
     
  11. kensarto17

    kensarto17

    Joined:
    Nov 11, 2017
    Posts:
    27
    Well, this thread was me asking others for their input on what is causing the issue so I'm not certain. But potentially the order in which unity performs actions i.e. update > lateupdate > gui changes etc etc might be getting in the road of this calculation because its not changing the size of the panel first. So that would be my guess.

    But I lack the knowledge on the inner workings of the engine to make any concrete statement.
     
  12. unit_dev123

    unit_dev123

    Joined:
    Feb 10, 2020
    Posts:
    989
    or maybe bug in your code somewhere too, worth investigating i think
     
  13. kensarto17

    kensarto17

    Joined:
    Nov 11, 2017
    Posts:
    27
    the only code related to this function is the code i have currently posted. if you can spot a logic error please do let me know, but I've been over it during my time before posting this thread and each line makes sense currently. As far as i understand it anyway, maybe im missing some vital knowledge about how something works, but then if thats the case no amount of just looking will help.
     
  14. unit_dev123

    unit_dev123

    Joined:
    Feb 10, 2020
    Posts:
    989
    ok sir sometimes we jump to conclusion it is software bug but many time it is error on our part, i try reducing complexity first with hardcoded strings and many println() but i at school now so sorry :(
     
  15. kensarto17

    kensarto17

    Joined:
    Nov 11, 2017
    Posts:
    27
    No need to apologise, thank you for the input thus far, and hopefully I can get some further assistance down the line from others on the forums.
     
  16. kensarto17

    kensarto17

    Joined:
    Nov 11, 2017
    Posts:
    27
    After much searching due to not really knowing what to look for i found the solution.
    immediately after changing the content of the panel I used the following:
    Code (CSharp):
    1. LayoutRebuilder.ForceRebuildLayoutImmediate(rectTransformOfTheParentLayoutGroup);
    this immediately reset the width and height accordingly and allowed me to access the correct values for the following calculation.
     
  17. unit_dev123

    unit_dev123

    Joined:
    Feb 10, 2020
    Posts:
    989
    happy you solve!