Friday, May 4, 2018

ProductiveGame 2 Trees Update #1

Making a tooltip implementation has been on my todo list for years, for multiple project.  I finally buckled down and made one for ProducteGame 2 - Trees.

It's not perfect, but it works pretty well.   It took much longer to get working right than I had anticipated.  There were many hurdles I didn't expect.  There is one panel prefab, with a text object, and a content size fitter, and a horizontal layout group.  It also has a script that makes the prefab instance a public static.  Another script attached to the other ui objects, sets the initial position of the tooltip based upon the center of it's parent.  Then it waits for half a second.  If the cursor is still within the parent object,  it enables the tooltip.  If the tooltip edges are within the game window, it stays in the same position.  If the tooltip edges are outside the game window it repositions the tooltip and saves the new position to be reused.  Whenever the cursor leaves the parent UI object, the tooltip disables.

Multiline text is kind of hacky, but it works.  The text is assigned in the inspector, and in the Windows Unity Editor, "\n" for new line doesn't work when it is typed in the editor for some reason.  So instead I use "\\n" with some code that replaces the text in the string, at runtime.  Like this:
tooltipText = tooltipText.Replace("\\n", "\n");

Because the code finds the tooltip position based upon its size, and its size is being driven by the  unity ui(content size fitter and horizontal layout group), the code has to wait until the end of the frame to find the position.  This causes the tooltip to sort of pop in to place, which is noticeable.  However, the code stores the position, so this only happens when the game first starts, or when the window is resized.  There might be a way I could calculate if the tooltip will fall outside the window beforehand, but I don't know how to do that, and the pop-in isn't very noticeable since it only happens once after a resize, or at start.

Added Hover over text.
Messed around with font sizes for the header, to try to make things a little more compact and a little more uniform

No comments:

Post a Comment