Does anyone know how to prevent the OnMouseOver() from firing? I tried adding a 2d box collider but it didn’t stay with the button I made. this is my simple tester script
public class HoverTest : MonoBehaviour {
bool hover = false;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if(hover) {
renderer.material.color = Color.yellow;
hover = false; //reset for next frame
}
else {
renderer.material.color = Color.white;
}
}
void OnMouseOver() {
hover = true;
}
}
Its the same with a simple raycast using
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hitInfo;
if(Physics.Raycast(ray, out hitInfo)) {
if(hitInfo.transform == transform) {
hover = true;
}
}
Be sure to also check out the CanvasGroup component if you wish to block raycasts, either to the background scene or other UI compnents.
Will G gave a pretty good demonstration of it in his UI introduction video
Yes, there is the new ICanvasRaycastFilter interface where you can define how your object reacts to the above call. We updated all of the primitive controls in the UIExtensions project to make use of this.
So now you can override if the IsPointerOverGameObject will return true or not per object.