• 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 demented_hedgehog · Mar 03, 2015 at 03:47 AM · uisizetooltip

How do I get the size of an inactive panel in the new UI?

I want to make my own tooltips (the new unity UI has some builtin .. but they only take richtext by the looks of it.. I want embedded images etc). I'd also like to use the new unity UI as it should be more more efficient and I believe it's the "right" way to do this (not the legacy GUI).

The base for my tooltip is a UI panel with a Content Size Fitter. I want to move the panel with the mouse so that it never extends beyond the screen bounds. Some time after the user mouses over a collider the tooltip panel is activated and then displayed. Because the content of the tooltip is dynamic I suspect the size of the tooltip panel is not known till rendering time? Tooltip placement is done something like the approach described here here but I have to check if the tooltip +/- half the tooltip panel width is out of the game windows bounds (< 0, > Screen.width and vertically as well). It's my intention that the content size is dynamic on a per object basis but does not change while the tooltip is active.

So there are three possible solutions I can see.

  1. The information on the size of the inactive panel is available and I just don't know how to get it, or

  2. I have to wait till the tooltip panel is rendered before the size information is accurate, or

  3. Have I got the wrong approach entirely here.. should I be getting the size and then positioning the thing during mouse move events (to avoid exiting the tooltip by moving the mouse over the tooltip itself .. generating a mouse exit for the original target)?

Now with i) I've tried some of the following:

  tooltipPanel.transform.GetComponent<RectTransform>().rect.width;
  tooltipPanel.transform.GetComponent<RectTransform>().sizeDelta[0];
  tooltipPanel.GetComponent<CanvasRenderer>().renderer.bounds.size.x;

But none of them seem to return the information I want (the last one doesn't work.. no renderer). I suspect that that info is not set at this stage so this is not a viable approach?

As far as ii) is concerned I'd like to avoid doing stuff in immediate mode but if it can't be helped then I guess it's ok. What would be the best way to do this? Will I get flicker when I draw the tooltip the first time so that I can get the size before moving it to the right place? Is there a callback where the tooltip panel is layed out with the right size but not yet displayed?

As for iii) it seems a little inefficient to recalculate the tooltip size all the time if it isn't changing.

Comment
Add comment · Show 4
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 maccabbe · Mar 03, 2015 at 04:53 AM 1
Share

You may want to use RectTransform.GetWorldCorners(Vector3[] fourCornersArray) to get positions of the corners in world space.

avatar image demented_hedgehog · Mar 03, 2015 at 05:59 AM 0
Share

Thanks. I'll give it a try and report back.

avatar image demented_hedgehog · Mar 03, 2015 at 06:15 AM 0
Share

Using code that looks like this:

 RectTransform rt = tooltipPanel.transform.GetComponent<RectTransform>();
 Vector3[] corners = new Vector3[4];
 rt.GetWorldCorners(corners);
 float panelWidth = $$anonymous$$athf.Abs(corners[0].x - corners[2].x);
 float panelHeight = $$anonymous$$athf.Abs(corners[0].y - corners[1].y);

I get a size of (100, 100). The tooltip isn't square and I've entered enough text to make the tooltip much bigger than that. So I'm sceptical about this being the right thing (or possibly I've done something wrong). $$anonymous$$aybe the UI layout stuff happens later?

avatar image demented_hedgehog · Mar 04, 2015 at 01:52 AM 0
Share

thanks maccabbe for the pointer in the right direction.

1 Reply

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

Answer by demented_hedgehog · Mar 04, 2015 at 01:51 AM

So.. after a bit of snooping around. The size is reported incorrectly using the GetWorldCorners(corners) method above as (100, 100) in the OnEnable() and Start() methods (well it's probably the correct size but the UI hasn't been layed out yet). The size looks to be reported correctly in the Update() method, e.g. (168, 838). So the UI layout happens sometime after the OnEnable() call. If someone has a pointer to some documentation that points to when the UI layout happens that would be nice (it'd be nice not to have this code in the Update callback - in a perfect world). There doesn't appear to be any useful events here.

So that makes me think that 2 is the answer. I just have to make sure that I only calculate the size of the tooltip when the content changes and the tooltip is active to keep the extra work in the Update() to a minimum.

Comment
Add comment · Show 1 · 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
avatar image demented_hedgehog · Mar 04, 2015 at 01:57 AM 0
Share

Accepted my own answer so the list of open questions doesn't get clogged up. If you have more info on this please post because I'm curious how it hangs together.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do i do tooltips on UI.Text objects? 1 Answer

GetComponent() Problem with getting value. 1 Answer

Dynamic UI Size? 1 Answer

Unity 4.6 UI Canvas width & height 2 Answers

UI 4.6 APK size problem : Sprite both in Atlas and Inspector 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