- Home /
Setting tooltip text depending on condition
Hello,
Wondering if anyone can help me solve a problem concerning tooltips. I have a character screen and a tooltip at the bottom of the screen to explain the label/button the player has the mouse over. This is working fine, but I'd like to have the text change depending on certain conditions present in the scene.
Example
// Speed + Button
if(GUI.Button(new Rect(x, y, a, b), new GUIContent("", "Increases Character Speed"), "StatButton")
{
// Condition satisfied
if(freeAttributePoints > 0)
{
// No need to show a message, just leave the button GUI content
}
// Condition not satisfied
else
{
// Show a different message on tooltip, i.e. No free attribute points
}
}
How I'm declaring tooltip:
// ToolTip Label
GUI.Label(new Rect(0, tooltipYPos, tooltipWidth, tooltipHeight), GUI.tooltip, "ToolTip");
Thanks for reading!
Following Ricardo's suggestion, I was able to solve the issue. I drew a second label (alt tool tip) over the tooltip, and only had it display when a bool was true. Then, when the button was pressed, I loaded in the string to be displayed by the alt tooltip, and set the boolean to draw over the original tooltip to true. A timer was used to reset the boolean, and allow the original tooltip to be seen.
Answer by ricardo_arango · Apr 20, 2013 at 04:58 PM
You could make the tooltip text a variable:
string tooltip = "Increases Character Speed";
then when the button is clicked and the condition is met, replace it with a different value:
tooltip = "a different message on tooltip";
You would display your button like this:
// Speed + Button
if (GUI.Button(new Rect(x, y, a, b), new GUIContent("", tooltip), "StatButton"))
{
// Condition satisfied
if (freeAttributePoints > 0)
{
// No need to show a message, just leave the button GUI content
}
// Condition not satisfied
else
{
// Show a different message on tooltip, i.e. No free attribute points
tooltip = "a different message on tooltip";
}
}
Thanks Ricardo. $$anonymous$$inda get the idea, but it's not quite working yet, the different message in the else block isn't showing. The "Increase character speed" keeps showing. Guessing the else block tooltip string is being overwritten by string tooltip = "Increase Character Speed". Any ideas?
string tooltip = "Increase Character Speed";
// Speed + Button
if(GUI.Button(new Rect(x, y, a, b), new GUIContent("", tooltip), "StatButton")
{
// Condition satisfied
if(freeAttributePoints > 0)
{
// No need to show a message, just leave the button GUI content
}
// Condition not satisfied
else
{
// Show a different message on tooltip, i.e. No free attribute points
tooltip = "No free stat points";
}
}
You need to define the "tooltip" variable outside of the OnGUI function, as part of the class. Otherwise it is initialized every frame.