- Home /
How do I get an invisible object on canvas to fire OnMouseClick or an Event trigger?
I would like to get an invisible, clickable area on the screen attached to a canvas. How do I go about this?
Answer by uk007singh · Sep 15, 2017 at 07:47 AM
Add invisible image to the button image and place button anywhere in the canvas.Now call function on button click .Like public void show() { print("clicked"); }
Answer by tormentoarmagedoom · Sep 15, 2017 at 07:55 AM
Good day @IndiaVenom !
There are multiple ways to get that, i explain 2 solutions for this (but there are more for sure):
1- If this invisible area is the only clikable thing in this canvas , make the Canvas Render "Screen Space - Camera" in the Inspector. This way, the canvas is in front of the camera. SO now, you can create a Collider in the canvas that will detect when you click it with the method OnMouseDown() .
2- The second way is to detect the mouse position in he screen. This way you can do a if to determninate wich area of the screen are you interested.
int ScreenMaxX = Screen.height;
int ScreenMaxY = Screen.width;
if (Input.mousePosition.x > ScreenMaxX / 4 && Input.mousePosition.x < 3 * ScreenMaxX / 4 )
{
if (Input.GetMouseButtonDown(0)
{
//Do things
}
}
If this helps, **Upvote and Check answe**r !
If need mroe help, just ask using @tormentoarmagedoom !
Bye :D!
Your answer
Follow this Question
Related Questions
Unity2D Player Sprite not showing up in either Editor/Game Scene 0 Answers
Calling method once 2 Answers
Scrolling Repeated Background - background is scrolling but not repeating 0 Answers
How to make a background Image in 3D game with Orthographic Camera? 0 Answers
how and Where to handle code for back button in google Vr sdk v1.10.0 0 Answers