Unity - TextMeshPro button is clicked through - unity3d

I am making a Tower Defence game for mobile and I'm struggling to fix this bug for the last couple of weeks. In order to build a tower, I tap on a tile, which pops up a UI menu with a couple of towers to choose from. If behind the location I tap on the UI to choose the tower there is another valid position to build a tower, the tap goes through the UI and selects the new valid tile.
I have an empty game object, which nests a canvas, which nests the buttons.
I have tried using the following possible fixes, but didn't work in my case:
Graphic raycaster on the canvas
if (EventSystem.current.IsPointerOverGameObject())
return;
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
if (EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId))
return;
How should I proceed?

You could try putting a sprite behind the UI object when it gets opened so that the tap only applies to the button and the sprite. You could also make the sprite transparent.

Related

Unity New Input system click UI elements

My game is a 3D shooter, and the player can shoot if left mouse button is clicked.
The problem is that I have also an UI Menu with buttons to restart the game, but I cannot click those buttons because in my action map click is equal to shoot.
I try to disable the player input and change the current action map to none, so I can press the buttons like the old input system with the Onclick() but it doesn't work.
At the time of displaying a Game Over screen I tried to disable the lock of the cursor.
PlayerController.sharedInstance.GetComponent<ThirdPersonController>().enabled = false;
PlayerController.sharedInstance.GetComponent<StarterAssetsInputs>().cursorLocked = false;
PlayerController.sharedInstance.GetComponent<StarterAssetsInputs>().cursorInputForLook = true;
I think I can start shooting raycast and check what element of the canvas is hit, and by the name of the button perform actions by I don't think this is the best idea.
Could you tell how can I click my UI elements please?
Thank you in advance.

Unity specific area touch input

i want to make an area in my game where i can get touch inputs from the player.
Now i get touch from all the way of the screen.
strong textI want to get from the buttom of the screen or so.
You can make an invisible UI element or GameObject and on there check if it was clicked or not. You can do this with either https://docs.unity3d.com/2018.1/Documentation/ScriptReference/EventSystems.IPointerClickHandler.html or https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnMouseDown.html

unity3d detect gameobject enters in main camera screen and leaves

I'm looking for events or a code that will give me information that gameobject entered in the main camera screen and then leaves the screen so i can destroy it.
What i'm doing is a top down game which is moving forward all the time... some objects are spawned far away so i want them to move when they enter in the game screen and after leaving the screen destroy them.
i tried this event but it's triggered before game object enters in the screen... i saw some comments that rendering engine is triggering this event for shadow rendering bla bla...
https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnBecameVisible.html
On top of OnBecomeVisible you also have other options such as OnWillRenderObject, Renderer.isVisible
You can also calculate whether an object's bounding box falls within the camera's view frustum, using GeometryUtility.TestPlanesAABB

Start game if anything but a UI Button is touched

I'm building an iOS game and I had the game set up to start whenever the user first touched the screen. This was working out perfectly fine for testing until I added menu buttons as an overlay that would disappear when the game starts. Obviously I can no longer just check for user input, because the game now starts even if I tap a UI button.
I tried adding an invisible UI panel with a box collider behind my UI buttons with an OnPointerDown that would start the game, but for some reason it was acting very inconsistent and would sometime require 5+ taps to start the game. It also wouldn't start if you tapped on the player because it would hit his collider instead.
Is there a better way to do this?

Touch not detectable past Panel - Unity3D/Vuforia

I have been trying to add touch event to 3D gameObject in Unity.
Previously I was not using any Canvas or Panel so by using Event Trigger and Event System I was able to add touch events to the Gameobject but then I wanted to use a UI for Application and implemented this hierarchy.
Camera
GameOBject
Canvas
Panel(Transparent)
-Buttons
Panel2
EventSystem
So if i tap a part it does not respond.
Camera does have Physics Raycaster.
Gameobjects have colliders and mesh renderers.
I want a touch input from the mobile device.
Thanks
If two Button overlap each other then the order in which camera renders top will be detected.
So try changing the hierarchy order (remember the last in the hierarchy is always top of the camera ) you can achieve which one to click