Hey all!
I have some buttons the player can click to place objects in the world. I want them to be able to stop placing those objects when the button is clicked again. The problem is that clicking the button triggers the click event that places the object down. I’m using the new input system, and have three action maps: UI, Player, and Camera. The UI and Player action maps both have a Click action that has a “Press [Pointer]” binding.
In my scene I have an “Input System UI Input Module” component that is mapped to the UI action map. I have another MonoBehaviour that is in charge of managing object placement, which is where I register a method to the Player.Click.performed callback.
How can I prevent the Player.Click.performed event from firing when the button gets clicked?
Sorry, are we talking about controller buttons or UI buttons here?
If we’re talking UIElements.Button, you can just extend the class and code in the desired behaviour that you want.
I’m talking about UIElement.Buttons. I’m not understanding what you mean by code the desired behavior, since the buttons are working properly. The only problem is that if you click a button when object placement is active, both the button gets clicked and the object gets placed at the same time. Instead, I want clicks that happen when over a UI element to only interact with that UI element without the object being placed.
I am… barely understanding what you’re saying, sorry.
If you want the button’s behaviour to swap between placing and stop placing… just code that?
Like, super rough example:
using UnityEngine.UIElements;
public PlaceButton : Button
{
public PlaceButton() : base(PlaceObject)
{
}
private bool placeMode = false;
private void PlaceObject()
{
placeMode = !placeMode;
if (placeMode)
{
//place things
}
else
{
//non place things
}
}
}
Obviously would be more sophisticated than this, but that’s the thing about UI elements, you can just code your own visual elements to do exactly what you want.
The buttons are working fine, my question isn’t about them exactly. Let me try to break it down another way:
- The player clicks a button which allows them to start placing objects in the world.
- The player clicks in the world, which places an object in the world.
- The player wants to stop placing objects in the world, which they can do by clicking the button again to deactivate object placement. They click the button again.
- A new object is placed in the game. At the same time, future object placement is deactivated.
Step 4 is what I’m trying to solve. Since the player has decided to stop placing objects in the world, it is incorrect for an object to be created in step 4. The only thing that should happen in step 4 is for object placement to be deactivated. When the button is clicked, the button deactivation event is being fired after the place object event.
Here’s another thread about the same topic, which a Unity employee replied to. They say it’s now handled properly, but provide no instructions on how to set your project up correctly.
Right, sort of get you now. Dunno if that thread pertains to you, exactly, it mentions it should just ‘work’ so long as you have an EventSystem with the module you mentioned.
Basically so long as you have the right picking mode on your visual elements, clicks shouldn’t click through the UI. I just tested this in my own project with some objects that use the UnityEngine.EventSystems
interfaces such as IPointerClickHandler
that the new Input system requires. These events did not fire when behind my UI.
If you’re just listening for pointer clicks then you really can’t distinguish between a button being clicked and clicking… anything else. You might be better of using the aforementioned interfaces which won’t be called should the object implementing them be behind your UI.
What we were talking about on that thread that was fixed is click-throughs - is that what you’re experiencing? If that’s the case, can you let us know what version of Unity you’re on?