I'm trying to make a tooltip pop up when I hover my mouse over a button. I had some issues calling the main thread from a separate thread, since the main thread is the only thread you can edit game objects from. So I decided to use a boolean and an update function to get the desired result. However, I ran into an issue I have never seen before. I am setting my boolean showToolTip to true, which is supposed to trigger the create pop-up function. However, by the time my update function runs, showTooltip is always false. I know when I set it to true, it becomes true, because I used debug statements in the requestShowTooltip function to see this. However, it is always false when the update function runs. I did some tests by changing showToolTip and removeToolTip to public booleans. I can change them in the editor while running the application, and when I manually change them through the editor they work 100% as expected, and the tooltip will appear and hide as I change the correlating booleans. However when they are changed from the script they are retained as false. I have also tried leaving them unitialized and initializing them to false in the start, and I was still having the same problem. I even tried taking "showTooltip = true;" out of the thread and just having the function do it immediately, and I was still having the same issue. showTooltip is always false when the update function is called. I have determined this by adding a debug statement in the update function that reports the value of the showTootip and removeTooltip booleans. Furthermore, no matter what, "Calling create tooltip" never appears in the console. Except for when I made the booleans public and changed them through the editor.
Some further explanation on how I set this up. I made a Tooltip Game Object that is a child of the canvas that contains the buttons I want the tooltip for. I added the Tooltips script to the tooltip object. I created event triggers in the button Game Objects. the Pointer Enter trigger calls the requestShowTooltip() function, and the Pointer Exit trigger calls the requestHideTooltip() function.
The Tooltip Background and Tooltip Text Game Objects are also children of this same canvas.
public class Tooltips : MonoBehaviour
{
private GameObject toolTipBackground;
private GameObject toolTipText;
private GameObject inButtonObject;
private bool showTooltip = false;
private bool removeTooltip = false;
void Start()
{
toolTipBackground = GameObject.Find("Tooltip background");
toolTipText = GameObject.Find("Tooltip Text");
//inButton = GameObject.Find("Aft Button");
toolTipBackground.SetActive(false);
toolTipText.SetActive(false);
//Debug.Log("Tool Tip Start");
}
void Update()
{
// show the tooltip when the appropriate boolean has been set
// to true
if(removeTooltip)
{
hideTooltip();
}
// this if statement is always false, because showTooltip is
// always false when the update function is called??
if(showTooltip)
{
Debug.Log("Calling create tooltip");
createTooltip();
}
}
// A timed request for a tooltip to show. The tooltip should
// currently show one second after the request has been made. The
// text of the tooltip will be equal to the passed object's name
public void requestShowTooltip(GameObject inButtonObject)
{
Thread thread = new Thread(delegate ()
{
System.Threading.Thread.Sleep(1000);
this.inButtonObject = inButtonObject;
// I know this runs, but showTooltip is always returning
// to false when the update function is called??
showTooltip = true;
removeTooltip = false;
Debug.Log("Request function completed");
});
thread.start();
}
public void createTooltip()
{
toolTipText.SetActive(true);
toolTipBackground.SetActive(true);
string labelString = inButtonObject.name;
Button inButton = inButtonObject.GetComponent<Button>();
int width = labelString.Length * 10;
int height = 35;
Vector2 size = new Vector2(width, height);
Vector3 position = new Vector3(inButton.transform.x,
inButton.transform.position.y,
inButton.transform.position.z + 1);
toolTipBackground.GetComponent<RectTransform>().
sizeDelta = size;
toolTipText.GetComponent<RectTransform>().sizeDelta = size;
toolTipBackground.transform.position = position;
toolTipText.transform.position = position;
toolTipText.GetComponent<Text>().text = labelString;
showTooltip = false;
}
public void requestHideTooltip()
{
removeTooltip = true;
showTooltip = false;
}
public void hideTooltip()
{
Vector2 hide = new Vector2(0, 0);
toolTipBackground.GetComponent<RectTransform>().
sizeDelta = hide;
toolTipText.GetComponent<RectTransform>().sizeDelta = hide;
toolTipText.setActive(false);
toolTopBackground.SetActive(false);
removeTooltip = false;
showTooltip = false;
}
}