Author Topic: How to detect Mouse inside a specific Element?  (Read 1817 times)

Chaosgod_Espér

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 28
    • View Profile
How to detect Mouse inside a specific Element?
« on: December 15, 2015, 01:48:25 PM »
Hi there,

after i created half of my Ingame Editor, i struggle with a Problem... See:


You see the Scrollview in the ledt Window, that holds the Tileset.
With the local position of the mouse position to world coordinates, i was able to calculate the clicked tile inside the view and placed the white cursor rect.

The Problem now:
The Editor registrates clicks outside the Scrollview, too.

So the Question is:
How to get a TRUE if the mouse hovers a specific UI Element?
like:
  1. if(mouseposition hovers Scrollview)
  2. {
  3.     //Cursorcode here
  4. }

Hint:
i tried UICamera.IsOverUI, but that returns true over EVERY UI.. like the Scrollview, the Window, each Button, Label..etc..  Bur not specified to one Element only..
« Last Edit: December 15, 2015, 03:16:31 PM by Chaosgod_Espér »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: How to detect Mouse inside a specific Element?
« Reply #1 on: December 16, 2015, 02:55:07 AM »
UICamera.hoveredObject will tell you what the mouse is hovering over (assuming you call it from within an NGUI event). The key here is using NGUI's events. If you are doing your own Input checks, then you are effectively bypassing the NGUI's event system, which will lead to issues. If you are bypassing NGUI's code, then it's up to you to check that sort of stuff. Checking to see if the event is inside some region is generally a simple task. Given the mouse position, convert to screen space, then check against the known dimensions. Alternatively, if you just use the UICamera.hoveredObject you will know what it's hovering over, so you can use GetComponentInParent to find your scroll view.