Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by RafiXWPT · Aug 07, 2015 at 07:24 PM · uicanvasrecttransformrecttooltip

GetComponent() Problem with getting value.

Hello. I am working on ToolTip for my inventory system. So far I have most of things, now I am refractoring code and adding additional functionality. Now I wanted to make my ToolTip show on left or right side of mouse based on end of screen (when tooltip is out of canvas simply show it on opposite side). Everyting is fine and I have function to do that BUT! Function that works not on first time! I mean, I have to hover item twice to get proper value of RectTransform (I need it to offset whole panel by RectTransform width) and I don't know why.

This is my function:

 public void ShowToolTip(Vector2 position, int ID)
     {
         Item gatherItem = itemDatabase.Items.Find(item => item.id == ID);
 
         if(!isDragging)
         {
             toolTip.GetComponent<CanvasGroup>().alpha = 1f;
             GatherItemInfo(gatherItem);
             toolTip.transform.SetAsLastSibling();
 
             float rectwidth = toolTip.GetComponent<RectTransform>().rect.width;
             Debug.Log(rectwidth);
             if(position.x + rectwidth >= Screen.width)
             {
                 Debug.Log("On the Left.");
             }
             else
             {
                 Debug.Log("On the Right.");
             }
         }
         else
             return;
     }

GatherItemInfo() method simply fills UI Text inside ToolTip.

And output from Debug.Log:

1st Hover:

  • 0 (initial size of ToolTip without any Text - WHY???)

  • On The Right.

2nd Hover:

  • 216

  • On The Left.

3rd and others:

  • 216

  • On The Left.

Now when I change item:

1st Hover:

  • 216

  • On The Left.

2nd Hover:

  • 180

  • On The Right.

etc.

Whole situation looks like rectwidth have value BEFORE content size fitter calculate it. Someone can help me how to solve that? GatherItemInfo() runs before GetComponent so rect should be already calculated, seems it is not.

My ToolTip is simply Panel with Content Size Fitter + Vertical Layout Group and inside it UI Text.

Comment
Add comment · Show 7
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MalachiteBR · Aug 07, 2015 at 08:22 PM 0
Share

try to comment this part: toolTip.transform.SetAsLastSibling();

$$anonymous$$aybe its doing something weird and changing the value you want to obtain.

avatar image RafiXWPT · Aug 07, 2015 at 08:30 PM 0
Share

Unfortunatelly nothing changed. Still GetComponent gets width before Content Size Fitter will calculate it.

avatar image MalachiteBR · Aug 07, 2015 at 08:39 PM 0
Share

try instantiating the component first, it just a shoot.. var rectTransform = toolTip.GetComponent(); and then use it.

after you do that, try to debug using visual studio so you can watch what is inside the variable rectTransform.

avatar image MalachiteBR · Aug 07, 2015 at 08:41 PM 0
Share

I was wondering now...

$$anonymous$$aybe the canvas didnt show yet... You must find a way to know if the canvas is printed on the screen already before you call the method to show tooltip...

avatar image RafiXWPT · Aug 07, 2015 at 08:51 PM 0
Share

It is printed, I already checked that. Thats why I deleted ".SetActive" and change alpha ins$$anonymous$$d to be sure that rect is already on screen.

Also I checked in debugger as you suggested. As I thought. rectwidth is not calculated inside that method. Proper values can be found after ShowToolTip() ends. Thats why I must hover 2x to get what I want. But now, how I can solve that?

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by RafiXWPT · Aug 08, 2015 at 03:03 AM

Oh My God. I figured out how to solve that. I just have to:

 float textwidth = toolTip.transform.GetChild(0).GetComponent<Text>().preferredWidth;

Instead of:

 float rectwidth = toolTip.GetComponent<RectTransform>().rect.width;

Now I have width just after adding text cus rect is based on that.

Unfortunatelly I don't know why, that doesn't work for height (same situation as before, proper height is calculated after showing tooltip. To solve that I wrote simple calculator:

 GUIStyle height_calculator = new GUIStyle();
 GUIContent toolTipContent = new GUIContent(toolTip.transform.GetChild(0).GetComponent<Text>().text);
         
 float textWidth = toolTip.transform.GetChild(0).GetComponent<Text>().preferredWidth;
 float textHeight = height_calculator.CalcHeight(toolTipContent, textWidth);

Now I have both width and height.

Hope that will help if someone will have similar problem to me....

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

How to get a Rect from Image or RectTranform? 1 Answer

Find UI Image position in Screen Space 1 Answer

UI How to configure Left And Right. 1 Answer

Editing RectTransform scale 2 Answers

Problem copying RectTransform UI elements 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges