Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How can I trigger MouseEnterEvent and MouseOutEvent in right time?

Discussion in 'UI Toolkit' started by whiteclouds2016, Nov 9, 2020.

  1. whiteclouds2016

    whiteclouds2016

    Joined:
    Sep 22, 2017
    Posts:
    18
    I found that the MouseOutEvent will be triggered when my mouse point in the element's children. It broke my logic in many scene.

    Like that:

    1 - I need to distinguish mouse pointer is in the UI or in the gameObject. It shouldn't trigger the MouseOutEvent that When I mouse enter UI range.

    2 - I need to the MouseEnterEvent triggered when my pointer enter UI element and the MouseOutEvent triggered when my pointer out UI element.

    I searched many post or google it but nothing. Maybe my search keyword is wrong ~

    So, is any way can trigger the MouseEnterEvent and the MouseOutEvent when I mouse enter or out UI element, and they won't be confused when I enter the UI element's children?

    I need to help !!!:( Has any idea?
     
  2. Nexer8

    Nexer8

    Joined:
    Dec 10, 2017
    Posts:
    271
    Something like this might work:
    Code (CSharp):
    1. bool yourEventWasCalled = true;
    2. VisualElement visualElementChild = new VisualElement();
    3. if(yourEventWasCalled && !visualElementChild.worldBound.Contains(Input.mousePosition))
    4. {
    5.     //Logic that happens when the event was called and the cursor is not inside the child element
    6. }
    What this snippet does is checking if the cursor is within the Rect of the child element and if not, do your logic.
     
  3. Arkenhammer

    Arkenhammer

    Joined:
    Nov 10, 2019
    Posts:
    51
    I've been doing what Xenerade suggests; essentially creating my own parallel event system. I've got a custom subclass of VisualElement which registers itself in a list when its added to the tree and removes itself when its removed from the tree. Then I add check against all the registered elements in the main loop of my game; I've done a bit extra to manage bounding boxes in a way that I don't have to check too many on any particular frame. It's a hassle but you do what ya gotta.
     
  4. whiteclouds2016

    whiteclouds2016

    Joined:
    Sep 22, 2017
    Posts:
    18
    Thanks your reply! But there are many extra work with your suggestion. It's not a simple way to detected that what I've been looking for like UGUI "EventSystem.current.IsPointerOverGameObject".
    And, I have to do what @Arkenhammer's suggestion if UI Toolkit not support this still.
     
  5. whiteclouds2016

    whiteclouds2016

    Joined:
    Sep 22, 2017
    Posts:
    18
    Thanks your suggestion! It's look like I have to do that if UI Toolkit not support this still.
     
  6. Nexer8

    Nexer8

    Joined:
    Dec 10, 2017
    Posts:
    271
    Haven't tested it, but this should register MouseEnter and MouseLeave event on all of your visual elements.
    Code (CSharp):
    1. [RequireComponent(typeof(UIDocument))]
    2. public class PointerOverRegister: MonoBehaviour {
    3.  
    4.     public static bool PointerOverGameObject;
    5.     UIDocument document;
    6.     public void OnEnable()
    7.     {
    8.         document = GetComponent<UIDocument>();
    9.         RegisterCallbackOnChildren(document.rootVisualElement);
    10.     }
    11.     void RegisterCallbackOnChildren(VisualElement v)
    12.     {
    13.         if (v.childCount == 0) return;
    14.         foreach(VisualElement child in v.Children())
    15.         {
    16.             child.RegisterCallback<MouseEnterEvent>(e => PointerOverGameObject = true);
    17.             child.RegisterCallback<MouseLeaveEvent>(e => PointerOverGameObject = false);
    18.             RegisterCallbackOnChildren(child);
    19.         }
    20.     }
    21. }
     
    oranchad likes this.
  7. whiteclouds2016

    whiteclouds2016

    Joined:
    Sep 22, 2017
    Posts:
    18
    You are right ! It is so simple that