ScreenToWorldPoint causing OnPointerEnter/Exit to fire multiple times
Hi, Trying to create a tooltip using Screenspace.camera - so instead of just doing mytransform.transform.position = input.mouseposition
I have to do some calculations like so;
var screenPoint = new Vector3(Input.mousePosition.x , Input.mousePosition.y);
screenPoint.z = 10.0f; //distance of the plane from the camera
ToolTipGO.transform.position = Camera.main.ScreenToWorldPoint(screenPoint);
The tooltip is beeing shown and follows the mouse araound perfectly, but theres a problem with multiple fireings of onPointerEnter and onPointerExit, it spams my log, and obviously causes the tooltip-gameobject to be instantiated and deleted, making it look like its flickering.
I have tried switching to diffrent Camera.main.DiffrentTypesOfFunctions (and even disabled that last line) and this fires the events as expected - 1 time, solving all issues appart from the tooltip not beeing at the mouse position...
My scripts are as follows:
public class Tooltip : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
{
GameObject UIManager;
public string _thisTooltipsString;
void Start()
{
UIManager = GameObject.Find("UIManager");
}
public void OnPointerEnter(PointerEventData eventData)
{
UIManager.GetComponent<TooltipMouseController>().CreateToolTip(thisTooltipsString);
}
public void OnPointerExit(PointerEventData eventData)
{
print("Fire");
UIManager.GetComponent<TooltipMouseController>().DestroyToolTip();
}
}
This script is attached to every gameobject i want to contain a tooltip
Next i have this attached to my UImanager object:
public class TooltipMouseController : MonoBehaviour
{
GameObject ToolTipPrefab;
GameObject ToolTipGO;
void Start()
{
ToolTipPrefab = Resources.Load("ToolTipPrefab") as GameObject;
}
public void CreateToolTip(string s)
{
ToolTipGO = Instantiate(ToolTipPrefab);
ToolTipGO.transform.SetParent(GameObject.Find("Background").transform , false);
ToolTipGO.transform.FindChild("Text").GetComponent<Text>().text = s;
}
public void DestroyToolTip()
{
Destroy(ToolTipGO.gameObject);
}
void LateUpdate()
{
if( ToolTipGO != null )
{
var screenPoint = new Vector3(Input.mousePosition.x , Input.mousePosition.y);
screenPoint.z = 10.0f; //distance of the plane from the camera
ToolTipGO.transform.position = Camera.main.ScreenToWorldPoint(screenPoint); //This last line can be commented out, and onPointerEnter/Exit only fires once like its supposed to - but tooltip position is ofc no longer in the correct position
}
}
}
So am i doing something wrong? Why does it keep fireing on LateUpdate calculations?
Answer by wicea · Feb 16, 2017 at 04:03 PM
I faced with same issue, that my tooltip script on object fired multiple times OnPointerEnter/OnPointerExit
. I was really surprised, that without using ScreenToWorldPoint and transform.TransformPoint everything works fine. After a while I discovered the cause.
I activate tooltip prefab with Image component (option "Block raycast" is turned on) in OnPointerEnter
. So my mouse cursor is over tooltip prefab now and tooltip script invokes OnPointerExit
and... this deactivates tooltip prefab. And this scenery repeats again and again.
In my case I fixed it just by unchecking "block raycast" option on my tooltip's arrow image. I think you have similar issue.
Answer by celtcraftgames · Nov 04, 2020 at 09:23 AM
In my case, the tooltip itself was blocking raycasts and that was causing the flickering. Make sure all text and image elements of the tooltip have "Raycast Target" unchecked.