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.
- The information on the size of the inactive panel is available and I just don’t know how to get it, or
- I have to wait till the tooltip panel is rendered before the size information is accurate, or
- 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.