Announcement

Collapse
No announcement yet.

How to detect if the mouse is over the UI?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    How to detect if the mouse is over the UI?

    I have various actors in my game world that can respond based on the mouse's position on the screen every tick. The problem is that when the mouse moves over the UI, the game world behind it is still responding to the mouse's position on the screen. This looks odd as you can see things happening behind the semi-transparent UI while moving the mouse around. So, I'm wondering if there is some way to check if the mouse is currently over any UMG widget? Ideally, in the Tick function of an Actor, I'd like to be able to check, Is Mouse Over UI? If no, respond to mouse position on screen, if yes, ignore and drop out. I don't mind writing C++ if I need to in order to expose something to Blueprint that will help me achieve this. Anyone have any ideas?

    #2
    There is Is Hovered. You can add the elements you want to check to a widget array and check them in a for loop if you've got multiple ones.

    In my BP I'm just checking whether EnteredWidget is true or not, if it's false it checks for Is Hovered in another branch, if it's true it sets EnteredWidget to true. In the next tick the execution flow will go to the true part of the branch and the single widget that was hovered is checked for if it's hovered or not. In my case I'm not doing anything special if it's still hovered, I'm just doing an animation with a timer to fade in and fade out the widget. But you could add a DoOnce node to the true branch and set the mouse mode to UI only for example.

    Comment


      #3
      Yeah I saw the Is Hovered, but was hoping there was a better way. It would mean I would need to iterate over every widget in the viewport and check if it Is Hovered, every single tick, which just seems wasteful.

      Comment


        #4
        In the widget override onMouseEnter and set game mode to UI only, in the same widget onMouseLeave sets the game mode back to Game&UI. If that's not enough, dispatch a bool on widget Enter/Leave to prevent/allow game world interaction.

        Comment


          #5
          Originally posted by wilberolive View Post
          Yeah I saw the Is Hovered, but was hoping there was a better way. It would mean I would need to iterate over every widget in the viewport and check if it Is Hovered, every single tick, which just seems wasteful.
          It seems wasteful but in reality my method used 0.025ms average on the game thread which didn't seem like a lot. And when you're packaging the project with nativized BPs it turns into an even smaller issue. If you wrap widgets within bigger widgets it's not a huge performance hit. It might not be useful if you're using modularized widgets however.

          Comment


            #6
            Thanks for all your suggestions. I've worked out a very fast C++ solution, so I can use it on Tick. Consider this case closed.

            Comment

            Working...
            X